Skip to content

Commit 5ee8d90

Browse files
author
Jesse Emond
committed
Add cities API call to query a list of cities
Add an optional URL parameter to retrieve and send_request to use instead of the class URL (in the case of queries to multiple cities at once, the URL differs from the regular `current` queries).
1 parent a787748 commit 5ee8d90

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/open_weather/api.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,21 @@ def geocode(lat, lon, options = {})
1919
end
2020
end
2121

22+
module SeveralCitiesClassMethods
23+
# City Ids, an array of integer values. Eg, [2172797, 524901]
24+
# Usage: OpenWeather::Current.cities([2172797, 524901])
25+
def cities(ids, options = {})
26+
url = 'http://api.openweathermap.org/data/2.5/group'
27+
ids = ids.join ',' # get comma-separated list, as required by the API
28+
new(options.merge(id: ids)).retrieve url
29+
end
30+
end
31+
2232
class Base
2333
extend ClassMethods
2434
end
35+
36+
class Current
37+
extend SeveralCitiesClassMethods
38+
end
2539
end

lib/open_weather/base.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def initialize(url, options)
1212
@options = extract_options!(options)
1313
end
1414

15-
def retrieve
16-
response = send_request unless @options.empty?
15+
def retrieve(url=nil)
16+
response = send_request url unless @options.empty?
1717
parse_response(response)
1818
end
1919

@@ -46,8 +46,9 @@ def parse_response(response)
4646
@weather_info
4747
end
4848

49-
def send_request
50-
uri = URI(@url)
49+
def send_request(url=nil)
50+
url = url || @url
51+
uri = URI(url)
5152
uri.query = URI.encode_www_form(options)
5253
Net::HTTP.get(uri)
5354
end

0 commit comments

Comments
 (0)