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



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

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
111
112
# 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) ⇒ 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

#detailObject



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

def detail()      period(0, 23)                 end

#early_hoursObject



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

def early_hours() period(0, 5)                  end

#emojiObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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

#eveningObject



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

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

#humidityObject



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

def humidity()
  @x.humidity
end

#iconObject



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

def icon()
  @x.icon
end

#morningObject



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

def morning()     period(6, 12)                 end

#nightObject



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

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

#noonObject Also known as: midday



153
154
155
# File 'lib/myweatherforecast.rb', line 153

def noon()
  at_hour 12
end

#summaryObject



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

def summary()
  @x.summary
end

#sunriseObject



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

def sunrise()
  @day.sunrise
end

#sunsetObject Also known as: night_time



174
175
176
# File 'lib/myweatherforecast.rb', line 174

def sunset()
  @day.sunset
end

#temperatureObject Also known as: temp



180
181
182
# File 'lib/myweatherforecast.rb', line 180

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

#timeObject



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

def time
  Time.at @x.time
end

#to_sObject



159
160
161
162
163
164
# 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

#visibilityObject



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

def visibility()
  @x.visibility
end

#windspeedObject



194
195
196
# File 'lib/myweatherforecast.rb', line 194

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