diff --git a/README.md b/README.md index 6d86b99..a2541c2 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,9 @@ OpenWeather::Current.rectangle_zone(12, 32, 15, 37, 10, options) # get current weather for cities around a point OpenWeather::Current.circle_zone(55.5, 37.5, 10, options) +# get current weather by zip code (zip,country_code) +OpenWeather::Current.zip("33704,US", options) + # By default temperature is returned in fahrenheit to get the current weather in degrees celsius use unit as follows. options = { units: "metric", APPID: "1111111111" } OpenWeather::Current.city("Cochin, IN", options) @@ -117,7 +120,7 @@ OpenWeather::History.city_id("1273874", options) OpenWeather::History.geocode(9.94, 76.26, options) ``` -Doucumentation about the weather forecast end-point: +Documentation about the weather forecast end-point: http://openweathermap.org/forecast diff --git a/lib/open_weather/api.rb b/lib/open_weather/api.rb index 62aaa3f..51d4c56 100644 --- a/lib/open_weather/api.rb +++ b/lib/open_weather/api.rb @@ -17,6 +17,12 @@ def city_id(id, options = {}) def geocode(lat, lon, options = {}) new(options.merge(lat: lat, lon: lon)).retrieve end + + # Zip code format: '33704,US' + # Usage: OpenWeather::Current.zip('33704,US') + def zip(zip_code, options = {}) + new(options.merge(zip: zip_code)).retrieve + end end module SeveralCitiesClassMethods diff --git a/lib/open_weather/base.rb b/lib/open_weather/base.rb index f34cf94..f025f13 100644 --- a/lib/open_weather/base.rb +++ b/lib/open_weather/base.rb @@ -24,7 +24,7 @@ def success? private def extract_options!(options) - valid_options = [ :id, :lat, :lon, :cnt, :city, :lang, :units, :APPID, + valid_options = [ :id, :zip, :lat, :lon, :cnt, :city, :lang, :units, :APPID, :country, :bbox, :q, :type, :start, :end] options.keys.each { |k| options.delete(k) unless valid_options.include?(k) } diff --git a/spec/fixtures/cassettes/api/current_zip_invalid.yml b/spec/fixtures/cassettes/api/current_zip_invalid.yml new file mode 100644 index 0000000..a6cd416 --- /dev/null +++ b/spec/fixtures/cassettes/api/current_zip_invalid.yml @@ -0,0 +1,47 @@ +--- +http_interactions: +- request: + method: get + uri: http://api.openweathermap.org/data/2.5/weather?zip=00000%2CUS + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Host: + - api.openweathermap.org + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Thu, 28 Aug 2014 10:50:49 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + X-Source: + - back + Access-Control-Allow-Origin: + - "*" + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Methods: + - GET, POST + body: + encoding: UTF-8 + string: | + {"message":"city not found","cod":"404"} + http_version: + recorded_at: Thu, 28 Aug 2014 10:50:49 GMT +recorded_with: VCR 2.9.2 diff --git a/spec/fixtures/cassettes/api/current_zip_valid.yml b/spec/fixtures/cassettes/api/current_zip_valid.yml new file mode 100644 index 0000000..828ae41 --- /dev/null +++ b/spec/fixtures/cassettes/api/current_zip_valid.yml @@ -0,0 +1,47 @@ +--- +http_interactions: +- request: + method: get + uri: http://api.openweathermap.org/data/2.5/weather?zip=33704%2CUS + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Host: + - api.openweathermap.org + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Thu, 28 Aug 2014 10:50:48 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + X-Source: + - back + Access-Control-Allow-Origin: + - "*" + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Methods: + - GET, POST + body: + encoding: UTF-8 + string: | + {"coord":{"lon":-82.64,"lat":27.77},"sys":{"message":0.0191,"country":"US","sunrise":1409186752,"sunset":1409231181},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"base":"cmc stations","main":{"temp":299.15,"temp_min":299.15,"temp_max":299.15,"pressure":1016,"sea_level":1016,"grnd_level":1015,"humidity":72},"wind":{"speed":3.6,"deg":170},"clouds":{"all":0},"dt":1409222627,"id":4172131,"name":"St. Petersburg","cod":200} + http_version: + recorded_at: Thu, 28 Aug 2014 10:50:48 GMT +recorded_with: VCR 2.9.2 diff --git a/spec/open_weather/api_spec.rb b/spec/open_weather/api_spec.rb index 2a71474..abde3ec 100644 --- a/spec/open_weather/api_spec.rb +++ b/spec/open_weather/api_spec.rb @@ -31,6 +31,22 @@ end end + context '.zip' do + it 'returns current weather for a valid zip code' do + response = VCR.use_cassette('api/current_zip_valid') do + OpenWeather::Current.zip('33704,US') + end + response['cod'].should eq(200) + end + + it 'returns error if the zip code is invalid' do + response = VCR.use_cassette('api/current_zip_invalid') do + OpenWeather::Current.zip('00000,US') + end + response['cod'].should eq('404') + end + end + context '.geocode' do it 'return current weather for geocode of cochi' do response = VCR.use_cassette('api/current_geocode_valid') do