lib/barometer/weather_services/google.rb in barometer-0.5.0 vs lib/barometer/weather_services/google.rb in barometer-0.6.1
- old
+ new
@@ -33,47 +33,29 @@
# = Google terms of use
# This is an unoffical API. There are no terms of use.
#
class WeatherService::Google < WeatherService
- def self.source_name; :google; end
- def self.accepted_formats; [:zipcode, :postalcode, :geocode]; end
+ #########################################################################
+ # PRIVATE
+ # If class methods could be private, the remaining methods would be.
+ #
- def self.wet_icon_codes
+ def self._source_name; :google; end
+ def self._accepted_formats; [:zipcode, :postalcode, :geocode]; end
+
+ def self._wet_icon_codes
%w(rain chance_of_rain chance_of_storm thunderstorm mist)
end
- def self.sunny_icon_codes
+ def self._sunny_icon_codes
%w(sunny mostly_sunny partly_cloudy)
end
-
- def self._measure(measurement, query, metric=true)
- raise ArgumentError unless measurement.is_a?(Data::Measurement)
- raise ArgumentError unless query.is_a?(Barometer::Query)
- measurement.source = self.source_name
-
- begin
- result = self.get_all(query.q, metric)
- rescue Timeout::Error => e
- return measurement
- end
-
- measurement.current = self.build_current(result, metric)
- measurement.forecast = self.build_forecast(result, metric)
- measurement.location = self.build_location(query.geo)
- measurement
- end
- def self.build_current(data, metric=true)
+ def self._build_current(data, metric=true)
raise ArgumentError unless data.is_a?(Hash)
- current = Data::CurrentMeasurement.new
+ current = Measurement::Current.new
- # google gives utc time, no way to convert to local
- #if data && data['forecast_information'] &&
- # data['forecast_information']['current_date_time']
- # current.updated_at = Data::LocalDateTime.parse(data['forecast_information']['current_date_time']['data'])
- #end
-
if data['current_conditions']
data = data['current_conditions']
if data['icon']
icon_match = data['icon']['data'].match(/.*\/([A-Za-z_]*)\.png/)
current.icon = icon_match[1] if icon_match
@@ -94,23 +76,23 @@
end
end
current
end
- def self.build_forecast(data, metric=true)
+ def self._build_forecast(data, metric=true)
raise ArgumentError unless data.is_a?(Hash)
- forecasts = []
+ forecasts = Measurement::ForecastArray.new
return forecasts unless data && data['forecast_information'] &&
data['forecast_information']['forecast_date']
start_date = Date.parse(data['forecast_information']['forecast_date']['data'])
data = data['forecast_conditions'] if data['forecast_conditions']
# go through each forecast and create an instance
d = 0
data.each do |forecast|
- forecast_measurement = Data::ForecastMeasurement.new
+ forecast_measurement = Measurement::Forecast.new
if forecast['icon']
icon_match = forecast['icon']['data'].match(/.*\/([A-Za-z_]*)\.png/)
forecast_measurement.icon = icon_match[1] if icon_match
end
forecast_measurement.condition = forecast['condition']['data'] if forecast['condition']
@@ -128,11 +110,11 @@
d += 1
end
forecasts
end
- def self.build_location(geo=nil)
+ def self._build_location(result=nil, geo=nil)
raise ArgumentError unless (geo.nil? || geo.is_a?(Data::Geo))
location = Data::Location.new
if geo
location.city = geo.locality
location.state_code = geo.region
@@ -143,13 +125,15 @@
end
location
end
# use HTTParty to get the current weather
- def self.get_all(query, metric=true)
+ def self._fetch(query, metric=true)
+ return unless query
+ puts "fetch google: #{query.q}" if Barometer::debug?
self.get(
"http://google.com/ig/api",
- :query => {:weather => query, :hl => (metric ? "en-GB" : "en-US")},
+ :query => {:weather => query.q, :hl => (metric ? "en-GB" : "en-US")},
:format => :xml,
:timeout => Barometer.timeout
)['xml_api_reply']['weather']
end
\ No newline at end of file