-
Notifications
You must be signed in to change notification settings - Fork 6
/
weather.rb
173 lines (145 loc) · 4.8 KB
/
weather.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
49
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
93
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Copyright (c) 2007-2024 Andy Maleh
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'glimmer-dsl-swt'
require 'net/http'
require 'json'
require 'facets/string/titlecase'
class Weather
include Glimmer::UI::CustomShell
DEFAULT_FONT_HEIGHT = 30
DEFAULT_FOREGROUND = :white
DEFAULT_BACKGROUND = rgb(135, 176, 235)
attr_accessor :city, :temp, :temp_min, :temp_max, :feels_like, :humidity
before_body do
@weather_mutex = Mutex.new
self.city = 'Montreal, QC, CA'
fetch_weather!
end
after_body do
Thread.new do
loop do
sleep(10)
break if body_root.disposed?
fetch_weather!
end
end
end
body {
shell(:no_resize) {
grid_layout
text 'Glimmer Weather'
minimum_size 400, (OS.linux? ? 330 : 300)
background DEFAULT_BACKGROUND
text {
layout_data(:center, :center, true, true)
text <=> [self, :city]
on_key_pressed do |event|
if event.keyCode == swt(:cr) # carriage return
Thread.new do
fetch_weather!
end
end
end
}
tab_folder {
layout_data(:center, :center, true, true) {
width_hint OS.linux? ? 300 : 270
}
['℃', '℉'].each do |temp_unit|
tab_item {
grid_layout 2, false
text temp_unit
background DEFAULT_BACKGROUND
rectangle(0, 0, [:default, -2], [:default, -2], 15, 15) {
foreground DEFAULT_FOREGROUND
}
%w[temp temp_min temp_max feels_like].each do |field_name|
temp_field(field_name, temp_unit)
end
humidity_field
}
end
}
}
}
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
background DEFAULT_BACKGROUND
foreground DEFAULT_FOREGROUND
}
end
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
background DEFAULT_BACKGROUND
foreground DEFAULT_FOREGROUND
}
end
def name_label(field_name)
label {
layout_data :fill, :center, false, false
text field_name.titlecase
font height: DEFAULT_FONT_HEIGHT
background DEFAULT_BACKGROUND
foreground DEFAULT_FOREGROUND
}
end
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
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
def kelvin_to_temp_unit(kelvin, temp_unit)
temp_unit == '℃' ? kelvin_to_celsius(kelvin) : kelvin_to_fahrenheit(kelvin)
end
def kelvin_to_celsius(kelvin)
return nil if kelvin.nil?
kelvin - 273.15
end
def celsius_to_fahrenheit(celsius)
return nil if celsius.nil?
(celsius * 9 / 5 ) + 32
end
def kelvin_to_fahrenheit(kelvin)
return nil if kelvin.nil?
celsius_to_fahrenheit(kelvin_to_celsius(kelvin))
end
end
Weather.launch