weatheruby 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 093f7d9f2737698be51c4686072424b28245aadc
4
- data.tar.gz: 814d49c28ade652024e010a4d6230c0e37b765ab
3
+ metadata.gz: 2364322995c8bd3925c33321a3744b70497a1613
4
+ data.tar.gz: 2bfd153a01c99e1e80fe929eadbb814b2ab754a5
5
5
  SHA512:
6
- metadata.gz: 61355f4a8b3691b51bf9b5a44ade57d1bd11b2c611fe0faae0db22ba521ff38c7815123684370a42cd3b8495ae8d65de5985935d5e6b56e21a09a64ad089eb90
7
- data.tar.gz: d10b898728d47b70f35b337c85bef4f9eaa236e45a88491289ce10e8b726bb58dd1d9c69b1d67e914064398dbd00085c886cea249e1a15d63311bfc8f3ebc303
6
+ metadata.gz: 2f9a50df8220dc9dcd04d3d3aee974f0ff47a0a118e12a6fba9b2bc04051d97155c4b49836e1bc05dfe4d033db7622b39073dfb697c9937ae0da8c9fc6f3c608
7
+ data.tar.gz: 765c5fc8b96b0bdc7a1fb21eb3db37a79867570b18fe8993a4ccda6fa7e76687280b51206c356878ebdf2d032eeea5f3ec369770692bf174e6a0bcb0d678d74c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
  ## Version 0
3
+ ### Version 0.3.0
4
+ * Proper error handling, with new initialization parameter verbose_errors.
5
+
3
6
  ### Version 0.2.0
4
7
  * New complex_forecast_10day and simple_forecast_20day.
5
8
  * New complex_forecast and simple_forecast methods. It's funny, complex_forecast uses the simpleforecast stuff from the API.
@@ -2,14 +2,12 @@ module Weather
2
2
  module Actions
3
3
  # Gets alert information for a location.
4
4
  # @param location [String] The place to get the alert data for.
5
- # @return [Hash/Nil] Nil if there are no alerts, or a hash of hashes
5
+ # @return [Hash/String] Nil if there are no alerts, or a hash of hashes
6
6
  # containing relevant data if not. Each array in the hash contains
7
7
  # information for a different alert.
8
8
  def alerts(location)
9
9
  response = get('alerts', location)
10
- if response['alerts'].nil?
11
- return nil
12
- else
10
+ if response['response']['error'].nil?
13
11
  ret = []
14
12
  count = 0
15
13
  response['alerts'].each do |a|
@@ -22,23 +20,38 @@ module Weather
22
20
  }
23
21
  count += 1
24
22
  end
25
- end
26
23
 
27
- ret
24
+ ret
25
+ else
26
+ if @verbose_errors
27
+ return response['response']['error']['description']
28
+ else
29
+ return response['response']['error']['type']
30
+ end
31
+ end
28
32
  end
29
33
 
30
34
  # Gets the current moon phase of the location.
31
35
  # @param location [String] The place to get the phase for.
32
- # @return [Hash] A hash of two integers for the moon phase information.
33
- # The age key in the hash contains the moon's age in days, and the
34
- # illumination key contains the percentage of how illuminated it is.
36
+ # @return [Hash/String] A hash of two integers for the moon phase
37
+ # information. The age key in the hash contains the moon's age in days,
38
+ # and the illumination key contains the percentage of how illuminated it
39
+ # is. Returns the error type if verbose_errors is off, else the desc.
35
40
  def moon_phase(location)
36
41
  response = get('astronomy', location)
37
- ret = {}
38
- ret[:age] = response['moon_phase']['ageOfMoon'].to_i
39
- ret[:illumination] = response['moon_phase']['percentIlluminated'].to_i
42
+ if response['response']['error'].nil?
43
+ ret = {}
44
+ ret[:age] = response['moon_phase']['ageOfMoon'].to_i
45
+ ret[:illumination] = response['moon_phase']['percentIlluminated'].to_i
40
46
 
41
- ret
47
+ ret
48
+ else
49
+ if @verbose_errors
50
+ return response['response']['error']['description']
51
+ else
52
+ return response['response']['error']['type']
53
+ end
54
+ end
42
55
  end
43
56
 
44
57
  # Gets weather conditions for the location.
@@ -46,35 +59,43 @@ module Weather
46
59
  # @return [Hash] A hash containing strings of relevant weather information.
47
60
  def conditions(location)
48
61
  response = get('conditions', location)
