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. This class creates some default behaviours, but they can easily be over-ridden.
Basically, all a service is required to do is take a query (ie “Paris”) and return a complete Barometer::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
- ._accepted_formats ⇒ Object
- ._build_current(result = nil, metric = true) ⇒ Object
- ._build_extra(measurement = nil, result = nil, metric = true) ⇒ Object
- ._build_forecast(result = nil, metric = true) ⇒ Object
- ._build_links(result = nil) ⇒ Object
- ._build_local_time(measurement) ⇒ Object
-
._build_location(result = nil, geo = nil) ⇒ Object
data processing stubs.
- ._build_station(result = nil) ⇒ Object
- ._build_sun(result = nil) ⇒ Object
- ._build_timezone(result = nil) ⇒ Object
-
._current_result(data = nil) ⇒ Object
data accessors (see the wunderground driver for an example of overriding these).
- ._fetch(query = nil, metric = true) ⇒ Object
- ._forecast_result(data = nil) ⇒ Object
-
._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.
- ._links_result(data = nil) ⇒ Object
-
._local_time(result, measurement = nil) ⇒ Object
return the current local time (as Data::LocalTime).
- ._location_result(data = nil) ⇒ Object
-
._measure(measurement, query, metric = true) ⇒ Object
this is the generic measuring and data processing for each weather service driver.
- ._meets_requirements?(query = nil) ⇒ Boolean
-
._parse_full_timezone(result = nil) ⇒ Object
given the result set, return the full_timezone or local time …
- ._parse_local_time(result = nil) ⇒ Object
-
._requires_keys? ⇒ Boolean
DEFAULT: override this if you need to determine if API keys are required.
-
._source_name ⇒ Object
REQUIRED re-defining these methods will be required.
- ._station_result(data = nil) ⇒ Object
- ._sun_result(data = nil) ⇒ Object
-
._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.
- ._time_result(data = nil) ⇒ Object
-
._timezone(result = nil, query = nil, location = nil) ⇒ Object
either get the timezone based on coords, or build it from the data.
- ._timezone_result(data = nil) ⇒ Object
-
._wet_icon_codes ⇒ Object
this returns an array of codes that indicate “wet”.
-
.measure(query, metric = true) ⇒ Object
get current weather and future (forecasted) weather.
-
.source(source_name) ⇒ Object
retrieves the weather source Service object.
Class Method Details
._accepted_formats ⇒ Object
64 |
# File 'lib/barometer/weather_services/service.rb', line 64 def self._accepted_formats; raise NotImplementedError; end |
._build_current(result = nil, metric = true) ⇒ Object
66 |
# File 'lib/barometer/weather_services/service.rb', line 66 def self._build_current(result=nil, metric=true); nil; end |
._build_extra(measurement = nil, result = nil, metric = true) ⇒ Object
81 |
# File 'lib/barometer/weather_services/service.rb', line 81 def self._build_extra(measurement=nil, result=nil, metric=true); measurement; end |
._build_forecast(result = nil, metric = true) ⇒ Object
67 |
# File 'lib/barometer/weather_services/service.rb', line 67 def self._build_forecast(result=nil, metric=true); nil; end |
._build_links(result = nil) ⇒ Object
78 |
# File 'lib/barometer/weather_services/service.rb', line 78 def self._build_links(result=nil); {}; end |
._build_local_time(measurement) ⇒ Object
82 83 84 |
# File 'lib/barometer/weather_services/service.rb', line 82 def self._build_local_time(measurement) (measurement && measurement.timezone) ? Data::LocalTime.parse(measurement.timezone.now) : nil end |
._build_location(result = nil, geo = nil) ⇒ Object
data processing stubs
76 |
# File 'lib/barometer/weather_services/service.rb', line 76 def self._build_location(result=nil, geo=nil); nil; end |
._build_station(result = nil) ⇒ Object
77 |
# File 'lib/barometer/weather_services/service.rb', line 77 def self._build_station(result=nil); Data::Location.new; end |
._build_sun(result = nil) ⇒ Object
79 |
# File 'lib/barometer/weather_services/service.rb', line 79 def self._build_sun(result=nil); Data::Sun.new; end |
._build_timezone(result = nil) ⇒ Object
80 |
# File 'lib/barometer/weather_services/service.rb', line 80 def self._build_timezone(result=nil); nil; end |
._current_result(data = nil) ⇒ Object
data accessors (see the wunderground driver for an example of overriding these)
118 |
# File 'lib/barometer/weather_services/service.rb', line 118 def self._current_result(data=nil); data; end |
._fetch(query = nil, metric = true) ⇒ Object
65 |
# File 'lib/barometer/weather_services/service.rb', line 65 def self._fetch(query=nil, metric=true); nil; end |
._forecast_result(data = nil) ⇒ Object
119 |
# File 'lib/barometer/weather_services/service.rb', line 119 def self._forecast_result(data=nil); data; 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
103 |
# File 'lib/barometer/weather_services/service.rb', line 103 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
107 |
# File 'lib/barometer/weather_services/service.rb', line 107 def self._keys=(keys=nil); nil; end |
._links_result(data = nil) ⇒ Object
122 |
# File 'lib/barometer/weather_services/service.rb', line 122 def self._links_result(data=nil); data; end |
._local_time(result, measurement = nil) ⇒ Object
return the current local time (as Data::LocalTime)
182 183 184 |
# File 'lib/barometer/weather_services/service.rb', line 182 def self._local_time(result, measurement=nil) _parse_local_time(result) || _build_local_time(measurement) end |
._location_result(data = nil) ⇒ Object
120 |
# File 'lib/barometer/weather_services/service.rb', line 120 def self._location_result(data=nil); data; end |
._measure(measurement, query, metric = true) ⇒ Object
this is the generic measuring and data processing for each weather service driver. this method should be re-defined if the driver in question doesn’t fit into “generic” (ie wunderground)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/barometer/weather_services/service.rb', line 137 def self._measure(measurement, query, metric=true) raise ArgumentError unless measurement.is_a?(Barometer::Measurement) raise ArgumentError unless query.is_a?(Barometer::Query) begin result = _fetch(query, metric) rescue Timeout::Error => e return measurement end if result measurement.current = _build_current(_current_result(result), metric) measurement.forecast = _build_forecast(_forecast_result(result), metric) measurement.location = _build_location(_location_result(result), query.geo) measurement.station = _build_station(_station_result(result)) measurement.links = _build_links(_links_result(result)) measurement.current.sun = _build_sun(_sun_result(result)) if measurement.current measurement.timezone = _timezone(_timezone_result(result), query, measurement.location) if local_time = _local_time(_time_result(result), measurement) measurement.measured_at = local_time measurement.current.current_at = local_time end measurement = _build_extra(measurement, result, metric) end measurement end |
._meets_requirements?(query = nil) ⇒ Boolean
186 187 188 |
# File 'lib/barometer/weather_services/service.rb', line 186 def self._meets_requirements?(query=nil) self._supports_country?(query) && (!self._requires_keys? || self._has_keys?) end |
._parse_full_timezone(result = nil) ⇒ Object
given the result set, return the full_timezone or local time … if not available return nil
88 |
# File 'lib/barometer/weather_services/service.rb', line 88 def self._parse_full_timezone(result=nil); nil; end |
._parse_local_time(result = nil) ⇒ Object
89 |
# File 'lib/barometer/weather_services/service.rb', line 89 def self._parse_local_time(result=nil); nil; end |
._requires_keys? ⇒ Boolean
DEFAULT: override this if you need to determine if API keys are required
113 |
# File 'lib/barometer/weather_services/service.rb', line 113 def self._requires_keys?; false; end |
._source_name ⇒ Object
REQUIRED re-defining these methods will be required
63 |
# File 'lib/barometer/weather_services/service.rb', line 63 def self._source_name; raise NotImplementedError; end |
._station_result(data = nil) ⇒ Object
121 |
# File 'lib/barometer/weather_services/service.rb', line 121 def self._station_result(data=nil); data; end |
._sun_result(data = nil) ⇒ Object
123 |
# File 'lib/barometer/weather_services/service.rb', line 123 def self._sun_result(data=nil); data; end |
._sunny_icon_codes ⇒ Object
this returns an array of codes that indicate “sunny”
94 |
# File 'lib/barometer/weather_services/service.rb', line 94 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
110 |
# File 'lib/barometer/weather_services/service.rb', line 110 def self._supports_country?(query=nil); true; end |
._time_result(data = nil) ⇒ Object
125 |
# File 'lib/barometer/weather_services/service.rb', line 125 def self._time_result(data=nil); data; end |
._timezone(result = nil, query = nil, location = nil) ⇒ Object
either get the timezone based on coords, or build it from the data
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/barometer/weather_services/service.rb', line 167 def self._timezone(result=nil, query=nil, location=nil) if full_timezone = _parse_full_timezone(result) full_timezone elsif query && query.timezone query.timezone elsif Barometer.enhance_timezone && location && location.latitude && location.longitude WebService::Timezone.fetch(location.latitude, location.longitude) else _build_timezone(result) end end |
._timezone_result(data = nil) ⇒ Object
124 |
# File 'lib/barometer/weather_services/service.rb', line 124 def self._timezone_result(data=nil); data; end |
._wet_icon_codes ⇒ Object
this returns an array of codes that indicate “wet”
92 |
# File 'lib/barometer/weather_services/service.rb', line 92 def self._wet_icon_codes; nil; end |
.measure(query, metric = true) ⇒ Object
get current weather and future (forecasted) weather
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/barometer/weather_services/service.rb', line 35 def self.measure(query, metric=true) raise ArgumentError unless query.is_a?(Barometer::Query) measurement = Barometer::Measurement.new(self._source_name, metric) measurement.start_at = Time.now.utc if self._meets_requirements?(query) converted_query = query.convert!(self._accepted_formats) if converted_query measurement.source = self._source_name measurement.query = converted_query.q measurement.format = converted_query.format measurement = self._measure(measurement, converted_query, metric) end end measurement.end_at = Time.now.utc measurement end |
.source(source_name) ⇒ Object
retrieves the weather source Service object
24 25 26 27 28 29 30 |
# File 'lib/barometer/weather_services/service.rb', line 24 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 |