class Weather

Constants

DEFAULT_BACKGROUND
DEFAULT_FONT_HEIGHT
DEFAULT_FOREGROUND

Attributes

city[RW]
feels_like[RW]
humidity[RW]
temp[RW]
temp_max[RW]
temp_min[RW]

Public Instance Methods

celsius_to_fahrenheit(celsius) click to toggle source
# File lib/glimmer-dsl-opal/samples/elaborate/weather.rb, line 146
def celsius_to_fahrenheit(celsius)
  return nil if celsius.nil?
  (celsius * 9 / 5 ) + 32
end
fetch_weather!() click to toggle source
# File lib/glimmer-dsl-opal/samples/elaborate/weather.rb, line 118
def fetch_weather!
  @weather_mutex.synchronize do
    self.weather_data = JSON.parse(Net::HTTP.get('api.openweathermap.org', "/data/2.5/weather?q=#{city}&appid=1d16d70a9aec3570b5cbd27e6b421330"))
  end
rescue => e
  Glimmer::Config.logger.error "Unable to fetch weather due to error: #{e.full_message}"
end
humidity_field() click to toggle source
# File lib/glimmer-dsl-opal/samples/elaborate/weather.rb, line 99
def humidity_field
  name_label('humidity')
  label {
    layout_data(:fill, :center, true, false)
    text <= [self, 'humidity', on_read: ->(h) { "#{h.to_f.round}%" }]
    font height: DEFAULT_FONT_HEIGHT
    foreground DEFAULT_FOREGROUND
  }
end
kelvin_to_celsius(kelvin) click to toggle source
# File lib/glimmer-dsl-opal/samples/elaborate/weather.rb, line 141
def kelvin_to_celsius(kelvin)
  return nil if kelvin.nil?
  kelvin - 273.15
end
kelvin_to_fahrenheit(kelvin) click to toggle source
# File lib/glimmer-dsl-opal/samples/elaborate/weather.rb, line 151
def kelvin_to_fahrenheit(kelvin)
  return nil if kelvin.nil?
  celsius_to_fahrenheit(kelvin_to_celsius(kelvin))
end
kelvin_to_temp_unit(kelvin, temp_unit) click to toggle source
# File lib/glimmer-dsl-opal/samples/elaborate/weather.rb, line 137
def kelvin_to_temp_unit(kelvin, temp_unit)
  temp_unit == '℃' ? kelvin_to_celsius(kelvin) : kelvin_to_fahrenheit(kelvin)
end
name_label(field_name) click to toggle source
# File lib/glimmer-dsl-opal/samples/elaborate/weather.rb, line 109
def name_label(field_name)
  label {
    layout_data :fill, :center, false, false
    text field_name.titlecase
    font height: DEFAULT_FONT_HEIGHT
    foreground DEFAULT_FOREGROUND
  }
end
temp_field(field_name, temp_unit) click to toggle source
# File lib/glimmer-dsl-opal/samples/elaborate/weather.rb, line 89
def temp_field(field_name, temp_unit)
  name_label(field_name)
  label {
    layout_data(:fill, :center, true, false)
    text <= [self, field_name, on_read: ->(t) { "#{kelvin_to_temp_unit(t, temp_unit).to_f.round}°" }]
    font height: DEFAULT_FONT_HEIGHT
    foreground DEFAULT_FOREGROUND
  }
end
weather_data=(data) click to toggle source
# File lib/glimmer-dsl-opal/samples/elaborate/weather.rb, line 126
def weather_data=(data)
  @weather_data = data
  main_data = data['main']
  # temps come back in Kelvin
  self.temp = main_data['temp']
  self.temp_min = main_data['temp_min']
  self.temp_max = main_data['temp_max']
  self.feels_like = main_data['feels_like']
  self.humidity = main_data['humidity']
end