Class: MyWeatherForecast::Hourly

Inherits:
Object
  • Object
show all
Defined in:
lib/myweatherforecast.rb

Direct Known Subclasses

Daily

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(forecast, tlabel, i = 0) ⇒ Hourly

Returns a new instance of Hourly.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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

Instance Attribute Details

#todayObject (readonly)

Returns the value of attribute today.



63
64
65
# File 'lib/myweatherforecast.rb', line 63

def today
  @today
end

Instance Method Details

#afternoonObject



112
# File 'lib/myweatherforecast.rb', line 112

def afternoon()   period(12, 17)                end

#ahead(advance = 2) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/myweatherforecast.rb', line 93

def ahead(advance=2)
  
  current_hour = Time.at(@hourly_data[0]['time']).hour + advance
  
  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      
  
end

#at(raw_hour) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# 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

#early_hoursObject



113
# File 'lib/myweatherforecast.rb', line 113

def early_hours() period(0, 5)                  end

#emojiObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/myweatherforecast.rb', line 115

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

#eveningObject



134
# File 'lib/myweatherforecast.rb', line 134

def evening()     period(17, night_time.hour+1) end

#humidityObject



142
143
144
# File 'lib/myweatherforecast.rb', line 142

def humidity()
  @x.humidity
end

#iconObject



146
147
148
# File 'lib/myweatherforecast.rb', line 146

def icon()
  @x.icon
end

#morningObject



135
# File 'lib/myweatherforecast.rb', line 135

def morning()     period(6, 12)                 end

#nightObject



136
# File 'lib/myweatherforecast.rb', line 136

def night()       period(night_time.hour, 23)   end

#noonObject Also known as: midday



150
151
152
# File 'lib/myweatherforecast.rb', line 150

def noon()
  at_hour 12
end

#summaryObject



163
164
165
# File 'lib/myweatherforecast.rb', line 163

def summary()
  @x.summary
end

#sunriseObject



167
168
169
# File 'lib/myweatherforecast.rb', line 167

def sunrise()
  @day.sunrise
end

#sunsetObject Also known as: night_time



171
172
173
# File 'lib/myweatherforecast.rb', line 171

def sunset()
  @day.sunset
end

#temperatureObject Also known as: temp



177
178
179
# File 'lib/myweatherforecast.rb', line 177

def temperature
  "%s°" % @x.temperature.round
end

#timeObject



183
184
185
# File 'lib/myweatherforecast.rb', line 183

def time
  Time.at @x.time
end

#to_sObject



156
157
158
159
160
161
# File 'lib/myweatherforecast.rb', line 156

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

#visibilityObject



187
188
189
# File 'lib/myweatherforecast.rb', line 187

def visibility()
  @x.visibility
end

#windspeedObject



191
192
193
# File 'lib/myweatherforecast.rb', line 191

def windspeed()
  "%s%s" % [@x.windSpeed.round, @speed_label]
end