Skip to content

Commit 7ab7ace

Browse files
JesseEmondcoderhs
authored andcommitted
Raise error when exceeding city IDs limit of 20
1 parent e113073 commit 7ab7ace

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

lib/open_weather.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ module OpenWeather
99
autoload :VERSION, 'open_weather/version'
1010

1111
require 'open_weather/api.rb'
12+
require 'open_weather/exceptions.rb'
1213
end

lib/open_weather/api.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,20 @@ def geocode(lat, lon, options = {})
2020
end
2121

2222
module SeveralCitiesClassMethods
23+
# Max amount of city IDs that can be requested at once,
24+
# from http://openweathermap.org/current
25+
LOCATIONS_LIMIT = 20
26+
2327
# City Ids, an array of integer values. Eg, [2172797, 524901]
2428
# Usage: OpenWeather::Current.cities([2172797, 524901])
29+
#
30+
# Note that every ID in the array counts as an API call.
31+
# A LocationsLimitExceeded error is raised if the API limit is exceeded.
2532
def cities(ids, options = {})
33+
if ids.length > SeveralCitiesClassMethods::LOCATIONS_LIMIT
34+
raise LocationsLimitExceeded
35+
end
36+
2637
url = 'http://api.openweathermap.org/data/2.5/group'
2738
ids = encode_array ids
2839
new(options.merge(id: ids)).retrieve url

lib/open_weather/exceptions.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module OpenWeather
2+
3+
# OpenWeather imposes a limit on the amount of city IDs requested at once,
4+
# see http://openweathermap.org/current
5+
class LocationsLimitExceeded < StandardError
6+
def initialize(msg="Too many city IDs requested at once")
7+
super
8+
end
9+
end
10+
11+
end

spec/open_weather/api_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
end
6262
response['list'].count.should eq(0)
6363
end
64+
65+
it 'raises a LocationsLimitExceeded exception with too many city IDs' do
66+
expect {
67+
OpenWeather::Current.cities([0] * 1000)
68+
}.to raise_error OpenWeather::LocationsLimitExceeded
69+
end
6470
end
6571

6672
context '.rectangle_zone' do

0 commit comments

Comments
 (0)