Class: WeatherReporter

Inherits:
Object
  • Object
show all
Includes:
Colors
Defined in:
lib/meteo/weather_reporter.rb

Constant Summary collapse

WIND_DIRECTIONS =
%W(N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW)

Instance Method Summary collapse

Methods included from Colors

#background, #clouds, #dashes, #data, #delimiter, #fog, #moon, #rain, #snow, #stop, #sun, #text, #thunderstorm

Instance Method Details

#current_period(sunset, sunrise) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/meteo/weather_reporter.rb', line 120

def current_period sunset, sunrise
  current_time = Time.now.to_i

  if current_time >= sunset or current_time <= sunrise
    'night'
  else
    'day'
  end
end

#current_weather_report(response, units) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/meteo/weather_reporter.rb', line 19

def current_weather_report response, units
  if response["message"]
    puts response["message"]
  else
    sunrise = response['sys']['sunrise']
    sunset = response['sys']['sunset']
    pressure = response['main']['pressure']
    city = response['name']
    temperature = response['main']['temp']
    humidity = response['main']['humidity']
    sky = response['weather'][0]['main']
    wind = response['wind']['speed']
    azimuth = response['wind']['deg']

    direction = WIND_DIRECTIONS[((azimuth + 11.25)/22.5 % 16).to_i]

    result = %W(
      #{background}#{text}#{get_icon(sky, current_period(sunset, sunrise))}#{dashes}#{text}
      #{city} #{delimiter}#{data}
      #{text}Temperature #{data}#{temperature}#{temperature_scale(units)}
      #{text}Humidity #{data}#{humidity}%
      #{text}Wind #{data}#{wind}#{speed_unit(units)} #{direction}
      #{text}Pressure #{data}#{pressure(pressure, units)}#{pressure_unit(units)}
      #{text}Sunrise #{data}#{Time.at(sunrise).strftime('%I:%M:%S%p')}
      #{text}Sunset #{data}#{Time.at(sunset).strftime('%I:%M:%S%p')}#{stop}
    )

    puts result.join(' ')
  end
end

#forecast_report(response, units, forecast) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/meteo/weather_reporter.rb', line 50

def forecast_report response, units, forecast
  city = response['city']['name']

  max_forecast = response['list'].size

  forecast = (forecast <= max_forecast) ? forecast : max_forecast

  puts "#{background}#{text}Forecast for #{city}:"

  (0..forecast-1).each do |index|
    day = response['list'][index]
    sky = day['weather'][0]['main']
    date = day['dt']
    max = day['temp']['max']
    min = day['temp']['min']
    morning = day['temp']['morn']
    evening = day['temp']['eve']
    night = day['temp']['night']
    humidity = day['humidity']
    pressure = day['pressure']

    wind = day['speed']
    azimuth = day['deg']

    direction = WIND_DIRECTIONS[((azimuth + 11.25)/22.5 % 16).to_i]

    result = %W(
      #{background}#{text}#{index+1}. #{get_icon(sky, 'none')}
      #{data}#{Time.at(date).strftime('%Y/%m/%d')}
      #{text}Temperature
      #{text}min: #{data}#{min}#{temperature_scale(units)}
      #{text}max: #{data}#{max}#{temperature_scale(units)}
      #{text}morning: #{data}#{morning}#{temperature_scale(units)}
      #{text}evening: #{data}#{evening}#{temperature_scale(units)}
      #{text}night: #{data}#{night}#{temperature_scale(units)}
      #{text}Humidity #{data}#{humidity}%
      #{text}Wind #{data}#{wind}#{speed_unit(units)} #{direction}
      #{text}Pressure #{data}#{pressure(pressure, units)}#{pressure_unit(units)}#{stop}
    )

    puts result.join(' ')
  end
end

#get_icon(sky, period) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/meteo/weather_reporter.rb', line 94

def get_icon(sky, period)
  name =
  case sky
    when 'Clear'
      (period == "night") ? moon : sun

    when 'Clouds'
      clouds

    when 'Rain'
      rain

    when 'Fog'
      fog

    when 'Snow'
      snow

    when 'Thunderstorm'
      thunderstorm

    else
      (period == "night") ? moon : sun
  end
end

#pressure(pressure, units) ⇒ Object



142
143
144
# File 'lib/meteo/weather_reporter.rb', line 142

def pressure pressure, units
  (units == 'metric') ? sprintf('%.0f', pressure) : sprintf('%.2f', pressure*0.0295)
end

#pressure_unit(units) ⇒ Object



138
139
140
# File 'lib/meteo/weather_reporter.rb', line 138

def pressure_unit units
  (units == 'metric') ? "hPa" : "inHg"
end

#report(response, units, forecast) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/meteo/weather_reporter.rb', line 11

def report(response, units, forecast)
  if forecast == 0
    current_weather_report response, units
  else
    forecast_report response, units, forecast
  end
end

#speed_unit(units) ⇒ Object



134
135
136
# File 'lib/meteo/weather_reporter.rb', line 134

def speed_unit units
  (units == 'metric') ? "m/s" : "mph"
end

#temperature_scale(units) ⇒ Object



130
131
132
# File 'lib/meteo/weather_reporter.rb', line 130

def temperature_scale units
  (units == 'metric') ? "°C" : "°F"
end