Class: Weather
- Inherits:
-
Object
- Object
- Weather
- Includes:
- Glimmer::UI::CustomShell
- Defined in:
- lib/glimmer-dsl-opal/samples/elaborate/weather.rb
Constant Summary collapse
- DEFAULT_FONT_HEIGHT =
30
- DEFAULT_FOREGROUND =
:white
- DEFAULT_BACKGROUND =
rgb(135, 176, 235)
Instance Attribute Summary collapse
-
#city ⇒ Object
Returns the value of attribute city.
-
#feels_like ⇒ Object
Returns the value of attribute feels_like.
-
#humidity ⇒ Object
Returns the value of attribute humidity.
-
#temp ⇒ Object
Returns the value of attribute temp.
-
#temp_max ⇒ Object
Returns the value of attribute temp_max.
-
#temp_min ⇒ Object
Returns the value of attribute temp_min.
Attributes included from Glimmer::UI::CustomWidget
#body_root, #options, #parent, #swt_style
Instance Method Summary collapse
- #celsius_to_fahrenheit(celsius) ⇒ Object
- #fetch_weather! ⇒ Object
- #humidity_field ⇒ Object
- #kelvin_to_celsius(kelvin) ⇒ Object
- #kelvin_to_fahrenheit(kelvin) ⇒ Object
- #kelvin_to_temp_unit(kelvin, temp_unit) ⇒ Object
- #name_label(field_name) ⇒ Object
- #temp_field(field_name, temp_unit) ⇒ Object
- #weather_data=(data) ⇒ Object
Methods included from Glimmer::UI::CustomShell
#close, encoded_request_parameter_string, #hide, included, #initialize, #open, request_parameter_string, requested?, requested_and_not_handled?, #show, #start_event_loop, #visible?
Methods included from Glimmer::UI::CustomWidget
add_custom_widget_namespaces_for, #add_observer, #async_exec, #attribute_setter, #can_add_observer?, #can_handle_observation_request?, #content, custom_widget_namespaces, for, #get_attribute, #handle_observation_request, #has_attribute?, #has_instance_method?, #has_style?, included, #initialize, #local_respond_to?, #method_missing, namespaces_for_class, #post_initialize_child, reset_custom_widget_namespaces, #respond_to?, #set_attribute, #sync_exec
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Glimmer::UI::CustomWidget
Instance Attribute Details
#city ⇒ Object
Returns the value of attribute city.
33 34 35 |
# File 'lib/glimmer-dsl-opal/samples/elaborate/weather.rb', line 33 def city @city end |
#feels_like ⇒ Object
Returns the value of attribute feels_like.
33 34 35 |
# File 'lib/glimmer-dsl-opal/samples/elaborate/weather.rb', line 33 def feels_like @feels_like end |
#humidity ⇒ Object
Returns the value of attribute humidity.
33 34 35 |
# File 'lib/glimmer-dsl-opal/samples/elaborate/weather.rb', line 33 def humidity @humidity end |
#temp ⇒ Object
Returns the value of attribute temp.
33 34 35 |
# File 'lib/glimmer-dsl-opal/samples/elaborate/weather.rb', line 33 def temp @temp end |
#temp_max ⇒ Object
Returns the value of attribute temp_max.
33 34 35 |
# File 'lib/glimmer-dsl-opal/samples/elaborate/weather.rb', line 33 def temp_max @temp_max end |
#temp_min ⇒ Object
Returns the value of attribute temp_min.
33 34 35 |
# File 'lib/glimmer-dsl-opal/samples/elaborate/weather.rb', line 33 def temp_min @temp_min end |
Instance Method Details
#celsius_to_fahrenheit(celsius) ⇒ Object
146 147 148 149 |
# 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! ⇒ Object
118 119 120 121 122 123 124 |
# 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.}" end |
#humidity_field ⇒ Object
99 100 101 102 103 104 105 106 107 |
# 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) ⇒ Object
141 142 143 144 |
# 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) ⇒ Object
151 152 153 154 |
# 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) ⇒ Object
137 138 139 |
# 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) ⇒ Object
109 110 111 112 113 114 115 116 |
# 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) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# 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) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# 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 |