From 86d3a8309ecdba07268a61f51da239188fed70c0 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sat, 13 Dec 2014 20:12:52 -0800 Subject: [PATCH] Add option to configure temperature units. Fixes #3. --- CHANGELOG.md | 4 ++++ README.md | 2 ++ alfred.rb | 3 ++- config-forecast.rb | 3 ++- forecaster.rb | 3 ++- 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a74c4fb..fb01049 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.6 - 2014.12.13 +### Added +- Add option to force Celsius/Fahrenheit using `FORECAST_UNITS`. + ## 0.0.5 - 2014.11.22 ### Changed - Use `forecast-config` for managing API keys. diff --git a/README.md b/README.md index 0fef7b1..9be682a 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ Run `forecast-config VALUE` to set API keys and the default location: - `DEFAULT_LOCATION`: Ex. "Seattle, WA". - `DEFAULT_LAT_LONG`: Only required if `GOOGLE_API_KEY` is unavailable, since `DEFAULT_LOCATION` can't be geocoded. Format: `lat,long`. +- `FORECAST_UNITS`: Defaults to `auto`, which sets the units based on the + location. Use `si` for Celsius and `us` for Fahrenheit. [forecast-api-key]: https://developer.forecast.io/register [google-api-key]: https://developers.google.com/maps/documentation/geocoding/#api_key diff --git a/alfred.rb b/alfred.rb index 67085f3..b7e77fa 100644 --- a/alfred.rb +++ b/alfred.rb @@ -64,6 +64,7 @@ def self.config @config = self.new(bundle_id) end + DEFAULTS = { 'FORECAST_UNITS' => 'auto' } WORKFLOW_DATA = '~/Library/Application Support/Alfred 2/Workflow Data/' attr_reader :path @@ -78,7 +79,7 @@ def initialize(bundle_id) end def [](key) - config.fetch(key) { '' } + config.fetch(key) { DEFAULTS.fetch(key) { '' } } end def []=(key, value) diff --git a/config-forecast.rb b/config-forecast.rb index df61417..945f176 100644 --- a/config-forecast.rb +++ b/config-forecast.rb @@ -3,7 +3,8 @@ OPTIONS = %w[ FORECAST_API_KEY GOOGLE_API_KEY DEFAULT_LOCATION - DEFAULT_LAT_LONG ] + DEFAULT_LAT_LONG + FORECAST_UNITS ] input = ARGV.shift || '' diff --git a/forecaster.rb b/forecaster.rb index 7379bd3..cd68fb2 100644 --- a/forecaster.rb +++ b/forecaster.rb @@ -16,7 +16,8 @@ def self.forecaster def forecast(location) lat, long = location.lat, location.long - url = "https://api.forecast.io/forecast/#{api_key}/#{lat},#{long}?units=auto" + units = Alfred::Config['FORECAST_UNITS'] + url = "https://api.forecast.io/forecast/#{api_key}/#{lat},#{long}?units=#{units}" response = JSON.load(open(url)) end end