Skip to content
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Installation

Latest version `0.11.0`
Latest version `0.12.0`

Add the following to your **Gemfile**

Expand All @@ -31,6 +31,15 @@ OpenWeather::Current.city_id("1273874")
# get current weather by geocode. (lat, lon)
OpenWeather::Current.geocode(9.94, 76.26)

# get current weather for a list of city ids
OpenWeather::Current.cities([524901, 703448, 2643743])

# get current weather for a bounding box
OpenWeather::Current.rectangle_zone(12, 32, 15, 37, 10)

# get current weather for cities around a point
OpenWeather::Current.circle_zone(55.5, 37.5, 10)

# get the current weather in degrees celsius
OpenWeather::Current.city("Cochin, IN", units: 'metric')
```
Expand Down
38 changes: 38 additions & 0 deletions lib/open_weather/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,45 @@ def geocode(lat, lon, options = {})
end
end

module SeveralCitiesClassMethods
# City Ids, an array of integer values. Eg, [2172797, 524901]
# Usage: OpenWeather::Current.cities([2172797, 524901])
def cities(ids, options = {})
url = 'http://api.openweathermap.org/data/2.5/group'
ids = encode_array ids
new(options.merge(id: ids)).retrieve url
end

# Bounding box (lat and lon of top left and bottom right points, map zoom)
# Usage: OpenWeather::Current.rectangle_zone(12, 32, 15, 37, 10)
def rectangle_zone(top_left_lat, top_left_lon,
bottom_right_lat, bottom_right_lon,
map_zoom, options = {})
url = 'http://api.openweathermap.org/data/2.5/box/city'
bbox = encode_array [top_left_lat, top_left_lon, bottom_right_lat,
bottom_right_lon, map_zoom]
new(options.merge(bbox: bbox)).retrieve url
end

# Circle zone (lat, lon and count of cities to return)
# Usage: OpenWeather::Current.circle_zone(55.5, 37.5, 10)
def circle_zone(lat, lon, count, options = {})
url = 'http://api.openweathermap.org/data/2.5/find'
new(options.merge(lat: lat, lon: lon, cnt: count)).retrieve url
end

private
# Encodes an array in the format expected by the API (comma-separated list)
def encode_array(arr)
arr.join ','
end
end

class Base
extend ClassMethods
end

class Current
extend SeveralCitiesClassMethods
end
end
11 changes: 6 additions & 5 deletions lib/open_weather/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def initialize(url, options)
@options = extract_options!(options)
end

def retrieve
response = send_request unless @options.empty?
def retrieve(url=nil)
response = send_request url unless @options.empty?
parse_response(response)
end

Expand All @@ -25,7 +25,7 @@ def success?

def extract_options!(options)
valid_options = [ :id, :lat, :lon, :cnt, :city, :lang, :units, :APPID,
:country]
:country, :bbox]

options.keys.each { |k| options.delete(k) unless valid_options.include?(k) }

Expand All @@ -46,8 +46,9 @@ def parse_response(response)
@weather_info
end

def send_request
uri = URI(@url)
def send_request(url=nil)
url = url || @url
uri = URI(url)
uri.query = URI.encode_www_form(options)
Net::HTTP.get(uri)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/open_weather/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module OpenWeather
VERSION = '0.11.0'
VERSION = '0.12.0'
end
47 changes: 47 additions & 0 deletions spec/fixtures/cassettes/api/current_circle_zone_invalid.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions spec/fixtures/cassettes/api/current_circle_zone_valid.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions spec/fixtures/cassettes/api/current_cities_invalid.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions spec/fixtures/cassettes/api/current_cities_valid.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading