lib/barometer/weather_services/wunderground.rb in barometer-0.7.3 vs lib/barometer/weather_services/wunderground.rb in barometer-0.8.0
- old
+ new
@@ -43,21 +43,21 @@
#
# = Wunderground terms of use
# Unable to locate.
#
class WeatherService::Wunderground < WeatherService
-
+
#########################################################################
# PRIVATE
# If class methods could be private, the remaining methods would be.
#
-
+
def self._source_name; :wunderground; end
def self._accepted_formats
[:zipcode, :postalcode, :icao, :coordinates, :geocode]
end
-
+
# these are the icon codes that indicate "wet", used by wet? function
def self._wet_icon_codes
%w(flurries rain sleet snow tstorms nt_flurries nt_rain nt_sleet nt_snow nt_tstorms chancerain chancetstorms)
end
# these are the icon codes that indicate "sun", used by sunny? function
@@ -73,11 +73,11 @@
if measurement.forecast && measurement.current.sun
measurement.forecast.each do |forecast|
forecast.sun = measurement.current.sun
end
end
-
+
measurement
end
def self._parse_full_timezone(data)
raise ArgumentError unless data.is_a?(Hash)
@@ -88,78 +88,80 @@
Data::Zone.new(
data['simpleforecast']['forecastday'].first['date']['tz_long']
)
end
end
-
+
def self._build_links(data)
links = {}
if data["credit"] && data["credit_URL"]
links[data["credit"]] = data["credit_URL"]
end
links
end
def self._build_current(data, metric=true)
raise ArgumentError unless data.is_a?(Hash)
-
+
current = Measurement::Result.new
- current.updated_at = Data::LocalDateTime.parse(data['observation_time']) if data['observation_time']
+ if data['observation_time'] && data['observation_time'].match(/\d/)
+ current.updated_at = Data::LocalDateTime.parse(data['observation_time'])
+ end
current.humidity = data['relative_humidity'].to_i
current.icon = data['icon'] if data['icon']
-
+
current.temperature = Data::Temperature.new(metric)
current.temperature << [data['temp_c'], data['temp_f']]
-
+
current.wind = Data::Speed.new(metric)
current.wind.mph = data['wind_mph'].to_f
current.wind.degrees = data['wind_degrees'].to_i
current.wind.direction = data['wind_dir']
-
+
current.pressure = Data::Pressure.new(metric)
current.pressure << [data['pressure_mb'], data['pressure_in']]
-
+
current.dew_point = Data::Temperature.new(metric)
current.dew_point << [data['dewpoint_c'], data['dewpoint_f']]
-
+
current.heat_index = Data::Temperature.new(metric)
current.heat_index << [data['heat_index_c'], data['heat_index_f']]
-
+
current.wind_chill = Data::Temperature.new(metric)
current.wind_chill << [data['windchill_c'], data['windchill_f']]
-
+
current.visibility = Data::Distance.new(metric)
current.visibility << [data['visibility_km'], data['visibility_mi']]
-
+
current
end
-
+
def self._build_forecast(data, metric=true)
raise ArgumentError unless data.is_a?(Hash)
forecasts = Measurement::ResultArray.new
# go through each forecast and create an instance
if data && data['simpleforecast'] &&
data['simpleforecast']['forecastday']
-
+
data['simpleforecast']['forecastday'].each do |forecast|
forecast_measurement = Measurement::Result.new
forecast_measurement.icon = forecast['icon']
forecast_measurement.date = Date.parse(forecast['date']['pretty'])
forecast_measurement.pop = forecast['pop'].to_i
-
+
forecast_measurement.high = Data::Temperature.new(metric)
forecast_measurement.high << [forecast['high']['celsius'],forecast['high']['fahrenheit']]
-
+
forecast_measurement.low = Data::Temperature.new(metric)
forecast_measurement.low << [forecast['low']['celsius'],forecast['low']['fahrenheit']]
-
+
forecasts << forecast_measurement
end
end
forecasts
end
-
+
def self._build_location(data, geo=nil)
raise ArgumentError unless data.is_a?(Hash)
location = Data::Location.new
if data['display_location']
location.name = data['display_location']['full']
@@ -171,11 +173,11 @@
location.latitude = data['display_location']['latitude']
location.longitude = data['display_location']['longitude']
end
location
end
-
+
def self._build_station(data)
raise ArgumentError unless data.is_a?(Hash)
station = Data::Location.new
station.id = data['station_id']
if data['observation_location']
@@ -188,11 +190,11 @@
station.latitude = data['observation_location']['latitude']
station.longitude = data['observation_location']['longitude']
end
station
end
-
+
def self._build_sun(data)
raise ArgumentError unless data.is_a?(Hash)
sun = nil
if data
if data['moon_phase']
@@ -207,22 +209,22 @@
sun = Data::Sun.new(rise,set)
end
end
sun || Data::Sun.new
end
-
+
# override default _fetch behavior
# this service requires TWO seperate http requests (one for current
# and one for forecasted weather) ... combine the results
#
def self._fetch(query, metric=true)
result = []
result << _fetch_current(query)
result << _fetch_forecast(query)
result
end
-
+
# use HTTParty to get the current weather
#
def self._fetch_current(query)
return unless query
puts "fetch wunderground current: #{query.q}" if Barometer::debug?
@@ -231,11 +233,11 @@
:query => {:query => query.q},
:format => :xml,
:timeout => Barometer.timeout
)['current_observation']
end
-
+
# use HTTParty to get the forecasted weather
#
def self._fetch_forecast(query)
return unless query
puts "fetch wunderground forecast: #{query.q}" if Barometer::debug?
@@ -244,19 +246,19 @@
:query => {:query => query.q},
:format => :xml,
:timeout => Barometer.timeout
)['forecast']
end
-
+
# since we have two sets of data, override these calls to choose the
# right set of data
#
def self._current_result(data); data[0]; end
def self._forecast_result(data=nil); data[1]; end
def self._location_result(data=nil); data[0]; end
def self._station_result(data=nil); data[0]; end
def self._links_result(data=nil); data[0]; end
def self._sun_result(data=nil); data[1]; end
def self._timezone_result(data=nil); data[1]; end
-
+
end
-end
\ No newline at end of file
+end