myweatherforecast 0.4.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 724647c8f6324b8f09781b112a58c1c2844fea17
4
- data.tar.gz: 446d0270e1c999c8688dc8c8e461ca8e84f9ec34
3
+ metadata.gz: 4828694b4f19f73d4b4d6de75c86ace00a9814cd
4
+ data.tar.gz: a189b59984efe9f649fafb7dc72ecc7348082cd2
5
5
  SHA512:
6
- metadata.gz: 17ddb50b0858836debd1e61b60e868fc078d000663b86e5481c7120a839b8b1bc594d094ef333c142c9de6880c9aa85fdb91b6884cc77e2deea640f6fbf99242
7
- data.tar.gz: 7671182c6e45fb87fd4f1adb17e0b4d093c09f1071ff14e1839cb90292ec814a98af44d492df6fc1132f6f35a54100dc782bbcfefb542769737072b5505b7983
6
+ metadata.gz: 0a83b05bd02a2bca202e0a53945218f7de5a3d217a1b5f0ee33a0b9a1e3f59dc2e6e67a2567e3048f95924c168d13e3ac87153bf569e059f87ec088269194aa7
7
+ data.tar.gz: 81400d8cd99433f65ecbae41a29956f05b05e3a6076251942977dac96e858f0698e76641b03220ccb243719a8966463f42ad466cbf921048845be19b2c8ecbeb
checksums.yaml.gz.sig CHANGED
Binary file
@@ -14,6 +14,8 @@ require 'open-uri'
14
14
  # SI explained: https://en.wikipedia.org/wiki/SI_derived_unit
15
15
 
16
16
  class MyWeatherForecast
17
+
18
+ attr_reader :coordinates
17
19
 
18
20
  def initialize(*location, api_key: nil, units: :auto, timeout: 3)
19
21
 
@@ -36,7 +38,7 @@ class MyWeatherForecast
36
38
  results.any?
37
39
  results[0].coordinates
38
40
 
39
- end
41
+ end
40
42
 
41
43
  ForecastIO.api_key = api_key
42
44
 
@@ -46,16 +48,23 @@ class MyWeatherForecast
46
48
  autounits = @forecast['flags']['units']
47
49
 
48
50
  @tlabel = autounits == 'us' ? '°F' : '°C'
51
+ @coordinates = [lat, lon]
49
52
 
50
53
  end
51
54
 
52
- class Day
55
+ class Hourly
56
+
57
+ attr_reader :today
53
58
 
54
- def initialize(forecast, tlabel, i=nil)
59
+ def initialize(forecast, tlabel, i=0)
55
60
 
56
- @forecast = forecast
57
- @x = i ? forecast['hourly']['data'][i] : forecast.currently
58
- @tlabel = tlabel
61
+ @forecast, @tlabel, @i = forecast, tlabel, i
62
+
63
+ @x, @hourly_data = if i > 0 then
64
+ [forecast['hourly']['data'][i], forecast['hourly']['data'][i..-1]]
65
+ else
66
+ [forecast.currently, forecast['hourly']['data']]
67
+ end
59
68
 
60
69
  end
61
70
 
@@ -63,11 +72,25 @@ class MyWeatherForecast
63
72
 
64
73
  hour = Time.parse(raw_hour).hour
65
74
  i = 0
66
- i += 1 until Time.at(@forecast['hourly']['data'][i]['time']).hour \
67
- == hour.to_i
68
- Day.new(@forecast, @tlabel, i)
75
+
76
+ return if Time.at(@hourly_data[i]['time']).hour > hour
77
+
78
+ i += 1 until Time.at(@hourly_data[i]['time']).hour == hour
79
+
80
+ Hourly.new(@forecast, @tlabel, i+@i)
81
+
69
82
  end
70
83
 
84
+ def afternoon() period(12, 17) end
85
+ def early_hours() period(0, 5) end
86
+ def evening() period(17, night_time.hour+1) end
87
+ def morning() period(6, 12) end
88
+ def night() period(night_time.hour, 23) end
89
+
90
+ def night_time()
91
+ Time.at(@day.sunsetTime)
92
+ end
93
+
71
94
  def humidity()
72
95
  @x.humidity
73
96
  end
@@ -75,19 +98,38 @@ class MyWeatherForecast
75
98
  def icon()
76
99
  @x.icon
77
100
  end
101
+
102
+ def noon()
103
+ at_hour 12
104
+ end
105
+
106
+ alias midday noon
78
107
 
79
108
  def to_s
80
- "%d%s, %s" % [@x.temperature.round, @tlabel, @x.summary]
109
+ "%s: %d%s, %s" % [self.time.strftime("%-I%P"), @x.temperature.round, \
110
+ @tlabel, @x.summary]
81
111
  end
82
112
 
83
113
  def summary()
84
114
  @x.summary
85
115
  end
86
116
 
117
+ def sunrise()
118
+ @day.sunrise
119
+ end
120
+
121
+ def sunset()
122
+ @day.sunset
123
+ end
124
+
125
+ alias night_time sunset
126
+
87
127
  def temperature
88
128
  "%s°" % @x.temperature.round
89
129
  end
90
130
 
131
+ alias temp temperature
132
+
91
133
  def time
92
134
  Time.at @x.time
93
135
  end
@@ -100,48 +142,72 @@ class MyWeatherForecast
100
142
  @x.windSpeed.round
101
143
  end
102
144
 
103
- end
104
-
105
- class DaysAhead < Day
145
+ private
106
146
 