49
- current_observation = response['current_observation']
50
- display_location = current_observation['display_location']
51
-
52
- ret = {
53
- :full_name => display_location['full'],
54
- :city_name => display_location['city'],
55
- :state_abbreviation => display_location['state'],
56
- :state_name => display_location['state_name'],
57
- :country => display_location['country'],
58
- :zip_code => display_location['zip'].to_i,
59
- :updated => current_observation['observation_time'],
60
- :weather => current_observation['weather'],
61
- :formatted_temperature => current_observation['temperature_string'],
62
- :temperature_f => current_observation['temp_f'],
63
- :temperature_c => current_observation['temp_c'],
64
- :humidity => current_observation['relative_humidity'],
65
- :formatted_wind => current_observation['wind_string'],
66
- :wind_direction => current_observation['wind_dir'],
67
- :wind_degrees => current_observation['wind_degrees'],
68
- :wind_speed => current_observation['wind_mph'],
69
- :wind_gust_speed => current_observation['wind_gust_mph'].to_i,
70
- :formatted_feelslike => current_observation['feelslike_string'],
71
- :feelslike_f => current_observation['feelslike_f'].to_i,
72
- :feelslike_c => current_observation['feelslike_c'].to_i
73
- }
74
-
75
- ret[:humidity] = ret[:humidity].sub('%', '').to_i
76
-
77
- ret
62
+ if response['response']['error'].nil?
63
+ current_observation = response['current_observation']
64
+ display_location = current_observation['display_location']
65
+
66
+ ret = {
67
+ :full_name => display_location['full'],
68
+ :city_name => display_location['city'],
69
+ :state_abbreviation => display_location['state'],
70
+ :state_name => display_location['state_name'],
71
+ :country => display_location['country'],
72
+ :zip_code => display_location['zip'].to_i,
73
+ :updated => current_observation['observation_time'],
74
+ :weather => current_observation['weather'],
75
+ :formatted_temperature => current_observation['temperature_string'],
76
+ :temperature_f => current_observation['temp_f'],
77
+ :temperature_c => current_observation['temp_c'],
78
+ :humidity => current_observation['relative_humidity'],
79
+ :formatted_wind => current_observation['wind_string'],
80
+ :wind_direction => current_observation['wind_dir'],
81
+ :wind_degrees => current_observation['wind_degrees'],
82
+ :wind_speed => current_observation['wind_mph'],
83
+ :wind_gust_speed => current_observation['wind_gust_mph'].to_i,
84
+ :formatted_feelslike => current_observation['feelslike_string'],
85
+ :feelslike_f => current_observation['feelslike_f'].to_i,
86
+ :feelslike_c => current_observation['feelslike_c'].to_i
87
+ }
88
+
89
+ ret[:humidity] = ret[:humidity].sub('%', '').to_i
90
+
91
+ ret
92
+ else
93
+ if @verbose_errors
94
+ return response['response']['error']['description']
95
+ else
96
+ return response['response']['error']['type']
97
+ end
98
+ end
78
99
  end
79
100
 
80
101
  # Gets the record low for the location.
@@ -83,15 +104,23 @@ module Weather
83
104
  def record_low(location)
84
105
  response = get('almanac', location)
85
106
 
86
- ret = {
87
- :average_low_f => response['almanac']['temp_low']['normal']['F'].to_i,
88
- :average_low_c => response['almanac']['temp_low']['normal']['C'].to_i,
89
- :record_year => response['almanac']['temp_low']['recordyear'].to_i,
90
- :record_low_f => response['almanac']['temp_low']['record']['F'].to_i,
91
- :record_low_c => response['almanac']['temp_low']['record']['C'].to_i
92
- }
107
+ if response['response']['error'].nil?
108
+ ret = {
109
+ :average_low_f => response['almanac']['temp_low']['normal']['F'].to_i,
110
+ :average_low_c => response['almanac']['temp_low']['normal']['C'].to_i,
111
+ :record_year => response['almanac']['temp_low']['recordyear'].to_i,
112
+ :record_low_f => response['almanac']['temp_low']['record']['F'].to_i,
113
+ :record_low_c => response['almanac']['temp_low']['record']['C'].to_i
114
+ }
93
115
 
94
- ret
116
+ ret
117
+ else
118
+ if @verbose_errors
119
+ return response['response']['error']['description']
120
+ else
121
+ return response['response']['error']['type']
122
+ end
123
+ end
95
124
  end
96
125
 
97
126
  # Gets the record high for the location.
@@ -100,15 +129,23 @@ module Weather
100
129
  def record_high(location)
101
130
  response = get('almanac', location)
102
131
 
