class MyWeatherForecast::Hourly
Attributes
today[R]
Public Class Methods
new(forecast, tlabel, i=0)
click to toggle source
# File lib/myweatherforecast.rb, line 65 def initialize(forecast, tlabel, i=0) @forecast, @tlabel, @i = forecast, tlabel, i @x, @hourly_data = if i > 0 then [forecast['hourly']['data'][i], forecast['hourly']['data'][i..-1]] else [forecast.currently, forecast['hourly']['data']] end #@speed_label = 'kph' @speed_label = 'mph' #if @forecast['flags']['units'][/^uk2$/] end
Public Instance Methods
afternoon()
click to toggle source
# File lib/myweatherforecast.rb, line 114 def afternoon() period(12, 17) end
ahead(advance=2)
click to toggle source
# File lib/myweatherforecast.rb, line 93 def ahead(advance=2) current_hour = Time.at(@hourly_data[0]['time']).hour + advance name = case current_hour when 12..17 :afternoon when 0..5 :early_hours when 17..(night_time.hour+1) :evening when 6..12 :morning when night_time.hour..23 :night end method(name).call end
at(raw_hour)
click to toggle source
# File lib/myweatherforecast.rb, line 80 def at(raw_hour) hour = Time.parse(raw_hour).hour i = 0 return if Time.at(@hourly_data[i]['time']).hour > hour i += 1 until Time.at(@hourly_data[i]['time']).hour == hour Hourly.new(@forecast, @tlabel, i+@i) end
detail()
click to toggle source
# File lib/myweatherforecast.rb, line 115 def detail() period(0, 23) end
early_hours()
click to toggle source
# File lib/myweatherforecast.rb, line 116 def early_hours() period(0, 5) end
emoji()
click to toggle source
# File lib/myweatherforecast.rb, line 118 def emoji() h = { 'clear-day' => :sun, 'clear-night' => :night_with_stars, 'rain' => :cloud_with_rain, 'snow' => :cloud_with_snow, 'sleet' => :cloud_with_rain, 'wind' => :leaf_fluttering_in_wind, 'fog' => :fog, 'cloudy' => :cloud, 'partly-cloudy-day' => :sun_behind_cloud, 'partly-cloudy-night' => :cloud } Emoji2020.new(h[icon()]).to_s end
evening()
click to toggle source
# File lib/myweatherforecast.rb, line 137 def evening() period(17, night_time.hour+1) end
humidity()
click to toggle source
# File lib/myweatherforecast.rb, line 145 def humidity() @x.humidity end
icon()
click to toggle source
# File lib/myweatherforecast.rb, line 149 def icon() @x.icon end
morning()
click to toggle source
# File lib/myweatherforecast.rb, line 138 def morning() period(6, 12) end
night()
click to toggle source
# File lib/myweatherforecast.rb, line 139 def night() period(night_time.hour, 23) end
night_time()
click to toggle source
# File lib/myweatherforecast.rb, line 141 def night_time() Time.at(@day.sunsetTime) end
noon()
click to toggle source
# File lib/myweatherforecast.rb, line 153 def noon() at_hour 12 end
Also aliased as: midday
summary()
click to toggle source
# File lib/myweatherforecast.rb, line 166 def summary() @x.summary end
sunrise()
click to toggle source
# File lib/myweatherforecast.rb, line 170 def sunrise() @day.sunrise end
sunset()
click to toggle source
# File lib/myweatherforecast.rb, line 174 def sunset() @day.sunset end
Also aliased as: night_time
temperature()
click to toggle source
# File lib/myweatherforecast.rb, line 180 def temperature "%s°" % @x.temperature.round end
Also aliased as: temp
time()
click to toggle source
# File lib/myweatherforecast.rb, line 186 def time Time.at @x.time end
to_s()
click to toggle source
# File lib/myweatherforecast.rb, line 159 def to_s r = "%s: %d%s, %s" % [self.time.strftime("%-I%P"), @x.temperature.round, \ @tlabel, @x.summary] r << ", %s" % [windspeed] if r[/windy|breezy/i] r end
visibility()
click to toggle source
# File lib/myweatherforecast.rb, line 190 def visibility() @x.visibility end
windspeed()
click to toggle source
# File lib/myweatherforecast.rb, line 194 def windspeed() "%s%s" % [@x.windSpeed.round, @speed_label] end
Private Instance Methods
at_hour(n)
click to toggle source
# File lib/myweatherforecast.rb, line 200 def at_hour(n) i = 0 return if Time.at(@hourly_data[i]['time']).hour > n len = @hourly_data.length i += 1 until Time.at(@hourly_data[i]['time']).hour == n or i >= len - 1 Hourly.new(@forecast, @tlabel, i+@i) end
period(hr1, hr2)
click to toggle source
# File lib/myweatherforecast.rb, line 211 def period(hr1, hr2) current_hour = Time.at(@hourly_data[0]['time']).hour return if current_hour >= hr2 hr1 = current_hour if current_hour > hr1 hr2 = @hourly_data.length - 2 if hr2 + 1 > @hourly_data.length - 1 (hr1..hr2).map {|n| at_hour n} end