weatheruby 0.6.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/lib/weather/actions.rb +115 -28
- data/lib/weather/planner.rb +48 -34
- data/lib/weatheruby.rb +26 -16
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 217f72114c4d922e058e95acda948b549ba8a91e
|
4
|
+
data.tar.gz: 8084acda49e9aad4bddb921d32b45c9e17cffe6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a40e220d7f4491383c53da7784f51b569ea491e0d4c35e402ca0073f63b4799e5a054d956a88edc1f49101e7c32552a9e999e160b703cf37fcbe1d79ecbb7f2
|
7
|
+
data.tar.gz: cfb1aeeac85b2334f7b35f5f0413f22d1a6c7d0682ffbeac8f88003f540a5a258da85475bb5805c5366dbdced6b1d71f5b1de2f423be3ec2e2fac7fb97d30951
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,20 @@
|
|
1
1
|
# Changelog
|
2
|
+
|
3
|
+
## Version 1
|
4
|
+
|
5
|
+
### Version 1.0.0
|
6
|
+
* New `#sun_info` method for sunset/sunrise times
|
7
|
+
* Add `:image_url` to simple forecast return hash
|
8
|
+
* Add `:date`, `:weekday_name`, and `:image_url` to complex forecast return data
|
9
|
+
* Improve documentation across the library. The recommended place for docs is now
|
10
|
+
[GitHub Pages](http://elifoster.github.io/weatheruby/) instead of RubyDocs.info
|
11
|
+
* `#get` is now private
|
12
|
+
* Remove `autoparse` parameter from `#get`
|
13
|
+
* Fix `#hurricane_data` by ignoring the `WeatherError` thrown by that API
|
14
|
+
* Return `Time` and `DateTime` objects instead of `String`s for timestamp values across the library.
|
15
|
+
|
2
16
|
## Version 0
|
17
|
+
|
3
18
|
### Version 0.6.1
|
4
19
|
* Update to StringUtility 3.0
|
5
20
|
|
data/lib/weather/actions.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
1
3
|
module Weather
|
2
4
|
module Actions
|
3
5
|
# Gets alert information for a location.
|
4
6
|
# @param location [String] The place to get the alert data for.
|
5
|
-
# @return [Hash
|
6
|
-
#
|
7
|
-
#
|
7
|
+
# @return [Array<Hash<Symbol, String>>] A list of alerts for the given location. The array will be empty if there
|
8
|
+
# are no alerts. Each value in the array is a hash containing symbol keys:
|
9
|
+
#
|
10
|
+
# * `:type` (`String`) — The 3 character identifier for the alert type (see Wunderground API docs)
|
11
|
+
# * `:description` (`String`) — The full name of the alert type
|
12
|
+
# * `:date` (`Time`) — The date that the alert begins to take effect, using the local timezone to this location.
|
13
|
+
# * `:expires` (`Time`) — The date that the alert is no longer in effect, using the local timezone to this location.
|
14
|
+
# * `:message` (`String`) — The full message for the alert (this is usually dozens of paragraphs long)
|
8
15
|
def alerts(location)
|
9
16
|
response = get('alerts', location)
|
10
17
|
ret = []
|
@@ -13,8 +20,8 @@ module Weather
|
|
13
20
|
ret[count] = {
|
14
21
|
type: a['type'],
|
15
22
|
description: a['description'],
|
16
|
-
date: a['date'],
|
17
|
-
expires: a['expires'],
|
23
|
+
date: Time.parse(a['date']),
|
24
|
+
expires: Time.parse(a['expires']),
|
18
25
|
message: a['message']
|
19
26
|
}
|
20
27
|
count += 1
|
@@ -25,9 +32,9 @@ module Weather
|
|
25
32
|
|
26
33
|
# Gets the current moon phase of the location.
|
27
34
|
# @param location [String] The place to get the phase for.
|
28
|
-
# @return [Hash
|
29
|
-
# information. The age key in the hash contains the moon's age in days,
|
30
|
-
# and the illumination key contains the percentage of how illuminated it
|
35
|
+
# @return [Hash<Symbol, Integer>] A hash of two integers for the moon phase
|
36
|
+
# information. The `:age` key in the hash contains the moon's age in days,
|
37
|
+
# and the `:illumination` key contains the percentage of how illuminated it
|
31
38
|
# is.
|
32
39
|
def moon_phase(location)
|
33
40
|
response = get('astronomy', location)
|
@@ -37,9 +44,54 @@ module Weather
|
|
37
44
|
}
|
38
45
|
end
|
39
46
|
|
47
|
+
# Gets sunrise and sunset information for the current day at the current location.
|
48
|
+
# @param location [String] The place to get the info for.
|
49
|
+
# @return [Hash<Symbol, Hash<Symbol, Integer>>] A hash containing two hashes at keys :rise and :set for sunrise
|
50
|
+
# and sunset information respectively. They each contain an :hour key and a :minute key which point to the hour
|
51
|
+
# and minute that the sun will rise or set.
|
52
|
+
def sun_info(location)
|
53
|
+
response = get('astronomy', location)
|
54
|
+
{
|
55
|
+
rise: {
|
56
|
+
hour: response['moon_phase']['sunrise']['hour'].to_i,
|
57
|
+
minute: response['moon_phase']['sunrise']['minute'].to_i
|
58
|
+
},
|
59
|
+
set: {
|
60
|
+
hour: response['moon_phase']['sunset']['hour'].to_i,
|
61
|
+
minute: response['moon_phase']['sunset']['minute'].to_i
|
62
|
+
}
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
40
66
|
# Gets weather conditions for the location.
|
41
67
|
# @param location [String] The place to get the weather report for.
|
42
|
-
# @return [Hash] A hash containing strings of relevant weather information.
|
68
|
+
# @return [Hash<Symbol, Object>] A hash containing strings of relevant weather information. It contains the
|
69
|
+
# following keys:
|
70
|
+
#
|
71
|
+
# * `:full_name` (`String`) — The full name of the location
|
72
|
+
# * `:city_name` (`String`) — The name of the city
|
73
|
+
# * `:state_abbreviation` (`String`) — The abbreviation for the state (or national equivalent)
|
74
|
+
# * `:state_name` (`String`) — The name of the state (or national equivalent)
|
75
|
+
# * `:country` (`String`) — The name of the country
|
76
|
+
# * `:zip_code` (`Integer`) — The zip code for this location
|
77
|
+
# * `:updated` (`String`) — A string describing the date for when this data was last updated.
|
78
|
+
# * `:weather` (`String`) — A brief description of the current weather conditions in this location (e.g., Partly
|
79
|
+
# Cloudy)
|
80
|
+
# * `:formatted_temperature` (`String`) — The formatted temperature as provided by the API. It does not contain °
|
81
|
+
# symbols. Its format is "XX F (YY C)"
|
82
|
+
# * `:temperature_f` (`Float`) — The current temperature in fahrenheit
|
83
|
+
# * `:temperature_c` (`Float`) — The current temperature in celsius
|
84
|
+
# * `:humidity` (`Integer`) — The humidity percentage
|
85
|
+
# * `:formatted_wind` (`String`) — A brief description of the current wind conditions (e.g., Calm)
|
86
|
+
# * `:wind_direction` (`String`) — The direction (East, West, etc.) that the wind is blowing
|
87
|
+
# * `:wind_degrees` (`Integer`) — The angle of the wind
|
88
|
+
# * `:wind_speed` (`Float`) — The speed of the wind in miles per hour
|
89
|
+
# * `:wind_gust_speed` (`Integer`) — The speed of the gusts of wind in miles per hour
|
90
|
+
# * `:formatted_feelslike` (`String`) — The formatted string for the "feels like" temperature data as provided
|
91
|
+
# by the API. See :formatted_temperature for the format.
|
92
|
+
# * `:feelslike_f` (`Integer`) — The temperature that it feels like (not always the same as the temperature it
|
93
|
+
# is) in fahrenheit
|
94
|
+
# * `:feelslike_c` (`Integer`) — Like feelslike_f but in celsius
|
43
95
|
def conditions(location)
|
44
96
|
response = get('conditions', location)
|
45
97
|
current_observation = response['current_observation']
|
@@ -75,7 +127,13 @@ module Weather
|
|
75
127
|
|
76
128
|
# Gets the record low for the location.
|
77
129
|
# @param location [String] The place to get the record low for.
|
78
|
-
# @return [Hash] A hash containing a few integers of data
|
130
|
+
# @return [Hash<Symbol, Integer>] A hash containing a few integers of data:
|
131
|
+
#
|
132
|
+
# * `:average_low_f` (`Integer`) — The average low temperature in this location in fahrenheit
|
133
|
+
# * `:average_low_c` (`Integer`) — The average low temperature in this location in celsius
|
134
|
+
# * `:record_year` (`Integer`) — The year in which this location had its lowest temperatures
|
135
|
+
# * `:record_low_f` (`Integer`) — The lowest temperature this location has had in fahrenheit
|
136
|
+
# * `:record_low_c` (`Integer`) — The lowest temperature this location has had in celsius
|
79
137
|
def record_low(location)
|
80
138
|
response = get('almanac', location)
|
81
139
|
{
|
@@ -89,7 +147,13 @@ module Weather
|
|
89
147
|
|
90
148
|
# Gets the record high for the location.
|
91
149
|
# @param location [String] The place to get the record high for.
|
92
|
-
# @return [Hash] A hash containing a few integers of data
|
150
|
+
# @return [Hash<Symbol, Integer>] A hash containing a few integers of data:
|
151
|
+
#
|
152
|
+
# * `:average_high_f` (`Integer`) — The average high temperature in this location in fahrenheit
|
153
|
+
# * `:average_high_c` (`Integer`) — The average high temperature in this location in celsius
|
154
|
+
# * `:record_year` (`Integer`) — The year in which this location had its highest temperatures
|
155
|
+
# * `:record_high_f` (`Integer`) — The highest temperature this location has had in fahrenheit
|
156
|
+
# * `:record_high_c` (`Integer`) — The highest temperature this location has had in celsius
|
93
157
|
def record_high(location)
|
94
158
|
response = get('almanac', location)
|
95
159
|
{
|
@@ -101,11 +165,30 @@ module Weather
|
|
101
165
|
}
|
102
166
|
end
|
103
167
|
|
104
|
-
# Gets data for currently-happening
|
105
|
-
# @return [Hash] A hash containing hashes of data. Each sub-hash is named
|
106
|
-
# as the
|
168
|
+
# Gets data for currently-happening storms around the world.
|
169
|
+
# @return [Hash<String, Hash<Symbol, String/Integer>>] A hash containing hashes of data. Each sub-hash is named
|
170
|
+
# as the name for the storm including the type (example: Hurricane Daniel). The sub-hash values are as follows:
|
171
|
+
#
|
172
|
+
# * `:name` (`String`) — The name of the hurricane (example: Daniel)
|
173
|
+
# * `:number` (`String`) — The ID of the storm, 8 characters with a 2 letter basin ID.
|
174
|
+
# * `:category` (`String`) — The type of storm according to the Saffir-Simpson scale.
|
175
|
+
# * `:time` (`Time`) — The time the storm is recorded to start or have started using the local timezone for this
|
176
|
+
# location.
|
177
|
+
# * `:wind_speed_mph` (`Integer`) — The speed of the wind in this storm in miles per hour.
|
178
|
+
# * `:wind_speed_kts` (`Integer`) — The speed of the wind in this storm in knots.
|
179
|
+
# * `:wind_speed_kph` (`Integer`) — The speed of the wind in this storm in kilometers per hour.
|
180
|
+
# * `:gust_speed_mph` (`Integer`) — The speed of the gusts of wind in this storm in miles per hour.
|
181
|
+
# * `:gust_speed_kts` (`Integer`) — The speed of the gusts of wind in this storm in knots.
|
182
|
+
# * `:gust_speed_kph` (`Integer`) — The speed of the gusts of wind in this storm in kilometers per hour.
|
107
183
|
def hurricane_data
|
108
|
-
|
184
|
+
begin
|
185
|
+
response = get('currenthurricane', 'view')
|
186
|
+
rescue Weatheruby::WeatherError => e
|
187
|
+
# For some reason the API always errors with this when getting current hurricane data.
|
188
|
+
fail e unless e.message.start_with?('querynotfound')
|
189
|
+
response = e.full_response
|
190
|
+
end
|
191
|
+
p response
|
109
192
|
|
110
193
|
ret = {}
|
111
194
|
response['currenthurricane'].each do |h|
|
@@ -113,7 +196,7 @@ module Weather
|
|
113
196
|
name: h['stormInfo']['stormName'],
|
114
197
|
number: h['stormInfo']['stormNumber'],
|
115
198
|
category: h['Current']['Category'],
|
116
|
-
time: h['Current']['Time']['pretty'],
|
199
|
+
time: Time.parse(h['Current']['Time']['pretty']),
|
117
200
|
wind_speed_mph: h['Current']['WindSpeed']['Mph'],
|
118
201
|
wind_speed_kts: h['Current']['WindSpeed']['Kts'],
|
119
202
|
wind_speed_kph: h['Current']['WindSpeed']['Kph'],
|
@@ -139,30 +222,29 @@ module Weather
|
|
139
222
|
parse_simple_forecast(response)
|
140
223
|
end
|
141
224
|
|
142
|
-
# Gets more complicated forecast information for the location. Only gets
|
143
|
-
# the forecast for the next three days.
|
225
|
+
# Gets more complicated forecast information for the location. Only gets the forecast for the next three days.
|
144
226
|
# @param location [String] The place to get the forecast for.
|
145
|
-
# @return [Hash] A hash containing hashes of information. Sub-hashes are
|
146
|
-
#
|
147
|
-
#
|
148
|
-
# by weekday. Unlike simple_forecast, you do not get very many strings in
|
149
|
-
# this method.
|
227
|
+
# @return [Hash] A hash containing hashes of information. Sub-hashes are named as their "period", or the day in
|
228
|
+
# relation to the current day. For example: 0 is today, 1 is tomorrow, etc. It does not organize itself by
|
229
|
+
# weekday. Unlike simple_forecast, you do not get very many strings in this method.
|
150
230
|
def complex_forecast(location)
|
151
231
|
response = get('forecast', location)
|
152
232
|
|
153
233
|
parse_complex_forecast(response)
|
154
234
|
end
|
155
235
|
|
156
|
-
# Exactly the same as #simple_forecast, except that it gets the data for
|
157
|
-
#
|
236
|
+
# Exactly the same as {#simple_forecast}, except that it gets the data for 10 days.
|
237
|
+
# @param (see #simple_forecast)
|
238
|
+
# @return (see #simple_forecast)
|
158
239
|
def simple_forecast_10day(location)
|
159
240
|
response = get('forecast10day', location)
|
160
241
|
|
161
242
|
parse_simple_forecast(response)
|
162
243
|
end
|
163
244
|
|
164
|
-
# Exactly the same as #complex_forecast, except that it gets the data for
|
165
|
-
#
|
245
|
+
# Exactly the same as {#complex_forecast}, except that it gets the data for 10 days.
|
246
|
+
# @param (see #complex_forecast)
|
247
|
+
# @return (see #complex_forecast)
|
166
248
|
def complex_forecast_10day(location)
|
167
249
|
response = get('forecast10day', location)
|
168
250
|
|
@@ -179,7 +261,8 @@ module Weather
|
|
179
261
|
ret[f['period']] = {
|
180
262
|
weekday_name: f['title'],
|
181
263
|
text: f['fcttext'],
|
182
|
-
text_metric: f['fcttext_metric']
|
264
|
+
text_metric: f['fcttext_metric'],
|
265
|
+
image_url: f['icon_url']
|
183
266
|
}
|
184
267
|
end
|
185
268
|
|
@@ -191,12 +274,16 @@ module Weather
|
|
191
274
|
ret = {}
|
192
275
|
|
193
276
|
response['forecast']['simpleforecast']['forecastday'].each do |f|
|
277
|
+
date = f['date']
|
194
278
|
ret[f['period'] - 1] = {
|
279
|
+
date: DateTime.new(date['year'], date['month'], date['day'], date['hour'], date['min'].to_i, date['sec'], date['tz_short']),
|
280
|
+
weekday_name: date['weekday'],
|
195
281
|
high_f: f['high']['fahrenheit'].to_i,
|
196
282
|
high_c: f['high']['celsius'].to_i,
|
197
283
|
low_f: f['low']['fahrenheit'].to_i,
|
198
284
|
low_c: f['low']['celsius'].to_i,
|
199
285
|
conditions: f['conditions'].to_i,
|
286
|
+
image_url: f['icon_url'],
|
200
287
|
snow: {
|
201
288
|
snow_total_in: f['snow_allday']['in'],
|
202
289
|
snow_total_cm: f['snow_allday']['cm'],
|
data/lib/weather/planner.rb
CHANGED
@@ -1,122 +1,140 @@
|
|
1
1
|
module Weather
|
2
2
|
module Planner
|
3
3
|
# Gets the chance of snow within the date range.
|
4
|
-
# @see #get_planner_response
|
4
|
+
# @param (see #get_planner_response)
|
5
|
+
# @return (see #get_chance_of)
|
5
6
|
def chance_of_snow(start_date, end_date, location)
|
6
7
|
get_chance_of('chanceofsnowday', start_date, end_date, location)
|
7
8
|
end
|
8
9
|
|
9
10
|
# Gets the chance of hail within the date range.
|
10
|
-
# @see #get_planner_response
|
11
|
+
# @param (see #get_planner_response)
|
12
|
+
# @return (see #get_chance_of)
|
11
13
|
def chance_of_hail(start_date, end_date, location)
|
12
14
|
get_chance_of('chanceofhailday', start_date, end_date, location)
|
13
15
|
end
|
14
16
|
|
15
17
|
# Gets the chance of temperatures above 0 C/32 F within the date range.
|
16
|
-
# @see #get_planner_response
|
18
|
+
# @param (see #get_planner_response)
|
19
|
+
# @return (see #get_chance_of)
|
17
20
|
def chance_of_not_freezing(start_date, end_date, location)
|
18
21
|
get_chance_of('tempoverfreezing', start_date, end_date, location)
|
19
22
|
end
|
20
23
|
|
21
24
|
# Gets the chance of temperatures below 0 C/32 F within the date range.
|
22
|
-
# @see #get_planner_response
|
25
|
+
# @param (see #get_planner_response)
|
26
|
+
# @return (see #get_chance_of)
|
23
27
|
def chance_of_freezing(start_date, end_date, location)
|
24
28
|
get_chance_of('tempbelowfreezing', start_date, end_date, location)
|
25
29
|
end
|
26
30
|
|
27
31
|
# Gets chance of sultry within the date range.
|
28
|
-
# @see #get_planner_response
|
32
|
+
# @param (see #get_planner_response)
|
33
|
+
# @return (see #get_chance_of)
|
29
34
|
def chance_of_sultry(start_date, end_date, location)
|
30
35
|
get_chance_of('chanceofsultryday', start_date, end_date, location)
|
31
36
|
end
|
32
37
|
|
33
38
|
# Gets the chance of a tornado within the date range.
|
34
|
-
# @see #get_planner_response
|
39
|
+
# @param (see #get_planner_response)
|
40
|
+
# @return (see #get_chance_of)
|
35
41
|
def chance_of_tornado(start_date, end_date, location)
|
36
42
|
get_chance_of('chanceoftornadoday', start_date, end_date, location)
|
37
43
|
end
|
38
44
|
|
39
45
|
# Gets the chance of snow on the ground within the date range.
|
40
|
-
# @see #get_planner_response
|
46
|
+
# @param (see #get_planner_response)
|
47
|
+
# @return (see #get_chance_of)
|
41
48
|
def chance_of_groundsnow(start_date, end_date, location)
|
42
49
|
get_chance_of('chanceofsnowonground', start_date, end_date, location)
|
43
50
|
end
|
44
51
|
|
45
52
|
# Gets the chance of thunderstorms within the date range.
|
46
|
-
# @see #get_planner_response
|
53
|
+
# @param (see #get_planner_response)
|
54
|
+
# @return (see #get_chance_of)
|
47
55
|
def chance_of_thunderstorms(start_date, end_date, location)
|
48
56
|
get_chance_of('chanceofthunderday', start_date, end_date, location)
|
49
57
|
end
|
50
58
|
|
51
59
|
# Gets the chance of temperatures above 32.2 C/90 F within the date range.
|
52
|
-
# @see #get_planner_response
|
60
|
+
# @param (see #get_planner_response)
|
61
|
+
# @return (see #get_chance_of)
|
53
62
|
def chance_of_heat(start_date, end_date, location)
|
54
63
|
get_chance_of('tempoverninety', start_date, end_date, location)
|
55
64
|
end
|
56
65
|
|
57
66
|
# Gets the chance of rain within the date range.
|
58
|
-
# @see #get_planner_response
|
67
|
+
# @param (see #get_planner_response)
|
68
|
+
# @return (see #get_chance_of)
|
59
69
|
def chance_of_rain(start_date, end_date, location)
|
60
70
|
get_chance_of('chanceofrainday', start_date, end_date, location)
|
61
71
|
end
|
62
72
|
|
63
73
|
# Gets the chance of precipitation within the date range.
|
64
|
-
# @see #get_planner_response
|
74
|
+
# @param (see #get_planner_response)
|
75
|
+
# @return (see #get_chance_of)
|
65
76
|
def chance_of_precipitation(start_date, end_date, location)
|
66
77
|
get_chance_of('chanceofprecip', start_date, end_date, location)
|
67
78
|
end
|
68
79
|
|
69
80
|
# Gets the chance of humidity within the date range.
|
70
|
-
# @see #get_planner_response
|
81
|
+
# @param (see #get_planner_response)
|
82
|
+
# @return (see #get_chance_of)
|
71
83
|
def chance_of_humid(start_date, end_date, location)
|
72
84
|
get_chance_of('chanceofhumidday', start_date, end_date, location)
|
73
85
|
end
|
74
86
|
|
75
87
|
# Gets the chance of fog within the date range.
|
76
|
-
# @see #get_planner_response
|
88
|
+
# @param (see #get_planner_response)
|
89
|
+
# @return (see #get_chance_of)
|
77
90
|
def chance_of_fog(start_date, end_date, location)
|
78
91
|
get_chance_of('chanceoffogday', start_date, end_date, location)
|
79
92
|
end
|
80
93
|
|
81
94
|
# Gets the chance of cloudy conditions within the date range.
|
82
|
-
# @see #get_planner_response
|
95
|
+
# @param (see #get_planner_response)
|
96
|
+
# @return (see #get_chance_of)
|
83
97
|
def chance_of_cloudy(start_date, end_date, location)
|
84
98
|
get_chance_of('chanceofcloudyday', start_date, end_date, location)
|
85
99
|
end
|
86
100
|
|
87
101
|
# Gets the chance of sunshine within the date range.
|
88
|
-
# @see #get_planner_response
|
102
|
+
# @param (see #get_planner_response)
|
103
|
+
# @return (see #get_chance_of)
|
89
104
|
def chance_of_sunny(start_date, end_date, location)
|
90
105
|
get_chance_of('chanceofsunnycloudyday', start_date, end_date, location)
|
91
106
|
end
|
92
107
|
|
93
108
|
# Gets the chance of partially cloudy conditions within the date range.
|
94
|
-
# @see #get_planner_response
|
109
|
+
# @param (see #get_planner_response)
|
110
|
+
# @return (see #get_chance_of)
|
95
111
|
def chance_of_partlycloudy(start_date, end_date, location)
|
96
112
|
get_chance_of('chanceofpartlycloudyday', start_date, end_date, location)
|
97
113
|
end
|
98
114
|
|
99
115
|
# Gets the chance of high winds within the date range.
|
100
|
-
# @see #get_planner_response
|
116
|
+
# @param (see #get_planner_response)
|
117
|
+
# @return (see #get_chance_of)
|
101
118
|
def chance_of_high_wind(start_date, end_date, location)
|
102
119
|
get_chance_of('chanceofwindyday', start_date, end_date, location)
|
103
120
|
end
|
104
121
|
|
105
122
|
# Gets the chance of a temperature of 15.5 C/60 F within the date range.
|
106
|
-
# @see #get_planner_response
|
123
|
+
# @param (see #get_planner_response)
|
124
|
+
# @return (see #get_chance_of)
|
107
125
|
def chance_of_warmth(start_date, end_date, location)
|
108
126
|
get_chance_of('tempoversixty', start_date, end_date, location)
|
109
127
|
end
|
110
128
|
|
111
129
|
# Gets the dewpoint highs and lows for the date range.
|
112
|
-
# @see #get_planner_response
|
113
|
-
# @return [Hash] Highs and lows minimum, average, and maximum for both
|
130
|
+
# @param (see #get_planner_response)
|
131
|
+
# @return [Hash<Symbol, Hash<Symbol, Hash<Symbol, Integer>>>] Highs and lows minimum, average, and maximum for both
|
114
132
|
# metric and imperial systems.
|
115
133
|
# @return [String] The error if possible.
|
134
|
+
# @todo Raise an error instead of returning a String.
|
116
135
|
def get_dewpoints(start_date, end_date, location)
|
117
136
|
response = get_planner_response(start_date, end_date, location)
|
118
|
-
return response['response']['error'] unless
|
119
|
-
response['response']['error'].nil?
|
137
|
+
return response['response']['error'] unless response['response']['error'].nil?
|
120
138
|
highs = response['trip']['dewpoint_high']
|
121
139
|
lows = response['trip']['dewpoint_low']
|
122
140
|
|
@@ -150,9 +168,10 @@ module Weather
|
|
150
168
|
|
151
169
|
# Gets the precipitation amounts (not chance) for the date range.
|
152
170
|
# @see #get_planner_response
|
153
|
-
# @return [Hash] Minimum, maximum, and average precipitation quantities for
|
171
|
+
# @return [Hash<Symbol, Hash<Symbol, Integer>>] Minimum, maximum, and average precipitation quantities for
|
154
172
|
# the location in both inches and centimeters.
|
155
173
|
# @return [String] The error if possible.
|
174
|
+
# @todo Raise an error instead of returning a String.
|
156
175
|
def get_precipitation(start_date, end_date, location)
|
157
176
|
response = get_planner_response(start_date, end_date, location)
|
158
177
|
return response['response']['error'] unless
|
@@ -179,7 +198,7 @@ module Weather
|
|
179
198
|
|
180
199
|
# Gets the highs and lows for the date range.
|
181
200
|
# @see #get_planner_response
|
182
|
-
# @return [Hash] Highs and lows minimum, average, and maximum for both
|
201
|
+
# @return [Hash<Symbol, Hash<Symbol, Hash<Symbol, Integer>>>] Highs and lows minimum, average, and maximum for both
|
183
202
|
# metric and imperial systems.
|
184
203
|
def get_temperatures(start_date, end_date, location)
|
185
204
|
response = get_planner_response(start_date, end_date, location)
|
@@ -217,25 +236,20 @@ module Weather
|
|
217
236
|
private
|
218
237
|
|
219
238
|
# Gets the full planner API response.
|
220
|
-
# @param start_date [DateTime] The date to start at. Only month and day
|
221
|
-
# actually matter.
|
222
|
-
# @param end_date [DateTime] The date to end at. Only month and day actually
|
223
|
-
# matter.
|
239
|
+
# @param start_date [DateTime] The date to start at. Only month and day actually matter.
|
240
|
+
# @param end_date [DateTime] The date to end at. Only month and day actually matter.
|
224
241
|
# @param location [String] The location to get the planner data for.
|
225
242
|
# @since 0.5.0
|
226
|
-
# @return
|
243
|
+
# @return (see Weatheruby#get)
|
227
244
|
def get_planner_response(start_date, end_date, location)
|
228
245
|
start = start_date.strftime('%m%d')
|
229
246
|
final = end_date.strftime('%m%d')
|
230
247
|
get("planner_#{start}#{final}", location)
|
231
248
|
end
|
232
249
|
|
233
|
-
# Gets the chance of any given string key in the chance_of hash returned by
|
234
|
-
# get_planner_response.
|
250
|
+
# Gets the chance of any given string key in the chance_of hash returned by {#get_planner_response}.
|
235
251
|
# @param subject [String] The chance_of hash's key.
|
236
|
-
# @param
|
237
|
-
# @param end_date [DateTime] (see {#get_planner_response})
|
238
|
-
# @param location [String] (see {#get_planner_response})
|
252
|
+
# @param (see #get_planner_response)
|
239
253
|
# @since 0.5.0
|
240
254
|
# @return [Integer] The chance of the subject happening.
|
241
255
|
def get_chance_of(subject, start_date, end_date, location)
|
data/lib/weatheruby.rb
CHANGED
@@ -3,8 +3,22 @@ require 'json'
|
|
3
3
|
require_relative 'weather/actions'
|
4
4
|
require_relative 'weather/planner'
|
5
5
|
|
6
|
+
# @todo Return proper objects instead of ugly hashes throughout the library
|
6
7
|
class Weatheruby
|
7
|
-
class WeatherError < StandardError
|
8
|
+
class WeatherError < StandardError
|
9
|
+
# @return [Hash] The full parsed HTTP response
|
10
|
+
attr_reader :full_response
|
11
|
+
|
12
|
+
def initialize(json, type, description)
|
13
|
+
@full_response = json
|
14
|
+
@type = type
|
15
|
+
@description = description
|
16
|
+
end
|
17
|
+
|
18
|
+
def message
|
19
|
+
"#{@type}: #{@description}"
|
20
|
+
end
|
21
|
+
end
|
8
22
|
|
9
23
|
include Weather::Actions
|
10
24
|
include Weather::Planner
|
@@ -25,29 +39,25 @@ class Weatheruby
|
|
25
39
|
@client = HTTPClient.new
|
26
40
|
end
|
27
41
|
|
42
|
+
private
|
43
|
+
|
28
44
|
# Performs a generic HTTP GET request. This method should generally not be used by a standard user, unless there is
|
29
45
|
# not a method for a particular action/feature.
|
30
46
|
# @param feature [String] The "feature" parameter defined by Wunderground.
|
31
47
|
# @param location [String] The location of the query.
|
32
|
-
# @
|
33
|
-
# @
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
"#{@language_key}/pws:#{@use_pws}/bestfct:#{@use_bestfct}/q/" \
|
38
|
-
"#{location}.json"
|
39
|
-
url = URI.encode(url)
|
40
|
-
uri = URI.parse(url)
|
41
|
-
res = @client.get(uri)
|
42
|
-
json = JSON.parse(res.body)
|
48
|
+
# @return [Hash] Parsed JSON response
|
49
|
+
# @raise [WeatherError] If anything goes wrong with the API, or it returns too many results for us to handle.
|
50
|
+
def get(feature, location)
|
51
|
+
url = "http://api.wunderground.com/api/#{@api_key}/#{feature}/lang:#{@language_key}/pws:#{@use_pws}/bestfct:#{@use_bestfct}/q/#{location}.json"
|
52
|
+
json = JSON.parse(@client.get(URI.parse(URI.encode(url))).body)
|
43
53
|
if json['response'].key?('error')
|
44
|
-
|
54
|
+
error = json['response']['error']
|
55
|
+
fail(WeatherError.new(json, error['type'], error['description'].capitalize))
|
45
56
|
end
|
46
57
|
if json['response'].key?('results')
|
47
|
-
fail(WeatherError, 'toomanyresults
|
58
|
+
fail(WeatherError.new(json, 'toomanyresults', 'Too many results were returned. Try narrowing your search.'))
|
48
59
|
end
|
49
60
|
|
50
|
-
|
51
|
-
autoparse ? json : res
|
61
|
+
json
|
52
62
|
end
|
53
63
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weatheruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eli Foster
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httpclient
|
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
86
|
version: '0'
|
87
87
|
requirements: []
|
88
88
|
rubyforge_project:
|
89
|
-
rubygems_version: 2.6.
|
89
|
+
rubygems_version: 2.6.10
|
90
90
|
signing_key:
|
91
91
|
specification_version: 4
|
92
92
|
summary: A Ruby gem for accessing the Weather Underground API.
|