Class: Barometer::Measurement

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#currentObject

Returns the value of attribute current.



19
20
21
# File 'lib/barometer/measurements/measurement.rb', line 19

def current
  @current
end

#end_atObject

Returns the value of attribute end_at.



23
24
25
# File 'lib/barometer/measurements/measurement.rb', line 23

def end_at
  @end_at
end

#forecastObject

Returns the value of attribute forecast.



19
20
21
# File 'lib/barometer/measurements/measurement.rb', line 19

def forecast
  @forecast
end

#formatObject

Returns the value of attribute format.



22
23
24
# File 'lib/barometer/measurements/measurement.rb', line 22

def format
  @format
end

Returns the value of attribute links.



20
21
22
# File 'lib/barometer/measurements/measurement.rb', line 20

def links
  @links
end

#locationObject

Returns the value of attribute location.



20
21
22
# File 'lib/barometer/measurements/measurement.rb', line 20

def location
  @location
end

#measured_atObject

Returns the value of attribute measured_at.



18
19
20
# File 'lib/barometer/measurements/measurement.rb', line 18

def measured_at
  @measured_at
end

#metricObject

Returns the value of attribute metric.



22
23
24
# File 'lib/barometer/measurements/measurement.rb', line 22

def metric
  @metric
end

#queryObject

Returns the value of attribute query.



22
23
24
# File 'lib/barometer/measurements/measurement.rb', line 22

def query
  @query
end

#sourceObject

Returns the value of attribute source.



17
18
19
# File 'lib/barometer/measurements/measurement.rb', line 17

def source
  @source
end

#start_atObject

Returns the value of attribute start_at.



23
24
25
# File 'lib/barometer/measurements/measurement.rb', line 23

def start_at
  @start_at
end

#stationObject

Returns the value of attribute station.



20
21
22
# File 'lib/barometer/measurements/measurement.rb', line 20

def station
  @station
end

#successObject (readonly)

Returns the value of attribute success.



21
22
23
# File 'lib/barometer/measurements/measurement.rb', line 21

def success
  @success
end

#timezoneObject

Returns the value of attribute timezone.



20
21
22
# File 'lib/barometer/measurements/measurement.rb', line 20

def timezone
  @timezone
end

#utc_time_stampObject

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

#weightObject

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

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


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

Returns:

  • (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

Returns:

  • (Boolean)


40
# File 'lib/barometer/measurements/measurement.rb', line 40

def metric?; @metric; end

#nowObject



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

Returns:

  • (Boolean)


39
# File 'lib/barometer/measurements/measurement.rb', line 39

def success?; @success; end

#sunny?(time_string = nil) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


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