Module: Weather::Actions
- Included in:
- Weatheruby
- Defined in:
- lib/weather/actions.rb
Instance Method Summary collapse
-
#alerts(location) ⇒ Array<Hash<Symbol, String>>
Gets alert information for a location.
-
#complex_forecast(location) ⇒ Hash
Gets more complicated forecast information for the location.
-
#complex_forecast_10day(location) ⇒ Hash
Exactly the same as #complex_forecast, except that it gets the data for 10 days.
-
#conditions(location) ⇒ Hash<Symbol, Object>
Gets weather conditions for the location.
-
#hurricane_data ⇒ Hash<String, Hash<Symbol, String/Integer>>
Gets data for currently-happening storms around the world.
-
#moon_phase(location) ⇒ Hash<Symbol, Integer>
Gets the current moon phase of the location.
-
#parse_complex_forecast(response) ⇒ Object
private
Parses the complex forecast information.
-
#parse_simple_forecast(response) ⇒ Object
private
Parses the simple forecast information.
-
#record_high(location) ⇒ Hash<Symbol, Integer>
Gets the record high for the location.
-
#record_low(location) ⇒ Hash<Symbol, Integer>
Gets the record low for the location.
-
#simple_forecast(location) ⇒ Hash
Gets the basic forecast information for the location.
-
#simple_forecast_10day(location) ⇒ Hash
Exactly the same as #simple_forecast, except that it gets the data for 10 days.
-
#sun_info(location) ⇒ Hash<Symbol, Hash<Symbol, Integer>>
Gets sunrise and sunset information for the current day at the current location.
Instance Method Details
#alerts(location) ⇒ Array<Hash<Symbol, String>>
Gets alert information for a location.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/weather/actions.rb', line 15 def alerts(location) response = get('alerts', location) ret = [] count = 0 response['alerts'].each do |a| ret[count] = { type: a['type'], description: a['description'], date: Time.parse(a['date']), expires: Time.parse(a['expires']), message: a['message'] } count += 1 end ret end |
#complex_forecast(location) ⇒ Hash
Gets more complicated forecast information for the location. Only gets the forecast for the next three days.
230 231 232 233 234 |
# File 'lib/weather/actions.rb', line 230 def complex_forecast(location) response = get('forecast', location) parse_complex_forecast(response) end |
#complex_forecast_10day(location) ⇒ Hash
Exactly the same as #complex_forecast, except that it gets the data for 10 days.
248 249 250 251 252 |
# File 'lib/weather/actions.rb', line 248 def complex_forecast_10day(location) response = get('forecast10day', location) parse_complex_forecast(response) end |
#conditions(location) ⇒ Hash<Symbol, Object>
Gets weather conditions for the location.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/weather/actions.rb', line 95 def conditions(location) response = get('conditions', location) current_observation = response['current_observation'] display_location = current_observation['display_location'] ret = { full_name: display_location['full'], city_name: display_location['city'], state_abbreviation: display_location['state'], state_name: display_location['state_name'], country: display_location['country'], zip_code: display_location['zip'].to_i, updated: current_observation['observation_time'], weather: current_observation['weather'], formatted_temperature: current_observation['temperature_string'], temperature_f: current_observation['temp_f'], temperature_c: current_observation['temp_c'], humidity: current_observation['relative_humidity'], formatted_wind: current_observation['wind_string'], wind_direction: current_observation['wind_dir'], wind_degrees: current_observation['wind_degrees'], wind_speed: current_observation['wind_mph'], wind_gust_speed: current_observation['wind_gust_mph'].to_i, formatted_feelslike: current_observation['feelslike_string'], feelslike_f: current_observation['feelslike_f'].to_i, feelslike_c: current_observation['feelslike_c'].to_i } ret[:humidity] = ret[:humidity].sub('%', '').to_i ret end |
#hurricane_data ⇒ Hash<String, Hash<Symbol, String/Integer>>
Gets data for currently-happening storms around the world.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/weather/actions.rb', line 183 def hurricane_data begin response = get('currenthurricane', 'view') rescue Weatheruby::WeatherError => e # For some reason the API always errors with this when getting current hurricane data. fail e unless e..start_with?('querynotfound') response = e.full_response end p response ret = {} response['currenthurricane'].each do |h| ret[h['stormInfo']['stormName_Nice']] = { name: h['stormInfo']['stormName'], number: h['stormInfo']['stormNumber'], category: h['Current']['Category'], time: Time.parse(h['Current']['Time']['pretty']), wind_speed_mph: h['Current']['WindSpeed']['Mph'], wind_speed_kts: h['Current']['WindSpeed']['Kts'], wind_speed_kph: h['Current']['WindSpeed']['Kph'], gust_speed_mph: h['Current']['WindGust']['Mph'], gust_speed_kts: h['Current']['WindGust']['Kts'], gust_speed_kph: h['Current']['WindGust']['Kph'] } end ret end |
#moon_phase(location) ⇒ Hash<Symbol, Integer>
Gets the current moon phase of the location.
39 40 41 42 43 44 45 |
# File 'lib/weather/actions.rb', line 39 def moon_phase(location) response = get('astronomy', location) { age: response['moon_phase']['ageOfMoon'].to_i, illumination: response['moon_phase']['percentIlluminated'].to_i } end |
#parse_complex_forecast(response) ⇒ Object (private)
Parses the complex forecast information.
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/weather/actions.rb', line 273 def parse_complex_forecast(response) ret = {} response['forecast']['simpleforecast']['forecastday'].each do |f| date = f['date'] ret[f['period'] - 1] = { date: DateTime.new(date['year'], date['month'], date['day'], date['hour'], date['min'].to_i, date['sec'], date['tz_short']), weekday_name: date['weekday'], high_f: f['high']['fahrenheit'].to_i, high_c: f['high']['celsius'].to_i, low_f: f['low']['fahrenheit'].to_i, low_c: f['low']['celsius'].to_i, conditions: f['conditions'].to_i, image_url: f['icon_url'], snow: { snow_total_in: f['snow_allday']['in'], snow_total_cm: f['snow_allday']['cm'], snow_night_in: f['snow_night']['in'], snow_night_cm: f['snow_night']['cm'], snow_day_in: f['snow_day']['in'], snow_day_cm: f['snow_day']['cm'] }, quantative_precipitation: { qpf_total_in: f['qpf_allday']['in'], qpf_total_cm: f['qpf_allday']['cm'], qpf_night_in: f['qpf_night']['in'], qpf_night_cm: f['qpf_night']['cm'], qpf_day_in: f['qpf_day']['in'], qpf_day_cm: f['qpf_day']['cm'] }, wind: { average_mph: f['avewind']['mph'], average_kph: f['avewind']['kph'], average_dir: f['avewind']['dir'], average_temp: f['avewind']['degrees'], max_mph: f['maxwind']['mph'], max_kph: f['maxwind']['kph'], max_dir: f['maxwind']['dir'], max_temp: f['maxwind']['degrees'] } } end ret end |
#parse_simple_forecast(response) ⇒ Object (private)
Parses the simple forecast information.
257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/weather/actions.rb', line 257 def parse_simple_forecast(response) ret = {} response['forecast']['txt_forecast']['forecastday'].each do |f| ret[f['period']] = { weekday_name: f['title'], text: f['fcttext'], text_metric: f['fcttext_metric'], image_url: f['icon_url'] } end ret end |
#record_high(location) ⇒ Hash<Symbol, Integer>
Gets the record high for the location.
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/weather/actions.rb', line 157 def record_high(location) response = get('almanac', location) { average_high_f: response['almanac']['temp_high']['normal']['F'].to_i, average_high_c: response['almanac']['temp_high']['normal']['C'].to_i, record_year: response['almanac']['temp_high']['recordyear'].to_i, record_high_f: response['almanac']['temp_high']['record']['F'].to_i, record_high_c: response['almanac']['temp_high']['record']['C'].to_i } end |
#record_low(location) ⇒ Hash<Symbol, Integer>
Gets the record low for the location.
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/weather/actions.rb', line 137 def record_low(location) response = get('almanac', location) { average_low_f: response['almanac']['temp_low']['normal']['F'].to_i, average_low_c: response['almanac']['temp_low']['normal']['C'].to_i, record_year: response['almanac']['temp_low']['recordyear'].to_i, record_low_f: response['almanac']['temp_low']['record']['F'].to_i, record_low_c: response['almanac']['temp_low']['record']['C'].to_i } end |
#simple_forecast(location) ⇒ Hash
Gets the basic forecast information for the location. Only gets data for the next 3 days.
219 220 221 222 223 |
# File 'lib/weather/actions.rb', line 219 def simple_forecast(location) response = get('forecast', location) parse_simple_forecast(response) end |
#simple_forecast_10day(location) ⇒ Hash
Exactly the same as #simple_forecast, except that it gets the data for 10 days.
239 240 241 242 243 |
# File 'lib/weather/actions.rb', line 239 def simple_forecast_10day(location) response = get('forecast10day', location) parse_simple_forecast(response) end |
#sun_info(location) ⇒ Hash<Symbol, Hash<Symbol, Integer>>
Gets sunrise and sunset information for the current day at the current location.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/weather/actions.rb', line 52 def sun_info(location) response = get('astronomy', location) { rise: { hour: response['moon_phase']['sunrise']['hour'].to_i, minute: response['moon_phase']['sunrise']['minute'].to_i }, set: { hour: response['moon_phase']['sunset']['hour'].to_i, minute: response['moon_phase']['sunset']['minute'].to_i } } end |