Class: Barometer::WeatherService
- Inherits:
-
Object
- Object
- Barometer::WeatherService
- Includes:
- HTTParty
- Defined in:
- lib/barometer/weather_services/service.rb
Overview
Service Class
This is a base class for creating alternate weather api-consuming drivers. Each driver inherits from this class.
Basically, all a service is required to do is take a query (ie “Paris”) and return a complete Data::Measurement instance.
Direct Known Subclasses
WeatherBug, WeatherDotCom, Wunderground, WeatherService::Google, WeatherService::Noaa, WeatherService::Yahoo
Defined Under Namespace
Classes: Google, Noaa, WeatherBug, WeatherDotCom, Wunderground, Yahoo
Class Method Summary collapse
-
._measure(measurement = nil, query = nil, metric = true) ⇒ Object
STUB: define this method to measure the current & future weather.
-
.accepted_formats ⇒ Object
STUB: define this method to indicate what query formats are accepted.
- .currently_after_sunrise?(current_measurement) ⇒ Boolean
- .currently_before_sunset?(current_measurement) ⇒ Boolean
- .currently_day?(measurement) ⇒ Boolean
-
.currently_sunny?(measurement) ⇒ Boolean
cookie cutter answer.
- .currently_sunny_by_icon?(current_measurement) ⇒ Boolean
-
.currently_wet?(measurement, threshold = 50) ⇒ Boolean
cookie cutter answer.
-
.currently_wet_by_dewpoint?(measurement) ⇒ Boolean
cookie cutter answer.
-
.currently_wet_by_humidity?(current_measurement) ⇒ Boolean
cookie cutter answer.
- .currently_wet_by_icon?(current_measurement) ⇒ Boolean
-
.currently_wet_by_pop?(measurement, threshold = 50) ⇒ Boolean
cookie cutter answer.
-
.currently_windy?(measurement, threshold = 10) ⇒ Boolean
cookie cutter answer, a driver can override this if they answer it differently if a service doesn’t support obtaining the wind value, it will be ignored.
-
.day?(measurement, time_string = nil) ⇒ Boolean
DAY?.
- .forecasted_after_sunrise?(forecast_measurement, time_string) ⇒ Boolean
- .forecasted_before_sunset?(forecast_measurement, time_string) ⇒ Boolean
- .forecasted_day?(measurement, time_string = nil) ⇒ Boolean
-
.forecasted_sunny?(measurement, time_string = nil) ⇒ Boolean
cookie cutter answer.
- .forecasted_sunny_by_icon?(forecast_measurement) ⇒ Boolean
-
.forecasted_wet?(measurement, threshold = 50, time_string = nil) ⇒ Boolean
cookie cutter answer.
- .forecasted_wet_by_icon?(forecast_measurement) ⇒ Boolean
-
.forecasted_wet_by_pop?(forecast_measurement, threshold = 50) ⇒ Boolean
cookie cutter answer.
-
.forecasted_windy?(measurement, threshold, time_string) ⇒ Boolean
no driver can currently answer this question, so it doesn’t have any code.
-
.has_keys? ⇒ Boolean
STUB: define this method to check for the existance of API keys, this method is NOT needed if requires_keys? returns false.
-
.keys=(keys = nil) ⇒ Object
STUB: define this method to check for the existance of API keys, this method is NOT needed if requires_keys? returns false.
-
.measure(query, metric = true) ⇒ Object
get current weather and future (forecasted) weather.
- .meets_requirements?(query = nil) ⇒ Boolean
-
.requires_keys? ⇒ Boolean
DEFAULT: override this if you need to determine if API keys are required.
-
.source(source_name) ⇒ Object
retrieves the weather source Service object.
-
.source_name ⇒ Object
STUB: define this method to actually retireve the source_name.
-
.sunny?(measurement, time_string = nil) ⇒ Boolean
SUNNY?.
-
.sunny_icon_codes ⇒ Object
this returns an array of codes that indicate “sunny”.
-
.supports_country?(query = nil) ⇒ Boolean
DEFAULT: override this if you need to determine if the country is specified.
-
.wet?(measurement, threshold = 50, time_string = nil) ⇒ Boolean
WET?.
-
.wet_icon_codes ⇒ Object
this returns an array of codes that indicate “wet”.
-
.windy?(measurement, threshold = 10, time_string = nil) ⇒ Boolean
WINDY?.
Class Method Details
._measure(measurement = nil, query = nil, metric = true) ⇒ Object
STUB: define this method to measure the current & future weather
64 65 66 |
# File 'lib/barometer/weather_services/service.rb', line 64 def self._measure(measurement=nil, query=nil, metric=true) raise NotImplementedError end |
.accepted_formats ⇒ Object
STUB: define this method to indicate what query formats are accepted
61 |
# File 'lib/barometer/weather_services/service.rb', line 61 def self.accepted_formats; raise NotImplementedError; end |
.currently_after_sunrise?(current_measurement) ⇒ Boolean
232 233 234 235 236 237 238 |
# File 'lib/barometer/weather_services/service.rb', line 232 def self.currently_after_sunrise?(current_measurement) raise ArgumentError unless current_measurement.is_a?(Data::CurrentMeasurement) return nil unless current_measurement.current_at && current_measurement.sun && current_measurement.sun.rise #Time.now.utc >= current_measurement.sun.rise current_measurement.current_at >= current_measurement.sun.rise end |
.currently_before_sunset?(current_measurement) ⇒ Boolean
240 241 242 243 244 245 246 |
# File 'lib/barometer/weather_services/service.rb', line 240 def self.currently_before_sunset?(current_measurement) raise ArgumentError unless current_measurement.is_a?(Data::CurrentMeasurement) return nil unless current_measurement.current_at && current_measurement.sun && current_measurement.sun.set #Time.now.utc <= current_measurement.sun.set current_measurement.current_at <= current_measurement.sun.set end |
.currently_day?(measurement) ⇒ Boolean
225 226 227 228 229 230 |
# File 'lib/barometer/weather_services/service.rb', line 225 def self.currently_day?(measurement) raise ArgumentError unless measurement.is_a?(Data::Measurement) return nil unless measurement.current && measurement.current.sun self.currently_after_sunrise?(measurement.current) && self.currently_before_sunset?(measurement.current) end |
.currently_sunny?(measurement) ⇒ Boolean
cookie cutter answer
288 289 290 291 292 293 |
# File 'lib/barometer/weather_services/service.rb', line 288 def self.currently_sunny?(measurement) raise ArgumentError unless measurement.is_a?(Data::Measurement) return nil unless measurement.current return false if self.currently_day?(measurement) == false self.currently_sunny_by_icon?(measurement.current) end |
.currently_sunny_by_icon?(current_measurement) ⇒ Boolean
307 308 309 310 311 312 313 314 |
# File 'lib/barometer/weather_services/service.rb', line 307 def self.currently_sunny_by_icon?(current_measurement) raise ArgumentError unless current_measurement.is_a?(Data::CurrentMeasurement) return nil unless self.sunny_icon_codes return nil unless current_measurement.icon? current_measurement.icon.is_a?(String) ? self.sunny_icon_codes.include?(current_measurement.icon.to_s.downcase) : self.sunny_icon_codes.include?(current_measurement.icon) end |
.currently_wet?(measurement, threshold = 50) ⇒ Boolean
cookie cutter answer
132 133 134 135 136 137 138 139 140 |
# File 'lib/barometer/weather_services/service.rb', line 132 def self.currently_wet?(measurement, threshold=50) raise ArgumentError unless measurement.is_a?(Data::Measurement) raise ArgumentError unless (threshold.is_a?(Fixnum) || threshold.is_a?(Float)) return nil unless measurement.current self.currently_wet_by_icon?(measurement.current) || self.currently_wet_by_dewpoint?(measurement) || self.currently_wet_by_humidity?(measurement.current) || self.currently_wet_by_pop?(measurement, threshold) end |
.currently_wet_by_dewpoint?(measurement) ⇒ Boolean
cookie cutter answer
143 144 145 146 147 148 149 150 |
# File 'lib/barometer/weather_services/service.rb', line 143 def self.currently_wet_by_dewpoint?(measurement) raise ArgumentError unless measurement.is_a?(Data::Measurement) return nil if (!measurement.current || !measurement.current.temperature? || !measurement.current.dew_point?) measurement.metric? ? measurement.current.temperature.c.to_f <= measurement.current.dew_point.c.to_f : measurement.current.temperature.f.to_f <= measurement.current.dew_point.f.to_f end |
.currently_wet_by_humidity?(current_measurement) ⇒ Boolean
cookie cutter answer
153 154 155 156 157 |
# File 'lib/barometer/weather_services/service.rb', line 153 def self.currently_wet_by_humidity?(current_measurement) raise ArgumentError unless current_measurement.is_a?(Data::CurrentMeasurement) return nil unless current_measurement.humidity? current_measurement.humidity.to_i >= 99 end |
.currently_wet_by_icon?(current_measurement) ⇒ Boolean
191 192 193 194 195 196 197 198 |
# File 'lib/barometer/weather_services/service.rb', line 191 def self.currently_wet_by_icon?(current_measurement) raise ArgumentError unless current_measurement.is_a?(Data::CurrentMeasurement) return nil unless self.wet_icon_codes return nil unless current_measurement.icon? current_measurement.icon.is_a?(String) ? self.wet_icon_codes.include?(current_measurement.icon.to_s.downcase) : self.wet_icon_codes.include?(current_measurement.icon) end |
.currently_wet_by_pop?(measurement, threshold = 50) ⇒ Boolean
cookie cutter answer
160 161 162 163 164 165 166 167 168 |
# File 'lib/barometer/weather_services/service.rb', line 160 def self.currently_wet_by_pop?(measurement, threshold=50) raise ArgumentError unless measurement.is_a?(Data::Measurement) raise ArgumentError unless (threshold.is_a?(Fixnum) || threshold.is_a?(Float)) return nil unless measurement.forecast # get todays forecast forecast_measurement = measurement.for return nil unless forecast_measurement forecast_measurement.pop.to_f >= threshold.to_f end |
.currently_windy?(measurement, threshold = 10) ⇒ Boolean
cookie cutter answer, a driver can override this if they answer it differently if a service doesn’t support obtaining the wind value, it will be ignored
106 107 108 109 110 111 112 113 |
# File 'lib/barometer/weather_services/service.rb', line 106 def self.currently_windy?(measurement, threshold=10) raise ArgumentError unless measurement.is_a?(Data::Measurement) raise ArgumentError unless (threshold.is_a?(Fixnum) || threshold.is_a?(Float)) return nil if (!measurement.current || !measurement.current.wind?) measurement.metric? ? measurement.current.wind.kph.to_f >= threshold.to_f : measurement.current.wind.mph.to_f >= threshold.to_f end |
.day?(measurement, time_string = nil) ⇒ Boolean
DAY?
215 216 217 218 219 220 221 222 223 |
# File 'lib/barometer/weather_services/service.rb', line 215 def self.day?(measurement, time_string=nil) local_datetime = Data::LocalDateTime.parse(time_string) if time_string raise ArgumentError unless measurement.is_a?(Data::Measurement) raise ArgumentError unless (local_datetime.is_a?(Data::LocalDateTime) || local_datetime.nil?) measurement.current?(local_datetime) ? self.currently_day?(measurement) : self.forecasted_day?(measurement, local_datetime) end |
.forecasted_after_sunrise?(forecast_measurement, time_string) ⇒ Boolean
259 260 261 262 263 264 265 |
# File 'lib/barometer/weather_services/service.rb', line 259 def self.forecasted_after_sunrise?(forecast_measurement, time_string) local_datetime = Data::LocalDateTime.parse(time_string) if time_string raise ArgumentError unless forecast_measurement.is_a?(Data::ForecastMeasurement) raise ArgumentError unless (local_datetime.is_a?(Data::LocalDateTime) || local_datetime.nil?) return nil unless forecast_measurement.sun && forecast_measurement.sun.rise local_datetime >= forecast_measurement.sun.rise end |
.forecasted_before_sunset?(forecast_measurement, time_string) ⇒ Boolean
267 268 269 270 271 272 273 |
# File 'lib/barometer/weather_services/service.rb', line 267 def self.forecasted_before_sunset?(forecast_measurement, time_string) local_datetime = Data::LocalDateTime.parse(time_string) if time_string raise ArgumentError unless forecast_measurement.is_a?(Data::ForecastMeasurement) raise ArgumentError unless (local_datetime.is_a?(Data::LocalDateTime) || local_datetime.nil?) return nil unless forecast_measurement.sun && forecast_measurement.sun.set local_datetime <= forecast_measurement.sun.set end |
.forecasted_day?(measurement, time_string = nil) ⇒ Boolean
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/barometer/weather_services/service.rb', line 248 def self.forecasted_day?(measurement, time_string=nil) local_datetime = Data::LocalDateTime.parse(time_string) if time_string raise ArgumentError unless measurement.is_a?(Data::Measurement) raise ArgumentError unless (local_datetime.is_a?(Data::LocalDateTime) || local_datetime.nil?) return nil unless measurement.forecast forecast_measurement = measurement.for(local_datetime) return nil unless forecast_measurement self.forecasted_after_sunrise?(forecast_measurement, local_datetime) && self.forecasted_before_sunset?(forecast_measurement, local_datetime) end |
.forecasted_sunny?(measurement, time_string = nil) ⇒ Boolean
cookie cutter answer
296 297 298 299 300 301 302 303 304 305 |
# File 'lib/barometer/weather_services/service.rb', line 296 def self.forecasted_sunny?(measurement, time_string=nil) local_time = Data::LocalDateTime.parse(time_string) if time_string raise ArgumentError unless measurement.is_a?(Data::Measurement) raise ArgumentError unless (local_time.is_a?(Data::LocalDateTime) || local_time.nil?) return nil unless measurement.forecast return false if self.forecasted_day?(measurement, local_time) == false forecast_measurement = measurement.for(local_time) return nil unless forecast_measurement self.forecasted_sunny_by_icon?(forecast_measurement) end |
.forecasted_sunny_by_icon?(forecast_measurement) ⇒ Boolean
316 317 318 319 320 321 322 323 |
# File 'lib/barometer/weather_services/service.rb', line 316 def self.forecasted_sunny_by_icon?(forecast_measurement) raise ArgumentError unless forecast_measurement.is_a?(Data::ForecastMeasurement) return nil unless self.sunny_icon_codes return nil unless forecast_measurement.icon? forecast_measurement.icon.is_a?(String) ? self.sunny_icon_codes.include?(forecast_measurement.icon.to_s.downcase) : self.sunny_icon_codes.include?(forecast_measurement.icon) end |
.forecasted_wet?(measurement, threshold = 50, time_string = nil) ⇒ Boolean
cookie cutter answer
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/barometer/weather_services/service.rb', line 171 def self.forecasted_wet?(measurement, threshold=50, time_string=nil) local_time = Data::LocalDateTime.parse(time_string) if time_string raise ArgumentError unless measurement.is_a?(Data::Measurement) raise ArgumentError unless (threshold.is_a?(Fixnum) || threshold.is_a?(Float)) raise ArgumentError unless (local_time.is_a?(Data::LocalDateTime) || local_time.nil?) return nil unless measurement.forecast forecast_measurement = measurement.for(local_time) return nil unless forecast_measurement self.forecasted_wet_by_icon?(forecast_measurement) || self.forecasted_wet_by_pop?(forecast_measurement, threshold) end |
.forecasted_wet_by_icon?(forecast_measurement) ⇒ Boolean
200 201 202 203 204 205 206 207 |
# File 'lib/barometer/weather_services/service.rb', line 200 def self.forecasted_wet_by_icon?(forecast_measurement) raise ArgumentError unless forecast_measurement.is_a?(Data::ForecastMeasurement) return nil unless self.wet_icon_codes return nil unless forecast_measurement.icon? forecast_measurement.icon.is_a?(String) ? self.wet_icon_codes.include?(forecast_measurement.icon.to_s.downcase) : self.wet_icon_codes.include?(forecast_measurement.icon) end |
.forecasted_wet_by_pop?(forecast_measurement, threshold = 50) ⇒ Boolean
cookie cutter answer
184 185 186 187 188 189 |
# File 'lib/barometer/weather_services/service.rb', line 184 def self.forecasted_wet_by_pop?(forecast_measurement, threshold=50) raise ArgumentError unless forecast_measurement.is_a?(Data::ForecastMeasurement) raise ArgumentError unless (threshold.is_a?(Fixnum) || threshold.is_a?(Float)) return nil unless forecast_measurement.pop? forecast_measurement.pop.to_f >= threshold.to_f end |
.forecasted_windy?(measurement, threshold, time_string) ⇒ Boolean
no driver can currently answer this question, so it doesn’t have any code
116 |
# File 'lib/barometer/weather_services/service.rb', line 116 def self.forecasted_windy?(measurement, threshold, time_string); nil; end |
.has_keys? ⇒ Boolean
STUB: define this method to check for the existance of API keys,
this method is NOT needed if requires_keys? returns false
74 |
# File 'lib/barometer/weather_services/service.rb', line 74 def self.has_keys?; raise NotImplementedError; end |
.keys=(keys = nil) ⇒ Object
STUB: define this method to check for the existance of API keys,
this method is NOT needed if requires_keys? returns false
78 |
# File 'lib/barometer/weather_services/service.rb', line 78 def self.keys=(keys=nil); nil; end |
.measure(query, metric = true) ⇒ Object
get current weather and future (forecasted) weather
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/barometer/weather_services/service.rb', line 34 def self.measure(query, metric=true) raise ArgumentError unless query.is_a?(Barometer::Query) measurement = Data::Measurement.new(self.source_name, metric) if self.meets_requirements?(query) converted_query = query.convert!(self.accepted_formats) if converted_query measurement.query = converted_query.q measurement.format = converted_query.format measurement = self._measure(measurement, converted_query, metric) end end measurement end |
.meets_requirements?(query = nil) ⇒ Boolean
49 50 51 |
# File 'lib/barometer/weather_services/service.rb', line 49 def self.meets_requirements?(query=nil) self.supports_country?(query) && (!self.requires_keys? || self.has_keys?) end |
.requires_keys? ⇒ Boolean
DEFAULT: override this if you need to determine if API keys are required
84 |
# File 'lib/barometer/weather_services/service.rb', line 84 def self.requires_keys?; false; end |
.source(source_name) ⇒ Object
retrieves the weather source Service object
23 24 25 26 27 28 29 |
# File 'lib/barometer/weather_services/service.rb', line 23 def self.source(source_name) raise ArgumentError unless (source_name.is_a?(String) || source_name.is_a?(Symbol)) source_name = source_name.to_s.split("_").collect{ |s| s.capitalize }.join('') raise ArgumentError unless Barometer::WeatherService.const_defined?(source_name) raise ArgumentError unless Barometer::WeatherService.const_get(source_name).superclass == Barometer::WeatherService Barometer::WeatherService.const_get(source_name) end |
.source_name ⇒ Object
STUB: define this method to actually retireve the source_name
58 |
# File 'lib/barometer/weather_services/service.rb', line 58 def self.source_name; raise NotImplementedError; end |
.sunny?(measurement, time_string = nil) ⇒ Boolean
SUNNY?
278 279 280 281 282 283 284 285 |
# File 'lib/barometer/weather_services/service.rb', line 278 def self.sunny?(measurement, time_string=nil) local_time = Data::LocalDateTime.parse(time_string) if time_string raise ArgumentError unless measurement.is_a?(Data::Measurement) raise ArgumentError unless (local_time.is_a?(Data::LocalDateTime) || local_time.nil?) measurement.current?(local_time) ? self.currently_sunny?(measurement) : self.forecasted_sunny?(measurement, local_time) end |
.sunny_icon_codes ⇒ Object
this returns an array of codes that indicate “sunny”
326 |
# File 'lib/barometer/weather_services/service.rb', line 326 def self.sunny_icon_codes; nil; end |
.supports_country?(query = nil) ⇒ Boolean
DEFAULT: override this if you need to determine if the country is specified
81 |
# File 'lib/barometer/weather_services/service.rb', line 81 def self.supports_country?(query=nil); true; end |
.wet?(measurement, threshold = 50, time_string = nil) ⇒ Boolean
WET?
121 122 123 124 125 126 127 128 129 |
# File 'lib/barometer/weather_services/service.rb', line 121 def self.wet?(measurement, threshold=50, time_string=nil) local_time = Data::LocalDateTime.parse(time_string) if time_string raise ArgumentError unless measurement.is_a?(Data::Measurement) raise ArgumentError unless (threshold.is_a?(Fixnum) || threshold.is_a?(Float)) raise ArgumentError unless (local_time.is_a?(Data::LocalDateTime) || local_time.nil?) measurement.current?(local_time) ? self.currently_wet?(measurement, threshold) : self.forecasted_wet?(measurement, threshold, local_time) end |
.wet_icon_codes ⇒ Object
this returns an array of codes that indicate “wet”
210 |
# File 'lib/barometer/weather_services/service.rb', line 210 def self.wet_icon_codes; nil; end |
.windy?(measurement, threshold = 10, time_string = nil) ⇒ Boolean
WINDY?
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/barometer/weather_services/service.rb', line 93 def self.windy?(measurement, threshold=10, time_string=nil) local_time = Data::LocalDateTime.parse(time_string) if time_string raise ArgumentError unless measurement.is_a?(Data::Measurement) raise ArgumentError unless (threshold.is_a?(Fixnum) || threshold.is_a?(Float)) raise ArgumentError unless (local_time.is_a?(Data::LocalDateTime) || local_time.nil?) measurement.current?(local_time) ? self.currently_windy?(measurement, threshold) : self.forecasted_windy?(measurement, threshold, local_time) end |