103
- ret = {
104
- :average_high_f => response['almanac']['temp_high']['normal']['F'].to_i,
105
- :average_high_c => response['almanac']['temp_high']['normal']['C'].to_i,
106
- :record_year => response['almanac']['temp_high']['recordyear'].to_i,
107
- :record_high_f => response['almanac']['temp_high']['record']['F'].to_i,
108
- :record_high_c => response['almanac']['temp_high']['record']['C'].to_i
109
- }
132
+ if response['response']['error'].nil?
133
+ ret = {
134
+ :average_high_f => response['almanac']['temp_high']['normal']['F'].to_i,
135
+ :average_high_c => response['almanac']['temp_high']['normal']['C'].to_i,
136
+ :record_year => response['almanac']['temp_high']['recordyear'].to_i,
137
+ :record_high_f => response['almanac']['temp_high']['record']['F'].to_i,
138
+ :record_high_c => response['almanac']['temp_high']['record']['C'].to_i
139
+ }
110
140
 
111
- ret
141
+ ret
142
+ else
143
+ if @verbose_errors
144
+ return response['response']['error']['description']
145
+ else
146
+ return response['response']['error']['type']
147
+ end
148
+ end
112
149
  end
113
150
 
114
151
  # Gets data for currently-happening hurricanes around the world.
@@ -117,23 +154,31 @@ module Weather
117
154
  def hurricane_data
118
155
  response = get('currenthurricane', 'view')
119
156
 
120
- ret = {}
121
- response['currenthurricane'].each do |h|
122
- ret[h['stormInfo']['stormName_Nice']] = {
123
- :name => h['stormInfo']['stormName'],
124
- :number => h['stormInfo']['stormNumber'],
125
- :category => h['Current']['Category'],
126
- :time => h['Current']['Time']['pretty'],
127
- :wind_speed_mph => h['Current']['WindSpeed']['Mph'],
128
- :wind_speed_kts => h['Current']['WindSpeed']['Kts'],
129
- :wind_speed_kph => h['Current']['WindSpeed']['Kph'],
130
- :gust_speed_mph => h['Current']['WindGust']['Mph'],
131
- :gust_speed_kts => h['Current']['WindGust']['Kts'],
132
- :gust_speed_kph => h['Current']['WindGust']['Kph'],
133
- }
134
- end
157
+ if response['response']['error'].nil?
158
+ ret = {}
159
+ response['currenthurricane'].each do |h|
160
+ ret[h['stormInfo']['stormName_Nice']] = {
161
+ :name => h['stormInfo']['stormName'],
162
+ :number => h['stormInfo']['stormNumber'],
163
+ :category => h['Current']['Category'],
164
+ :time => h['Current']['Time']['pretty'],
165
+ :wind_speed_mph => h['Current']['WindSpeed']['Mph'],
166
+ :wind_speed_kts => h['Current']['WindSpeed']['Kts'],
167
+ :wind_speed_kph => h['Current']['WindSpeed']['Kph'],
168
+ :gust_speed_mph => h['Current']['WindGust']['Mph'],
169
+ :gust_speed_kts => h['Current']['WindGust']['Kts'],
170
+ :gust_speed_kph => h['Current']['WindGust']['Kph'],
171
+ }
172
+ end
135
173
 
136
- ret
174
+ ret
175
+ else
176
+ if @verbose_errors
177
+ return response['response']['error']['description']
178
+ else
179
+ return response['response']['error']['type']
180
+ end
181
+ end
137
182
  end
138
183
 
139
184
  # Gets the basic forecast information for the location.
@@ -181,60 +226,76 @@ module Weather
181
226
  private
182
227
  # Parses the simple forecast information.
183
228
  def parse_simple_forecast(response)
184
- ret = {}
229
+ if response['response']['error'].nil?
230
+ ret = {}
185
231
 
186
- response['forecast']['txt_forecast']['forecastday'].each do |f|
187
- ret[f['period']] = {
188
- :weekday_name => f['title'],
189
- :text => f['fcttext'],
190
- :text_metric => f['fcttext_metric']
191
- }
192
- end
232
+ response['forecast']['txt_forecast']['forecastday'].each do |f|
233
+ ret[f['period']] = {
234
+ :weekday_name => f['title'],
235
+ :text => f['fcttext'],
236
+ :text_metric => f['fcttext_metric']
237
+ }
238
+ end
193
239
 
194
- ret
240
+ ret
241
+ else
242
+ if @verbose_errors
243
+ return response['response']['error']['description']
244
+ else
245
+ return response['response']['error']['type']
246
+ end
247
+ end
195
248
  end
196
249
 
197
250
  # Parses the complex forecast information.
198
251
  def parse_complex_forecast(response)
