Skip to content

Commit 2a1fcf0

Browse files
committed
Merge pull request #27 from JesseEmond/master
Support API calls for several cities
2 parents 8189f78 + 7a3c74d commit 2a1fcf0

16 files changed

+769
-9
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Installation
66

7-
Latest version `0.11.0`
7+
Latest version `0.12.0`
88

99
Add the following to your **Gemfile**
1010

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

34+
# get current weather for a list of city ids
35+
OpenWeather::Current.cities([524901, 703448, 2643743])
36+
37+
# get current weather for a bounding box
38+
OpenWeather::Current.rectangle_zone(12, 32, 15, 37, 10)
39+
40+
# get current weather for cities around a point
41+
OpenWeather::Current.circle_zone(55.5, 37.5, 10)
42+
3443
# get the current weather in degrees celsius
3544
OpenWeather::Current.city("Cochin, IN", units: 'metric')
3645
```

lib/open_weather/api.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,45 @@ 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 = encode_array ids
28+
new(options.merge(id: ids)).retrieve url
29+
end
30+
31+
# Bounding box (lat and lon of top left and bottom right points, map zoom)
32+
# Usage: OpenWeather::Current.rectangle_zone(12, 32, 15, 37, 10)
33+
def rectangle_zone(top_left_lat, top_left_lon,
34+
bottom_right_lat, bottom_right_lon,
35+
map_zoom, options = {})
36+
url = 'http://api.openweathermap.org/data/2.5/box/city'
37+
bbox = encode_array [top_left_lat, top_left_lon, bottom_right_lat,
38+
bottom_right_lon, map_zoom]
39+
new(options.merge(bbox: bbox)).retrieve url
40+
end
41+
42+
# Circle zone (lat, lon and count of cities to return)
43+
# Usage: OpenWeather::Current.circle_zone(55.5, 37.5, 10)
44+
def circle_zone(lat, lon, count, options = {})
45+
url = 'http://api.openweathermap.org/data/2.5/find'
46+
new(options.merge(lat: lat, lon: lon, cnt: count)).retrieve url
47+
end
48+
49+
private
50+
# Encodes an array in the format expected by the API (comma-separated list)
51+
def encode_array(arr)
52+
arr.join ','
53+
end
54+
end
55+
2256
class Base
2357
extend ClassMethods
2458
end
59+
60+
class Current
61+
extend SeveralCitiesClassMethods
62+
end
2563
end

lib/open_weather/base.rb

Lines changed: 6 additions & 5 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

@@ -25,7 +25,7 @@ def success?
2525

2626
def extract_options!(options)
2727
valid_options = [ :id, :lat, :lon, :cnt, :city, :lang, :units, :APPID,
28-
:country]
28+
:country, :bbox]
2929

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

@@ -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

lib/open_weather/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module OpenWeather
2-
VERSION = '0.11.0'
2+
VERSION = '0.12.0'
33
end

spec/fixtures/cassettes/api/current_circle_zone_invalid.yml

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/api/current_circle_zone_valid.yml

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/api/current_cities_invalid.yml

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/api/current_cities_valid.yml

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)