class WeatherReporter

Constants

WIND_DIRECTIONS

Public Instance Methods

current_period(sunset, sunrise) click to toggle source
# File lib/meteo/weather_reporter.rb, line 119
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) click to toggle source
# File lib/meteo/weather_reporter.rb, line 18
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) click to toggle source
# File lib/meteo/weather_reporter.rb, line 49
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) click to toggle source
# File lib/meteo/weather_reporter.rb, line 93
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) click to toggle source
# File lib/meteo/weather_reporter.rb, line 141
def pressure pressure, units
  (units == 'metric') ? sprintf('%.0f', pressure) : sprintf('%.2f', pressure*0.0295)
end
pressure_unit(units) click to toggle source
# File lib/meteo/weather_reporter.rb, line 137
def pressure_unit units
  (units == 'metric') ? "hPa" : "inHg"
end
report(response, units, forecast) click to toggle source
# File lib/meteo/weather_reporter.rb, line 10
def report(response, units, forecast)
  if forecast == 0
    current_weather_report response, units
  else
    forecast_report response, units, forecast
  end
end
speed_unit(units) click to toggle source
# File lib/meteo/weather_reporter.rb, line 133
def speed_unit units
  (units == 'metric') ? "m/s" : "mph"
end
temperature_scale(units) click to toggle source
# File lib/meteo/weather_reporter.rb, line 129
def temperature_scale units
  (units == 'metric') ? "°C" : "°F"
end