199
- ret = {}
200
-
201
- response['forecast']['simpleforecast']['forecastday'].each do |f|
202
- ret[f['period'] - 1] = {
203
- :high_f => f['high']['fahrenheit'].to_i,
204
- :high_c => f['high']['celsius'].to_i,
205
- :low_f => f['low']['fahrenheit'].to_i,
206
- :low_c => f['low']['celsius'].to_i,
207
- :conditions => f['conditions'].to_i,
208
- :snow => {
209
- :snow_total_in => f['snow_allday']['in'],
210
- :snow_total_cm => f['snow_allday']['cm'],
211
- :snow_night_in => f['snow_night']['in'],
212
- :snow_night_cm => f['snow_night']['cm'],
213
- :snow_day_in => f['snow_day']['in'],
214
- :snow_day_cm => f['snow_day']['cm']
215
- },
216
- :quantative_precipitation => {
217
- :qpf_total_in => f['qpf_allday']['in'],
218
- :qpf_total_cm => f['qpf_allday']['cm'],
219
- :qpf_night_in => f['qpf_night']['in'],
220
- :qpf_night_cm => f['qpf_night']['cm'],
221
- :qpf_day_in => f['qpf_day']['in'],
222
- :qpf_day_cm => f['qpf_day']['cm']
223
- },
224
- :wind => {
225
- :average_mph => f['avewind']['mph'],
226
- :average_kph => f['avewind']['kph'],
227
- :average_dir => f['avewind']['dir'],
228
- :average_temp => f['avewind']['degrees'],
229
- :max_mph => f['maxwind']['mph'],
230
- :max_kph => f['maxwind']['kph'],
231
- :max_dir => f['maxwind']['dir'],
232
- :max_temp => f['maxwind']['degrees']
252
+ if response['response']['error'].nil?
253
+ ret = {}
254
+
255
+ response['forecast']['simpleforecast']['forecastday'].each do |f|
256
+ ret[f['period'] - 1] = {
257
+ :high_f => f['high']['fahrenheit'].to_i,
258
+ :high_c => f['high']['celsius'].to_i,
259
+ :low_f => f['low']['fahrenheit'].to_i,
260
+ :low_c => f['low']['celsius'].to_i,
261
+ :conditions => f['conditions'].to_i,
262
+ :snow => {
263
+ :snow_total_in => f['snow_allday']['in'],
264
+ :snow_total_cm => f['snow_allday']['cm'],
265
+ :snow_night_in => f['snow_night']['in'],
266
+ :snow_night_cm => f['snow_night']['cm'],
267
+ :snow_day_in => f['snow_day']['in'],
268
+ :snow_day_cm => f['snow_day']['cm']
269
+ },
270
+ :quantative_precipitation => {
271
+ :qpf_total_in => f['qpf_allday']['in'],
272
+ :qpf_total_cm => f['qpf_allday']['cm'],
273
+ :qpf_night_in => f['qpf_night']['in'],
274
+ :qpf_night_cm => f['qpf_night']['cm'],
275
+ :qpf_day_in => f['qpf_day']['in'],
276
+ :qpf_day_cm => f['qpf_day']['cm']
277
+ },
278
+ :wind => {
279
+ :average_mph => f['avewind']['mph'],
280
+ :average_kph => f['avewind']['kph'],
281
+ :average_dir => f['avewind']['dir'],
282
+ :average_temp => f['avewind']['degrees'],
283
+ :max_mph => f['maxwind']['mph'],
284
+ :max_kph => f['maxwind']['kph'],
285
+ :max_dir => f['maxwind']['dir'],
286
+ :max_temp => f['maxwind']['degrees']
287
+ }
233
288
  }
234
- }
235
- end
289
+ end
236
290
 
237
- ret
291
+ ret
292
+ else
293
+ if @verbose_errors
294
+ return response['response']['error']['description']
295
+ else
296
+ return response['response']['error']['type']
297
+ end
298
+ end
238
299
  end
239
300
  end
240
301
  end
data/lib/weatheruby.rb CHANGED
@@ -12,11 +12,14 @@ class Weatheruby
12
12
  # @param use_pws [Boolean] Whether to use the Personal Weather Station
13
13
  # feature.
14
14
  # @param use_bestfct [Boolean] Whether to use BestForecast.
15
- def initialize(api_key, language = 'EN', use_pws = true, use_bestfct = true)
15
+ # @param verbose_errors [Boolean] Whether to get verbose errors or not.
16
+ def initialize(api_key, language = 'EN', use_pws = true, use_bestfct = true,
17
+ verbose_errors = false)
16
18
  @api_key = api_key
17
19
  @language_key = language.upcase
18
20
  @use_pws = use_pws ? 1 : 0
19
21
  @use_bestfct = use_bestfct ? 1 : 0
22
+ @verbose_errors = verbose_errors
20
23
 
21
24
  @client = HTTPClient.new
22
25
  end
@@ -38,6 +41,7 @@ class Weatheruby
38
41
 
39
42
  if autoparse
40
43
  return JSON.parse(res.body)
44
+ # puts JSON.parse(res.body)
41
45
  else
42
46
  return res
43
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weatheruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster