Skip to content

Commit de465fe

Browse files
committed
Merge pull request #11 from dNitza/Units
Adding units as a valid options
2 parents 150d605 + c69be32 commit de465fe

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ Add the following to your **Gemfile**
2525
# get current weather by geocode. (lat, lon)
2626
OpenWeather::Current.geocode(9.94, 76.26)
2727

28+
# get the current weather in degrees celsius
29+
OpenWeather::Current.city("Cochin, IN", units: 'metric')
30+
2831
# weather forecast APIs
2932

3033
# get weather forecast by city name
3134
OpenWeather::Forecast.city("Cochin, IN")
3235

36+
# get weather forecast by city name in fahrenheit
37+
OpenWeather::Forecast.city("Cochin, IN", units: 'imperial')
38+
3339
# get weather forecast by city id
3440
OpenWeather::Forecast.city_id("1273874")
3541

lib/open_weather/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def success?
2424
private
2525

2626
def extract_options!(options)
27-
valid_options = [:lat, :lon, :city, :country, :id]
27+
valid_options = [:lat, :lon, :city, :country, :id, :units]
2828
options.keys.each { |k| options.delete(k) unless valid_options.include?(k) }
2929

3030
if options[:city] || options[:country]

spec/open_weather/api_spec.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@
6767
response['cod'].should eq("404")
6868
end
6969
end
70+
71+
context 'units option' do
72+
it 'returns the current temperature in requested units' do
73+
response = OpenWeather::Current.city('Cochin, In', units: 'metric')
74+
temp_metric = response['main']['temp']
75+
76+
response = OpenWeather::Current.city('Cochin, In', units: 'imperial')
77+
temp_imperial = response['main']['temp']
78+
farenheit_to_celsius = ((temp_imperial - 32) / 1.8000)
79+
80+
expect(farenheit_to_celsius).to be_within(0.01).of(temp_metric)
81+
end
82+
end
7083
end
7184

7285
describe 'Open weather Forecast API' do
@@ -105,4 +118,17 @@
105118
response['cod'].to_s.should eq("404")
106119
end
107120
end
108-
end
121+
122+
context 'units option' do
123+
it 'returns the forecast in requested units' do
124+
response = OpenWeather::Forecast.city('Cochin, In', units: 'metric')
125+
temp_metric = response['list'].first['main']['temp']
126+
127+
response = OpenWeather::Forecast.city('Cochin, In', units: 'imperial')
128+
temp_imperial = response['list'].first['main']['temp']
129+
farenheit_to_celsius = ((temp_imperial - 32) / 1.8000)
130+
131+
expect(farenheit_to_celsius).to be_within(0.01).of(temp_metric)
132+
end
133+
end
134+
end

0 commit comments

Comments
 (0)