Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fetch based on string or latitude,longitude

  • Loading branch information...
commit 2e58b6faaf7b0a6c826c66083df6255a6b0ca9ad 1 parent 8aabca5
@keavy authored
View
35 lib/google_weather.rb
@@ -1,29 +1,42 @@
require 'httparty'
require File.dirname(__FILE__) + '/google_weather/data'
-class GoogleWeather
+class GoogleWeather
include HTTParty
base_uri "www.google.com"
-
- attr_reader :zip
-
- def initialize(zip)
- @zip = zip
+
+ attr_reader :param
+
+ def initialize(value)
+ @param = prep_param(value)
end
-
+
def weather
- @weather ||= self.class.get("/ig/api", :query => {:weather => @zip}, :format => :xml)['xml_api_reply']['weather']
+ @weather ||= self.class.get("/ig/api", :query => {:weather => @param}, :format => :xml)['xml_api_reply']['weather']
end
-
+
def forecast_information
@forecast_information ||= ForecastInformation.new(weather['forecast_information'])
end
-
+
def current_conditions
@current_conditions ||= CurrentConditions.new(weather['current_conditions'])
end
-
+
def forecast_conditions
@forecast_conditions ||= weather['forecast_conditions'].map { |cond| ForecastCondition.new(cond) }
end
+
+ private
+ def prep_param(value)
+ if value.kind_of?(Array)
+ value = value.inject([]) do |result, element|
+ result << (element * 1e6).to_i
+ result
+ end
+ value = ",,,#{value[0]},#{value[1]}"
+ else
+ value
+ end
+ end
end
View
1  test/fixtures/coords.xml
@@ -0,0 +1 @@
+<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information><city data=""/><postal_code data=""/><latitude_e6 data="32221700"/><longitude_e6 data="110925800"/><forecast_date data="2012-04-21"/><current_date_time data="2012-04-21 17:00:00 +0000"/><unit_system data="US"/></forecast_information><current_conditions><condition data="Mostly Cloudy"/><temp_f data="66"/><temp_c data="19"/><humidity data="Humidity: 69%"/><icon data="/ig/images/weather/mostly_cloudy.gif"/><wind_condition data="Wind: SE at 2 mph"/></current_conditions><forecast_conditions><day_of_week data="Sat"/><low data="54"/><high data="84"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Clear"/></forecast_conditions><forecast_conditions><day_of_week data="Sun"/><low data="55"/><high data="88"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Clear"/></forecast_conditions><forecast_conditions><day_of_week data="Mon"/><low data="61"/><high data="95"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="Mostly Sunny"/></forecast_conditions><forecast_conditions><day_of_week data="Tue"/><low data="50"/><high data="73"/><icon data="/ig/images/weather/chance_of_rain.gif"/><condition data="Chance of Rain"/></forecast_conditions></weather></xml_api_reply>
View
1  test/fixtures/london.xml
@@ -0,0 +1 @@
+<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information><city data="london,uk"/><postal_code data="london,uk"/><latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2012-04-21"/><current_date_time data="1970-01-01 00:00:00 +0000"/><unit_system data="US"/></forecast_information><current_conditions><condition data="Clear"/><temp_f data="43"/><temp_c data="6"/><humidity data="Humidity: 81%"/><icon data="/ig/images/weather/sunny.gif"/><wind_condition data="Wind: SW at 10 mph"/></current_conditions><forecast_conditions><day_of_week data="Sat"/><low data="43"/><high data="55"/><icon data="/ig/images/weather/chance_of_storm.gif"/><condition data="Chance of Storm"/></forecast_conditions><forecast_conditions><day_of_week data="Sun"/><low data="43"/><high data="57"/><icon data="/ig/images/weather/chance_of_storm.gif"/><condition data="Chance of Storm"/></forecast_conditions><forecast_conditions><day_of_week data="Mon"/><low data="46"/><high data="55"/><icon data="/ig/images/weather/chance_of_rain.gif"/><condition data="Chance of Rain"/></forecast_conditions><forecast_conditions><day_of_week data="Tue"/><low data="45"/><high data="55"/><icon data="/ig/images/weather/fog.gif"/><condition data="Fog"/></forecast_conditions></weather></xml_api_reply>
View
238 test/google_weather_test.rb
@@ -2,13 +2,17 @@
class GoogleWeatherTest < Test::Unit::TestCase
context "Initialization" do
- should "require a zip code" do
+ should "require a value" do
lambda { GoogleWeather.new }.should raise_error
-
- GoogleWeather.new(46544).zip.should == 46544
+
+ GoogleWeather.new(46544).param.should == 46544
+ end
+
+ should "convert lat, lng array into string of e6 formatted lat,lng values" do
+ GoogleWeather.new([32.24959602450668,-110.8394506158091]).param.should == ',,,32249596,-110839450'
end
end
-
+
context "Data" do
setup do
@data = GoogleWeather::Data.new({'foo' => {'data' => 'bar'}})
@@ -17,50 +21,134 @@ class GoogleWeatherTest < Test::Unit::TestCase
should "use method missing to get value for existing keys" do
@data.foo.should == 'bar'
end
-
+
should "return nil for missing keys" do
@data.foobar.should be(nil)
end
end
-
-
+
+
context "Fetching" do
- setup do
- FakeWeb.register_uri(:get, "http://www.google.com/ig/api?weather=46544", :body => fixture_file("fixtures/46544.xml"))
- @weather = GoogleWeather.new(46544)
- end
-
- should "have forecast information" do
- information = @weather.forecast_information
- information.city.should == 'Mishawaka, IN'
- information.postal_code.should == '46544'
- information.unit_system.should == 'US'
- information.forecast_date.should == '2009-03-24'
- information.current_date_time.should == '2009-03-25 04:16:27 +0000'
+ context "with a zip code" do
+ setup do
+ FakeWeb.register_uri(:get, "http://www.google.com/ig/api?weather=46544", :body => fixture_file("fixtures/46544.xml"))
+ @weather = GoogleWeather.new(46544)
+ end
+
+ should "have forecast information" do
+ information = @weather.forecast_information
+ information.city.should == 'Mishawaka, IN'
+ information.postal_code.should == '46544'
+ information.unit_system.should == 'US'
+ information.forecast_date.should == '2009-03-24'
+ information.current_date_time.should == '2009-03-25 04:16:27 +0000'
+ end
+
+ should "have current conditions" do
+ conditions = @weather.current_conditions
+ conditions.humidity.should == 'Humidity: 48%'
+ conditions.icon.should == '/images/weather/cloudy.gif'
+ conditions.temp_c.should == '17'
+ conditions.temp_f.should == '62'
+ conditions.condition.should == 'Overcast'
+ conditions.wind_condition.should == 'Wind: SE at 11 mph'
+ end
+
+ should "have forecast conditions" do
+ conditions = @weather.forecast_conditions
+ conditions[0].low.should == '45'
+ conditions[0].high.should == '67'
+ conditions[0].icon.should == '/images/weather/mostly_sunny.gif'
+ conditions[0].condition.should == 'Partly Sunny'
+ conditions[0].day_of_week.should == 'Tue'
+ conditions[1].low.should == '34'
+ conditions[1].high.should == '52'
+ conditions[1].icon.should == '/images/weather/mostly_sunny.gif'
+ conditions[1].condition.should == 'Mostly Sunny'
+ conditions[1].day_of_week.should == 'Wed'
+ end
end
-
- should "have current conditions" do
- conditions = @weather.current_conditions
- conditions.humidity.should == 'Humidity: 48%'
- conditions.icon.should == '/images/weather/cloudy.gif'
- conditions.temp_c.should == '17'
- conditions.temp_f.should == '62'
- conditions.condition.should == 'Overcast'
- conditions.wind_condition.should == 'Wind: SE at 11 mph'
+
+ context "with a string" do
+ setup do
+ FakeWeb.register_uri(:get, "http://www.google.com/ig/api?weather=London%2CUK", :body => fixture_file("fixtures/london.xml"))
+ @weather = GoogleWeather.new('London,UK')
+ end
+
+ should "have forecast information" do
+ information = @weather.forecast_information
+ information.city.should == 'london,uk'
+ information.postal_code.should == 'london,uk'
+ information.unit_system.should == 'US'
+ information.forecast_date.should == '2012-04-21'
+ information.current_date_time.should == '1970-01-01 00:00:00 +0000'
+ end
+
+ should "have current conditions" do
+ conditions = @weather.current_conditions
+ conditions.humidity.should == 'Humidity: 81%'
+ conditions.icon.should == '/ig/images/weather/sunny.gif'
+ conditions.temp_c.should == '6'
+ conditions.temp_f.should == '43'
+ conditions.condition.should == 'Clear'
+ conditions.wind_condition.should == 'Wind: SW at 10 mph'
+ end
+
+ should "have forecast conditions" do
+ conditions = @weather.forecast_conditions
+ conditions[0].low.should == '43'
+ conditions[0].high.should == '55'
+ conditions[0].icon.should == '/ig/images/weather/chance_of_storm.gif'
+ conditions[0].condition.should == 'Chance of Storm'
+ conditions[0].day_of_week.should == 'Sat'
+ conditions[1].low.should == '43'
+ conditions[1].high.should == '57'
+ conditions[1].icon.should == '/ig/images/weather/chance_of_storm.gif'
+ conditions[1].condition.should == 'Chance of Storm'
+ conditions[1].day_of_week.should == 'Sun'
+ end
end
-
- should "have forecast conditions" do
- conditions = @weather.forecast_conditions
- conditions[0].low.should == '45'
- conditions[0].high.should == '67'
- conditions[0].icon.should == '/images/weather/mostly_sunny.gif'
- conditions[0].condition.should == 'Partly Sunny'
- conditions[0].day_of_week.should == 'Tue'
- conditions[1].low.should == '34'
- conditions[1].high.should == '52'
- conditions[1].icon.should == '/images/weather/mostly_sunny.gif'
- conditions[1].condition.should == 'Mostly Sunny'
- conditions[1].day_of_week.should == 'Wed'
+
+ context "with latitude, longitude array" do
+ setup do
+ FakeWeb.register_uri(:get, "http://www.google.com/ig/api?weather=%2C%2C%2C32221700%2C110925800", :body => fixture_file("fixtures/coords.xml"))
+ @weather = GoogleWeather.new(',,,32221700,110925800')
+ end
+
+ should "have forecast information" do
+ information = @weather.forecast_information
+ information.city.should == ''
+ information.postal_code.should == ''
+ information.latitude_e6.should == '32221700'
+ information.longitude_e6.should == '110925800'
+ information.unit_system.should == 'US'
+ information.forecast_date.should == '2012-04-21'
+ information.current_date_time.should == '2012-04-21 17:00:00 +0000'
+ end
+
+ should "have current conditions" do
+ conditions = @weather.current_conditions
+ conditions.humidity.should == 'Humidity: 69%'
+ conditions.icon.should == '/ig/images/weather/mostly_cloudy.gif'
+ conditions.temp_c.should == '19'
+ conditions.temp_f.should == '66'
+ conditions.condition.should == 'Mostly Cloudy'
+ conditions.wind_condition.should == 'Wind: SE at 2 mph'
+ end
+
+ should "have forecast conditions" do
+ conditions = @weather.forecast_conditions
+ conditions[0].low.should == '54'
+ conditions[0].high.should == '84'
+ conditions[0].icon.should == '/ig/images/weather/sunny.gif'
+ conditions[0].condition.should == 'Clear'
+ conditions[0].day_of_week.should == 'Sat'
+ conditions[1].low.should == '55'
+ conditions[1].high.should == '88'
+ conditions[1].icon.should == '/ig/images/weather/sunny.gif'
+ conditions[1].condition.should == 'Clear'
+ conditions[1].day_of_week.should == 'Sun'
+ end
end
end
end
@@ -71,56 +159,56 @@ class GoogleWeatherTest < Test::Unit::TestCase
{
"xml_api_reply"=> {
- "version"=>"1",
+ "version"=>"1",
"weather"=> {
- "mobile_row"=>"0",
- "mobile_zipped"=>"1",
- "module_id"=>"0",
+ "mobile_row"=>"0",
+ "mobile_zipped"=>"1",
+ "module_id"=>"0",
"forecast_information"=>{
- "city"=>{"data"=>"Mishawaka, IN"},
- "postal_code"=>{"data"=>"46544"},
- "longitude_e6"=>{"data"=>""},
- "current_date_time"=>{"data"=>"2009-03-25 04:16:27 +0000"},
- "latitude_e6"=>{"data"=>""},
- "forecast_date"=>{"data"=>"2009-03-24"},
+ "city"=>{"data"=>"Mishawaka, IN"},
+ "postal_code"=>{"data"=>"46544"},
+ "longitude_e6"=>{"data"=>""},
+ "current_date_time"=>{"data"=>"2009-03-25 04:16:27 +0000"},
+ "latitude_e6"=>{"data"=>""},
+ "forecast_date"=>{"data"=>"2009-03-24"},
"unit_system"=>{"data"=>"US"}
- },
+ },
"current_conditions"=> {
"humidity"=>{"data"=>"Humidity: 48%"},
- "icon"=>{"data"=>"/images/weather/cloudy.gif"},
- "condition"=>{"data"=>"Overcast"},
- "temp_c"=>{"data"=>"17"},
- "wind_condition"=>{"data"=>"Wind: SE at 11 mph"},
+ "icon"=>{"data"=>"/images/weather/cloudy.gif"},
+ "condition"=>{"data"=>"Overcast"},
+ "temp_c"=>{"data"=>"17"},
+ "wind_condition"=>{"data"=>"Wind: SE at 11 mph"},
"temp_f"=>{"data"=>"62"}
- },
- "tab_id"=>"0",
+ },
+ "tab_id"=>"0",
"forecast_conditions"=>[
{
- "high"=>{"data"=>"67"},
+ "high"=>{"data"=>"67"},
"day_of_week"=>{"data"=>"Tue"},
- "icon"=>{"data"=>"/images/weather/mostly_sunny.gif"},
- "condition"=>{"data"=>"Partly Sunny"},
+ "icon"=>{"data"=>"/images/weather/mostly_sunny.gif"},
+ "condition"=>{"data"=>"Partly Sunny"},
"low"=>{"data"=>"45"}
- },
+ },
{
- "high"=>{"data"=>"52"},
- "day_of_week"=>{"data"=>"Wed"},
- "icon"=>{"data"=>"/images/weather/mostly_sunny.gif"},
- "condition"=>{"data"=>"Mostly Sunny"},
+ "high"=>{"data"=>"52"},
+ "day_of_week"=>{"data"=>"Wed"},
+ "icon"=>{"data"=>"/images/weather/mostly_sunny.gif"},
+ "condition"=>{"data"=>"Mostly Sunny"},
"low"=>{"data"=>"34"}
},
{
- "high"=>{"data"=>"54"},
- "day_of_week"=>{"data"=>"Thu"},
- "icon"=>{"data"=>"/images/weather/mostly_sunny.gif"},
- "condition"=>{"data"=>"Mostly Sunny"},
+ "high"=>{"data"=>"54"},
+ "day_of_week"=>{"data"=>"Thu"},
+ "icon"=>{"data"=>"/images/weather/mostly_sunny.gif"},
+ "condition"=>{"data"=>"Mostly Sunny"},
"low"=>{"data"=>"36"}
- },
+ },
{
- "high"=>{"data"=>"56"},
- "day_of_week"=>{"data"=>"Fri"},
- "icon"=>{"data"=>"/images/weather/mostly_sunny.gif"},
- "condition"=>{"data"=>"Mostly Sunny"},
+ "high"=>{"data"=>"56"},
+ "day_of_week"=>{"data"=>"Fri"},
+ "icon"=>{"data"=>"/images/weather/mostly_sunny.gif"},
+ "condition"=>{"data"=>"Mostly Sunny"},
"low"=>{"data"=>"38"}
}
]
Please sign in to comment.
Something went wrong with that request. Please try again.