Module: OpenWeather::Forecast
- Defined in:
- lib/OpenWeather/forecast.rb
Instance Method Summary collapse
- #forecast_raw(city) ⇒ Object
- #forecast_tomorrow_max(city) ⇒ Object
- #forecast_tomorrow_min(city) ⇒ Object
- #forecast_tomorrow_rain?(city) ⇒ Boolean
- #forecast_tomorrow_sunny?(city) ⇒ Boolean
Instance Method Details
#forecast_raw(city) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/OpenWeather/forecast.rb', line 3 def forecast_raw(city) city.is_number? ? query = {id: city, units: @units_format, mode: @data_format} : query = {q: city, units: @units_format, mode: @data_format} @city_file.nil? ? get('/forecast', query: query) : JSON.parse(File.read(@city_file)) end |
#forecast_tomorrow_max(city) ⇒ Object
18 19 20 21 22 |
# File 'lib/OpenWeather/forecast.rb', line 18 def forecast_tomorrow_max(city) forecast = forecast_raw(city) tomorrow_list = forecast['list'].select { |weather| weather['dt_txt'].split(" ")[0] == tomorrow } tomorrow_list.max_by { |temp| temp['main']['temp_max'] }['main']['temp_max'] end |
#forecast_tomorrow_min(city) ⇒ Object
12 13 14 15 16 |
# File 'lib/OpenWeather/forecast.rb', line 12 def forecast_tomorrow_min(city) forecast = forecast_raw(city) tomorrow_list = forecast['list'].select { |weather| weather['dt_txt'].split(" ")[0] == tomorrow } tomorrow_list.min_by { |temp| temp['main']['temp_min'] }['main']['temp_min'] end |
#forecast_tomorrow_rain?(city) ⇒ Boolean
24 25 26 27 28 29 30 |
# File 'lib/OpenWeather/forecast.rb', line 24 def forecast_tomorrow_rain?(city) forecast = forecast_raw(city) tomorrow_list = forecast['list'].select { |weather| weather['dt_txt'].split(" ")[0] == tomorrow } # This selects any period of time during the day where the condition code is between 500 and 599 which # corresponds to rainy condition. If the array is empty that means it won't rain so we return the opposite. ! tomorrow_list.select { |code| Integer(code['weather'][0]['id']).between?(500, 599) }.empty? end |
#forecast_tomorrow_sunny?(city) ⇒ Boolean
32 33 34 35 36 37 38 |
# File 'lib/OpenWeather/forecast.rb', line 32 def forecast_tomorrow_sunny?(city) forecast = forecast_raw(city) tomorrow_list = forecast['list'].select { |weather| weather['dt_txt'].split(" ")[0] == tomorrow } # This selects any period of time during the day where the condition code is between 500 and 599 which # corresponds to rainy condition. If the array is empty that means it won't rain so we return the opposite. ! tomorrow_list.select { |code| Integer(code['weather'][0]['id']).between?(800, 801) }.empty? end |