Class: Barometer::Measurement
- Inherits:
-
Object
- Object
- Barometer::Measurement
- Defined in:
- lib/barometer/measurements/measurement.rb
Overview
Measurement a class that holds the response from a weather service
its main purpose is to hold all the data collected from a single weather service as it is passed to the weather object
this response includes
-
current weather data (using the CurrentMeasurement class)
-
forecasted weather data (an Array of instances of the ForecastMeasurement class)
-
time_zone information (for the location in question)
-
weather station information (for the station that gave collected the data)
Defined Under Namespace
Classes: Result, ResultArray
Instance Attribute Summary collapse
-
#current ⇒ Object
Returns the value of attribute current.
-
#end_at ⇒ Object
Returns the value of attribute end_at.
-
#forecast ⇒ Object
Returns the value of attribute forecast.
-
#format ⇒ Object
Returns the value of attribute format.
-
#links ⇒ Object
Returns the value of attribute links.
-
#location ⇒ Object
Returns the value of attribute location.
-
#measured_at ⇒ Object
Returns the value of attribute measured_at.
-
#metric ⇒ Object
Returns the value of attribute metric.
-
#query ⇒ Object
Returns the value of attribute query.
-
#source ⇒ Object
Returns the value of attribute source.
-
#start_at ⇒ Object
Returns the value of attribute start_at.
-
#station ⇒ Object
Returns the value of attribute station.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
-
#timezone ⇒ Object
Returns the value of attribute timezone.
-
#utc_time_stamp ⇒ Object
Returns the value of attribute utc_time_stamp.
-
#weight ⇒ Object
Returns the value of attribute weight.
Instance Method Summary collapse
-
#current?(local_time = nil) ⇒ Boolean
this will tell us if the measurement is still current …
- #day?(time_string = nil) ⇒ Boolean
-
#for(date = nil) ⇒ Object
Returns a forecast for a day given by a Date, DateTime, Time, or a string that can be parsed to a date.
- #imperial! ⇒ Object
-
#initialize(source = nil, metric = true) ⇒ Measurement
constructor
A new instance of Measurement.
- #metric! ⇒ Object
- #metric? ⇒ Boolean
- #now ⇒ Object
- #stamp! ⇒ Object
- #success! ⇒ Object
- #success? ⇒ Boolean
- #sunny?(time_string = nil) ⇒ Boolean
- #wet?(time_string = nil, pop_threshold = 50, humidity_threshold = 99) ⇒ Boolean
-
#windy?(time_string = nil, threshold = 10) ⇒ Boolean
simple questions.
Constructor Details
#initialize(source = nil, metric = true) ⇒ Measurement
Returns a new instance of Measurement.
25 26 27 28 29 30 31 |
# File 'lib/barometer/measurements/measurement.rb', line 25 def initialize(source=nil, metric=true) @source = source @metric = metric @success = false @weight = 1 @links = {} end |
Instance Attribute Details
#current ⇒ Object
Returns the value of attribute current.
19 20 21 |
# File 'lib/barometer/measurements/measurement.rb', line 19 def current @current end |
#end_at ⇒ Object
Returns the value of attribute end_at.
23 24 25 |
# File 'lib/barometer/measurements/measurement.rb', line 23 def end_at @end_at end |
#forecast ⇒ Object
Returns the value of attribute forecast.
19 20 21 |
# File 'lib/barometer/measurements/measurement.rb', line 19 def forecast @forecast end |
#format ⇒ Object
Returns the value of attribute format.
22 23 24 |
# File 'lib/barometer/measurements/measurement.rb', line 22 def format @format end |
#links ⇒ Object
Returns the value of attribute links.
20 21 22 |
# File 'lib/barometer/measurements/measurement.rb', line 20 def links @links end |
#location ⇒ Object
Returns the value of attribute location.
20 21 22 |
# File 'lib/barometer/measurements/measurement.rb', line 20 def location @location end |
#measured_at ⇒ Object
Returns the value of attribute measured_at.
18 19 20 |
# File 'lib/barometer/measurements/measurement.rb', line 18 def measured_at @measured_at end |
#metric ⇒ Object
Returns the value of attribute metric.
22 23 24 |
# File 'lib/barometer/measurements/measurement.rb', line 22 def metric @metric end |
#query ⇒ Object
Returns the value of attribute query.
22 23 24 |
# File 'lib/barometer/measurements/measurement.rb', line 22 def query @query end |
#source ⇒ Object
Returns the value of attribute source.
17 18 19 |
# File 'lib/barometer/measurements/measurement.rb', line 17 def source @source end |
#start_at ⇒ Object
Returns the value of attribute start_at.
23 24 25 |
# File 'lib/barometer/measurements/measurement.rb', line 23 def start_at @start_at end |
#station ⇒ Object
Returns the value of attribute station.
20 21 22 |
# File 'lib/barometer/measurements/measurement.rb', line 20 def station @station end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
21 22 23 |
# File 'lib/barometer/measurements/measurement.rb', line 21 def success @success end |
#timezone ⇒ Object
Returns the value of attribute timezone.
20 21 22 |
# File 'lib/barometer/measurements/measurement.rb', line 20 def timezone @timezone end |
#utc_time_stamp ⇒ Object
Returns the value of attribute utc_time_stamp.
18 19 20 |
# File 'lib/barometer/measurements/measurement.rb', line 18 def utc_time_stamp @utc_time_stamp end |
#weight ⇒ Object
Returns the value of attribute weight.
17 18 19 |
# File 'lib/barometer/measurements/measurement.rb', line 17 def weight @weight end |
Instance Method Details
#current?(local_time = nil) ⇒ Boolean
this will tell us if the measurement is still current … if it is still current this means that the CurrentMeasurement can still used as now
what it also means is that if you took a measurement right now (time = now) and then asked if current?(time_in_future) that current? would be true for any time_in_future within 4 hours of now
where is this useful? lets say you take the measurement now (time = now), and then you want to know if self.windy?(5_hours_in_future) … we could not use the current data for this answser as the time 5_hours_in_future is not current
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/barometer/measurements/measurement.rb', line 58 def current?(local_time=nil) current_at = ((self.current && self.current.current_at) ? self.current.current_at : self.measured_at) local_time = (local_time.nil? ? current_at : Data::LocalTime.parse(local_time)) return true unless local_time raise ArgumentError unless local_time.is_a?(Data::LocalTime) hours_still_current = 4 difference = (local_time.diff(current_at)).to_i.abs difference <= (60*60*hours_still_current).to_i end |
#day?(time_string = nil) ⇒ Boolean
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/barometer/measurements/measurement.rb', line 157 def day?(time_string=nil) time_string ||= (measured_at || now) local_time = Data::LocalTime.parse(time_string) if current?(local_time) return nil unless current current.day?(local_time) else return nil unless forecast && (future = forecast[local_time]) future.day?(local_time) end end |
#for(date = nil) ⇒ Object
Returns a forecast for a day given by a Date, DateTime, Time, or a string that can be parsed to a date
75 76 77 78 79 80 81 |
# File 'lib/barometer/measurements/measurement.rb', line 75 def for(date=nil) date = @timezone.today unless date || !@timezone date ||= Date.today return nil unless (@forecast && @forecast.size > 0) @forecast.for(date) end |
#imperial! ⇒ Object
42 |
# File 'lib/barometer/measurements/measurement.rb', line 42 def imperial!; @metric=false; end |
#metric! ⇒ Object
41 |
# File 'lib/barometer/measurements/measurement.rb', line 41 def metric!; @metric=true; end |
#metric? ⇒ Boolean
40 |
# File 'lib/barometer/measurements/measurement.rb', line 40 def metric?; @metric; end |
#now ⇒ Object
43 |
# File 'lib/barometer/measurements/measurement.rb', line 43 def now; timezone ? timezone.now : nil; end |
#stamp! ⇒ Object
38 |
# File 'lib/barometer/measurements/measurement.rb', line 38 def stamp!; @utc_time_stamp = Time.now.utc; end |
#success! ⇒ Object
33 34 35 36 |
# File 'lib/barometer/measurements/measurement.rb', line 33 def success! current && current.temperature && !current.temperature.c.nil? && @success = true end |
#success? ⇒ Boolean
39 |
# File 'lib/barometer/measurements/measurement.rb', line 39 def success?; @success; end |
#sunny?(time_string = nil) ⇒ Boolean
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/barometer/measurements/measurement.rb', line 170 def sunny?(time_string=nil) time_string ||= (measured_at || now) local_time = Data::LocalTime.parse(time_string) sunny_icons = Barometer::WeatherService.source(@source)._sunny_icon_codes is_day = day?(local_time) return is_day unless is_day if current?(local_time) return nil unless current current.sunny?(local_time, sunny_icons) else return nil unless forecast && (future = forecast[local_time]) future.sunny?(local_time, sunny_icons) end end |
#wet?(time_string = nil, pop_threshold = 50, humidity_threshold = 99) ⇒ Boolean
187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/barometer/measurements/measurement.rb', line 187 def wet?(time_string=nil, pop_threshold=50, humidity_threshold=99) time_string ||= (measured_at || now) local_time = Data::LocalTime.parse(time_string) wet_icons = Barometer::WeatherService.source(@source)._wet_icon_codes if current?(local_time) return nil unless current current.wet?(wet_icons, humidity_threshold) else return nil unless forecast && (future = forecast[local_time]) future.wet?(wet_icons, pop_threshold, humidity_threshold) end end |
#windy?(time_string = nil, threshold = 10) ⇒ Boolean
simple questions
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/barometer/measurements/measurement.rb', line 144 def windy?(time_string=nil, threshold=10) time_string ||= (measured_at || now) local_time = Data::LocalTime.parse(time_string) if current?(local_time) return nil unless current current.windy?(threshold) else return nil unless forecast && (future = forecast[local_time]) future.windy?(threshold) end end |