Class: Barometer::Weather
- Inherits:
-
Object
- Object
- Barometer::Weather
- Defined in:
- lib/barometer/weather.rb
Overview
Weather
holds all the measurements taken and provdes methods to interact with the data
Instance Attribute Summary collapse
-
#end_at ⇒ Object
Returns the value of attribute end_at.
-
#measurements ⇒ Object
Returns the value of attribute measurements.
-
#start_at ⇒ Object
Returns the value of attribute start_at.
Instance Method Summary collapse
- #average(value_name, do_average = true, class_name = nil) ⇒ Object
- #current ⇒ Object
-
#current_average(value_name) ⇒ Object
this assumes calculating for current, and that “to_f” for a value will return the value needed value_name = the name of the value we are averaging if a measurement has weighting, it will respect that.
- #day?(time_string = nil) ⇒ Boolean
-
#default ⇒ Object
the default measurement is the first successful measurement.
- #dew_point(do_average = true) ⇒ Object
-
#for(query) ⇒ Object
measurement search this will search the default measurements forecasts looking for the matching date.
- #forecast ⇒ Object
- #heat_index(do_average = true) ⇒ Object
-
#humidity(do_average = true) ⇒ Object
average of all values.
-
#initialize ⇒ Weather
constructor
A new instance of Weather.
-
#metric? ⇒ Boolean
Quick access methods.
- #night?(time_string = nil) ⇒ Boolean
- #now ⇒ Object
- #pressure(do_average = true) ⇒ Object
-
#source(source) ⇒ Object
find the measurement for the given source, if it exists.
-
#sources ⇒ Object
list successful sources.
- #sunny?(time_string = nil) ⇒ Boolean
- #temperature(do_average = true) ⇒ Object
- #today ⇒ Object
- #tomorrow ⇒ Object
- #visibility(do_average = true) ⇒ Object
- #wet?(time_string = nil, threshold = 50) ⇒ Boolean
- #wind(do_average = true) ⇒ Object
- #wind_chill(do_average = true) ⇒ Object
-
#windy?(time_string = nil, threshold = 10) ⇒ Boolean
simple questions pass the question on to each successful measurement until we get an answer.
Constructor Details
#initialize ⇒ Weather
Returns a new instance of Weather.
13 |
# File 'lib/barometer/weather.rb', line 13 def initialize; @measurements = []; end |
Instance Attribute Details
#end_at ⇒ Object
Returns the value of attribute end_at.
11 12 13 |
# File 'lib/barometer/weather.rb', line 11 def end_at @end_at end |
#measurements ⇒ Object
Returns the value of attribute measurements.
10 11 12 |
# File 'lib/barometer/weather.rb', line 10 def measurements @measurements end |
#start_at ⇒ Object
Returns the value of attribute start_at.
11 12 13 |
# File 'lib/barometer/weather.rb', line 11 def start_at @start_at end |
Instance Method Details
#average(value_name, do_average = true, class_name = nil) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/barometer/weather.rb', line 101 def average(value_name, do_average=true, class_name=nil) if class_name if do_average avg = Data.const_get(class_name).new(self.metric?) avg << self.current_average(value_name) else avg = self.now.send(value_name) end else avg = (do_average ? self.current_average(value_name) : self.now.send(value_name)) end avg end |
#current ⇒ Object
43 |
# File 'lib/barometer/weather.rb', line 43 def current; (default = self.default) ? default.current : nil; end |
#current_average(value_name) ⇒ Object
this assumes calculating for current, and that “to_f” for a value will return the value needed value_name = the name of the value we are averaging if a measurement has weighting, it will respect that
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/barometer/weather.rb', line 83 def current_average(value_name) values = [] @measurements.each do |measurement| if measurement.weight && measurement.weight > 1 measurement.weight.times do values << measurement.current.send(value_name).to_f if measurement.success? && measurement.current.send(value_name) end else values << measurement.current.send(value_name).to_f if measurement.success? && measurement.current.send(value_name) end end values.compact! return nil unless values && values.size > 0 values.inject(0.0) { |sum,v| sum += v if v } / values.size end |
#day?(time_string = nil) ⇒ Boolean
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/barometer/weather.rb', line 173 def day?(time_string=nil) local_datetime = Data::LocalDateTime.parse(time_string) raise ArgumentError unless (local_datetime.nil? || local_datetime.is_a?(Data::LocalDateTime)) is_day = nil @measurements.each do |measurement| if measurement.success? is_day = measurement.day?(local_datetime) return is_day if !is_day.nil? end end is_day end |
#default ⇒ Object
the default measurement is the first successful measurement
17 18 19 20 |
# File 'lib/barometer/weather.rb', line 17 def default return nil unless self.sources self.source(self.sources.first) end |
#dew_point(do_average = true) ⇒ Object
121 |
# File 'lib/barometer/weather.rb', line 121 def dew_point(do_average=true); average("dew_point",do_average,"Temperature"); end |
#for(query) ⇒ Object
measurement search this will search the default measurements forecasts looking for the matching date
61 62 63 64 |
# File 'lib/barometer/weather.rb', line 61 def for(query) default = self.default default && default.forecast ? default.for(query) : nil end |
#forecast ⇒ Object
44 |
# File 'lib/barometer/weather.rb', line 44 def forecast; (default = self.default) ? default.forecast : nil; end |
#heat_index(do_average = true) ⇒ Object
122 |
# File 'lib/barometer/weather.rb', line 122 def heat_index(do_average=true); average("heat_index",do_average,"Temperature"); end |
#humidity(do_average = true) ⇒ Object
average of all values
117 |
# File 'lib/barometer/weather.rb', line 117 def humidity(do_average=true); average("humidity",do_average); end |
#metric? ⇒ Boolean
Quick access methods
42 |
# File 'lib/barometer/weather.rb', line 42 def metric?; self.default ? self.default.metric? : true; end |
#night?(time_string = nil) ⇒ Boolean
187 188 189 190 191 192 |
# File 'lib/barometer/weather.rb', line 187 def night?(time_string=nil) local_datetime = Data::LocalDateTime.parse(time_string) raise ArgumentError unless (local_datetime.nil? || local_datetime.is_a?(Data::LocalDateTime)) is_day = self.day?(local_datetime) is_day.nil? ? nil : !is_day end |
#now ⇒ Object
45 |
# File 'lib/barometer/weather.rb', line 45 def now; self.current; end |
#pressure(do_average = true) ⇒ Object
120 |
# File 'lib/barometer/weather.rb', line 120 def pressure(do_average=true); average("pressure",do_average,"Pressure"); end |
#source(source) ⇒ Object
find the measurement for the given source, if it exists
24 25 26 27 28 29 30 |
# File 'lib/barometer/weather.rb', line 24 def source(source) raise ArgumentError unless (source.is_a?(String) || source.is_a?(Symbol)) @measurements.each do |measurement| return measurement if measurement.source == source.to_sym end nil end |
#sources ⇒ Object
list successful sources
34 35 36 |
# File 'lib/barometer/weather.rb', line 34 def sources @measurements.collect {|m| m.source.to_sym if m.success?}.compact end |
#sunny?(time_string = nil) ⇒ Boolean
194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/barometer/weather.rb', line 194 def sunny?(time_string=nil) local_datetime = Data::LocalDateTime.parse(time_string) raise ArgumentError unless (local_datetime.nil? || local_datetime.is_a?(Data::LocalDateTime)) is_sunny = nil @measurements.each do |measurement| if measurement.success? return false if self.day?(local_datetime) == false is_sunny = measurement.sunny?(local_datetime) return is_sunny if !is_sunny.nil? end end is_sunny end |
#temperature(do_average = true) ⇒ Object
118 |
# File 'lib/barometer/weather.rb', line 118 def temperature(do_average=true); average("temperature",do_average,"Temperature"); end |
#today ⇒ Object
47 48 49 50 |
# File 'lib/barometer/weather.rb', line 47 def today default = self.default default && default.forecast ? default.forecast[0] : nil end |
#tomorrow ⇒ Object
52 53 54 55 |
# File 'lib/barometer/weather.rb', line 52 def tomorrow default = self.default default && default.forecast ? default.forecast[1] : nil end |
#visibility(do_average = true) ⇒ Object
124 |
# File 'lib/barometer/weather.rb', line 124 def visibility(do_average=true); average("visibility",do_average,"Distance"); end |
#wet?(time_string = nil, threshold = 50) ⇒ Boolean
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/barometer/weather.rb', line 158 def wet?(time_string=nil, threshold=50) local_datetime = Data::LocalDateTime.parse(time_string) raise ArgumentError unless (threshold.is_a?(Fixnum) || threshold.is_a?(Float)) raise ArgumentError unless (local_datetime.nil? || local_datetime.is_a?(Data::LocalDateTime)) is_wet = nil @measurements.each do |measurement| if measurement.success? is_wet = measurement.wet?(local_datetime, threshold) return is_wet if !is_wet.nil? end end is_wet end |
#wind(do_average = true) ⇒ Object
119 |
# File 'lib/barometer/weather.rb', line 119 def wind(do_average=true); average("wind",do_average,"Speed"); end |
#wind_chill(do_average = true) ⇒ Object
123 |
# File 'lib/barometer/weather.rb', line 123 def wind_chill(do_average=true); average("wind_chill",do_average,"Temperature"); end |
#windy?(time_string = nil, threshold = 10) ⇒ Boolean
simple questions pass the question on to each successful measurement until we get an answer
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/barometer/weather.rb', line 143 def windy?(time_string=nil, threshold=10) local_datetime = Data::LocalDateTime.parse(time_string) raise ArgumentError unless (threshold.is_a?(Fixnum) || threshold.is_a?(Float)) raise ArgumentError unless (local_datetime.nil? || local_datetime.is_a?(Data::LocalDateTime)) is_windy = nil @measurements.each do |measurement| if measurement.success? is_windy = measurement.windy?(local_datetime, threshold) return is_windy if !is_windy.nil? end end is_windy end |