107
- def initialize(forecast, tlabel, index: 0)
147
+ def at_hour(n)
148
+
149
+ i = 0
150
+
151
+ return if Time.at(@hourly_data[i]['time']).hour > n
152
+ len = @hourly_data.length
153
+ i += 1 until Time.at(@hourly_data[i]['time']).hour == n or i >= len - 1
154
+
155
+ Hourly.new(@forecast, @tlabel, i+@i)
156
+ end
157
+
158
+ def period(hr1, hr2)
108
159
 
109
- @forecast = forecast
110
- @x = forecast['hourly']['data'][index]
111
- @tlabel = tlabel
112
- @i = index
160
+ current_hour = Time.at(@hourly_data[0]['time']).hour
161
+
162
+ return if current_hour >= hr2
163
+
164
+ hr1 = current_hour if current_hour > hr1
165
+ hr2 = @hourly_data.length - 2 if hr2 + 1 > @hourly_data.length - 1
166
+ (hr1..hr2).map {|n| at_hour n}
113
167
 
114
168
  end
115
169
 
116
- def at(raw_hour)
117
-
118
- hour = Time.parse(raw_hour).hour
119
-
120
- i = @i - 12
121
- i += 1 until Time.at(@forecast['hourly']['data'][i]['time']).hour \
122
- == hour.to_i
123
- DaysAhead.new(@forecast, @tlabel, index: i)
124
- end
125
-
126
170
  end
171
+
127
172
 
128
- class OutlookDay < Day
173
+ class Daily < Hourly
129
174
 
130
- def initialize(day, tlabel)
131
- @x = day
175
+ def initialize(forecast, tlabel, d=0)
176
+
177
+ @forecast = forecast
178
+
179
+ @x = forecast['daily']['data'][d]
132
180
  @tlabel = tlabel
181
+
182
+ found = forecast['hourly']['data'].detect do |hour|
183
+ Time.at(@x.time).to_date == Time.at(hour.time).to_date
184
+ end
185
+
186
+ @i = forecast['hourly']['data'].index found
187
+
188
+ return if @i.nil?
189
+
190
+ @hourly_data = forecast['hourly']['data'][@i..-1]
191
+ @day = self
192
+
133
193
  end
134
194
 
135
- def at(*a)
136
- "%s outlook: %s - %s, %s" % [Date::DAYNAMES[self.time.wday], \
137
- tempmin, tempmax, @x.summary]
138
- end
139
-
140
195
  def to_s
141
- "%s: %s - %s, %s" % [Date::ABBR_DAYNAMES[self.time.wday], \
142
- tempmin, tempmax, @x.summary]
196
+
197
+ label = self.time.to_date == Time.now.to_date ? 'Today' : \
198
+ Date::ABBR_DAYNAMES[self.time.wday]
199
+
200
+ "%s: ▽%s ▲%s, %s" % [label, tempmin, tempmax, @x.summary]
201
+ end
202
+
203
+ def sunrise()
204
+ Time.at @x.sunriseTime
143
205
  end
144
206
 
207
+ def sunset()
208
+ Time.at @x.sunsetTime
209
+ end
210
+
145
211
  def temperature()
146
212
  end
147
213
 
@@ -155,7 +221,7 @@ class MyWeatherForecast
155
221
 
156
222
 
157
223
  end
158
-
224
+
159
225
 
160
226
  # e.g.
161
227
  # require 'myweatherforecast'
@@ -168,25 +234,31 @@ class MyWeatherForecast
168
234
  # Sun: 5° - 12°, Mostly cloudy throughout the day.
169
235
 
170
236
  def days()
171
- @forecast['daily']['data'].map {|day| OutlookDay.new(day, @tlabel) }
237
+ (@forecast['daily']['data'].length).times.map {|n| Daily.new(@forecast, @tlabel, n) }
238
+ end
239
+
240
+ def hours()
241
+
242
+ len = @forecast['hourly']['data'].length
243
+ len.times.map {|i| Hourly.new @forecast, @tlabel, i}
244
+
172
245
  end
173
246
 
174
247
  def today()
175
- Day.new(@forecast, @tlabel)
248
+ Daily.new(@forecast, @tlabel)
176
249
  end
177
250
 
178
- alias now today
179
-
180
- def tomorrow()
181
-
182
- # select tomorrow at midday
183
- i = 0
184
-
185
- i += 7 if Time.at(@forecast['hourly']['data'][i]['time']).hour >= 6
186
-
187
- i += 1 until Time.at(@forecast['hourly']['data'][i]['time']).hour == 12
188
- DaysAhead.new(@forecast, @tlabel, index: i)
251
+ def tonight()
252
+ Daily.new(@forecast, @tlabel).night
189
253
  end
254
+
255
+ alias currently today
256
+ alias now today
257
+ alias this today
258
+
259
+ def tomorrow()
260
+ Daily.new(@forecast, @tlabel, 1)
261
+ end
190
262
 
191
263
  def monday() day :monday end
192
264
  def tuesday() day :tuesday end
@@ -209,16 +281,11 @@ class MyWeatherForecast
209
281
  def day(name)
210
282
 
211
283
  name = (name.to_s + '?').to_sym
212
- len = @forecast['hourly']['data'].length
213
- i = 0
214
- i += 1 until Time.at(@forecast['hourly']['data'][i]['time']).\
215
- method(name).call or i >= len - 1
216
- return DaysAhead.new(@forecast, @tlabel, index: i) unless i == len - 1
217
284
 
218
285
  d = 0
219
286
  d += 1 until Time.at(@forecast['daily']['data'][d].time).method(name).call
220
287
  Time.at(@forecast['daily']['data'][d].time)
221
- OutlookDay.new(@forecast['daily']['data'][d], @tlabel)
288
+ Daily.new(@forecast, @tlabel, d)
222
289
  end
223
290
 
224
291
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myweatherforecast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file