Permalink
Browse files

mass execution of all first born get_'s

get_-icide
get_-ocaust
nuclear get_-splosion
  • Loading branch information...
wnadeau committed Jul 3, 2012
1 parent c9fd198 commit 4d1c1c9d116d6aec90696206f5bd57777588819f
Showing with 63 additions and 58 deletions.
  1. +26 −26 README.markdown
  2. +7 −7 lib/wunderground.rb
  3. +30 −25 test/test_wunderground.rb
View
@@ -36,7 +36,7 @@ Check out below and test file for more examples.
Standard request breakdown:
- wrapper_instance.get_[feature]_and_[another feature]_for("location string",optional: "hash", values: "at the end")
+ wrapper_instance.[feature]_and_[another feature]_for("location string",optional: "hash", values: "at the end")
##Optional Hash
@@ -53,29 +53,29 @@ Can you think of a better way to handle these? Pull requests welcome.
The method_missing magic happens here.
- w_api.get_forecast_for("WA","Spokane")
- w_api.get_forecast_and_conditions_for("1234.1234,-1234.1234") #a lat/long string
- w_api.get_webcams_and_conditions_and_alerts_for("33043") #a zipcode
+ w_api.forecast_for("WA","Spokane")
+ w_api.forecast_and_conditions_for("1234.1234,-1234.1234") #a lat/long string
+ w_api.webcams_and_conditions_and_alerts_for("33043") #a zipcode
##Locations
Any location string that Wunderground accepts will pass straight through this wrapper to their API, _except for a specific geo-ip._ (examples below)
#there is some handy array joining, if needed
- w_api.get_forecast_for("WA/Spokane") #equivalent to the next example
- w_api.get_forecast_for("WA","Spokane") #equivalent to the previous example
+ w_api.forecast_for("WA/Spokane") #equivalent to the next example
+ w_api.forecast_for("WA","Spokane") #equivalent to the previous example
#zipcodes,lat/long, aiport codes, all of them just pass straight through this wrapper and into the request URL
- w_api.get_conditions_for("77898")
+ w_api.conditions_for("77898")
#weather station code uniqueness - they use the 'pws:' prefix for weather station codes. So does this wrapper.
- w_api.get_conditions_for("pws:STATIONCODE")
+ w_api.conditions_for("pws:STATIONCODE")
- w_api.get_conditions_for("autoip") #passes straight through, but only gets the weather for your server's IP, so not very useful probably
+ w_api.conditions_for("autoip") #passes straight through, but only gets the weather for your server's IP, so not very useful probably
For geocoding a specific ip address as the location, just provide an IP like this:
- w_api.get_alerts_for(geo_ip: "127.0.0.1")
+ w_api.alerts_for(geo_ip: "127.0.0.1")
This was the quickest workaround to the non-conformity of the auto_ip request format.
@@ -84,42 +84,42 @@ This was the quickest workaround to the non-conformity of the auto_ip request fo
Because the Language modifier in Wunderground's request structure uses a colon, which doesn't jive with the method_missing design, adding a specific language to one request can be done like this:
- w_api.get_forecast_for("France","Paris", lang 'FR')
+ w_api.forecast_for("France","Paris", lang 'FR')
Also, you can set the default language in the constructor or with a setter.
w_api = Wunderground.new("apikey",language: "FR")
w_api.language = 'FR'
- w_api.get_forecast_for("France","Paris") #automatically includes /lang:FR/ in the request url, so results will be in French
- w_api.get_forecast_for("France","Paris",lang: 'DE') #this will override the French(FR) default with German(DE)
+ w_api.forecast_for("France","Paris") #automatically includes /lang:FR/ in the request url, so results will be in French
+ w_api.forecast_for("France","Paris",lang: 'DE') #this will override the French(FR) default with German(DE)
##History and Planner Helpers
While it is possible to call
- w_api.get_history20101231_for("77789")
- w_api.get_planner03150323_for("FL","Destin")
+ w_api.history20101231_for("77789")
+ w_api.planner03150323_for("FL","Destin")
-to get the history/planner data for this date/location. You may enjoy more flexibility when using get_history_for and get_planner_for:
+to get the history/planner data for this date/location. You may enjoy more flexibility when using history_for and planner_for:
- w_api.get_history_for("20101010","AL","Birmingham")
- w_api.get_history_for(1.year.ago,"33909")
- w_api.get_history_for(Date.now, "France/Paris",lang: "FR")
- w_api.get_history_for(Date.now, geo_ip:"123.4.5.6", lang: "FR")
- w_api.get_planner_for("03150323","AL","Gulf Shores")
- w_api.get_planner_for(Time.now,Time.now+7.days, geo_ip: "10.0.0.1")
- w_api.get_planner_for(Time.now,Time.now+7.days,"33030")
+ w_api.history_for("20101010","AL","Birmingham")
+ w_api.history_for(1.year.ago,"33909")
+ w_api.history_for(Date.now, "France/Paris",lang: "FR")
+ w_api.history_for(Date.now, geo_ip:"123.4.5.6", lang: "FR")
+ w_api.planner_for("03150323","AL","Gulf Shores")
+ w_api.planner_for(Time.now,Time.now+7.days, geo_ip: "10.0.0.1")
+ w_api.planner_for(Time.now,Time.now+7.days,"33030")
-.get_history_for and .get_planner_for accepts a preformatted string or any Date/Time/DateTime-like object that responds to .strftime to auto-format the date.
+.history_for and .planner_for accepts a preformatted string or any Date/Time/DateTime-like object that responds to .strftime to auto-format the date.
-### Request Timeout
+## Request Timeout
wunderground_ruby defaults to a 30 second timeout. You can optionally set your own timeout (in seconds) in three ways like so:
w_api = Wunderground.new("apikey",timeout: 60)
w_api.timeout = 5
- w_api.get_history_for(1.year.ago, geo_ip: '127.0.0.1', timeout:60)
+ w_api.history_for(1.year.ago, geo_ip: '127.0.0.1', timeout:60)
### Error Handling
View
@@ -23,15 +23,15 @@ def initialize(api_key = nil, extra_params = {})
def base_api_url
"http://api.wunderground.com/api/#{api_key}/"
end
- def get_history_for(date,*args)
+ def history_for(date,*args)
history = (date.class == String ? "history_#{date}" : "history_#{date.strftime("%Y%m%d")}")
- send("get_#{history}_for",*args)
+ send("#{history}_for",*args)
end
- def get_planner_for(date,*args)
- send("get_planner_#{date}_for",args) and return if date.class == String
+ def planner_for(date,*args)
+ send("planner_#{date}_for",args) and return if date.class == String
range = date.strftime("%m%d") << args[0].strftime("%m%d")
args.delete_at(0)
- send("get_planner_#{range}_for",*args)
+ send("planner_#{range}_for",*args)
end
protected
@@ -53,8 +53,8 @@ def call(method, timeout)
end
def method_missing(method, *args)
- raise NoMethodError, "undefined method: #{method} for Wunderground" unless method.to_s.start_with?("get_") and method.to_s.end_with?("_for")
- url = method.to_s.gsub("get_","").gsub("_for","").gsub("_and_","/")
+ raise NoMethodError, "undefined method: #{method} for Wunderground" unless method.to_s.end_with?("_for")
+ url = method.to_s.gsub("_for","").gsub("_and_","/")
url << "/lang:#{@language}" if @language
if args.last.instance_of? Hash
opts = args.pop
View
@@ -62,99 +62,104 @@ class TestWunderground < Test::Unit::TestCase
@wunderground.api_key=nil
expect_get(@url,{timeout:30})
assert_raise Wunderground::MissingAPIKey do
- @wunderground.get_forecast_for("CA","San Fransisco")
+ @wunderground.forecast_for("CA","San Fransisco")
end
end
should "contain api key" do
expect_get(@url+"forecast/q/ME/Portland.json",{timeout:30})
- @wunderground.get_forecast_for("ME","Portland")
+ @wunderground.forecast_for("ME","Portland")
end
should 'contain multiple Wunderground methods from ruby method' do
expect_get(@url+"forecast/conditions/q/.json",{timeout: 30})
- @wunderground.get_forecast_and_conditions_for()
+ @wunderground.forecast_and_conditions_for()
end
should 'contain language modifier for method with {lang:"code"} hash' do
expect_get(@url+"forecast/lang:FR/q/ME/Portland.json",{timeout: 30})
- @wunderground.get_forecast_for("ME","Portland", lang: 'FR')
+ @wunderground.forecast_for("ME","Portland", lang: 'FR')
end
context 'location parameter' do
should 'formats query of type array' do
expect_get(@url+"forecast/q/ME/Portland.json",{timeout: 30})
- @wunderground.get_forecast_for("ME","Portland")
+ @wunderground.forecast_for("ME","Portland")
end
should 'formats query of type string' do
expect_get(@url+"forecast/q/1234.1234,-1234.1234.json",{timeout: 30})
- @wunderground.get_forecast_for("1234.1234,-1234.1234")
+ @wunderground.forecast_for("1234.1234,-1234.1234")
expect_get(@url+"forecast/q/pws:WHAT.json",{timeout: 30})
- @wunderground.get_forecast_for("pws:WHAT")
+ @wunderground.forecast_for("pws:WHAT")
end
should 'formats query of type geo_ip' do
expect_get(@url+"forecast/q/autoip.json?geo_ip=127.0.0.1",{timeout: 30})
- @wunderground.get_forecast_for(geo_ip: "127.0.0.1")
+ @wunderground.forecast_for(geo_ip: "127.0.0.1")
end
end
context 'language support' do
setup {@wunderground.language = "FR"}
should 'automatically set language for all location types' do
expect_get(@url+"forecast/lang:FR/q/pws:KCATAHOE2.json",{timeout: 30})
- @wunderground.get_forecast_for("pws:KCATAHOE2")
+ @wunderground.forecast_for("pws:KCATAHOE2")
end
should 'have optional language override on call' do
expect_get(@url+"forecast/lang:DE/q/ME/Portland.json",{timeout: 30})
- @wunderground.get_forecast_for("ME","Portland", lang: 'DE')
+ @wunderground.forecast_for("ME","Portland", lang: 'DE')
end
should 'pass language through history helper' do
expect_get(@url+"history_#{Time.now.strftime("%Y%m%d")}/lang:DE/q/ME/Portland.json",{timeout: 30})
- @wunderground.get_history_for(Time.now,"ME","Portland",lang: 'DE')
+ @wunderground.history_for(Time.now,"ME","Portland",lang: 'DE')
end
should 'pass language through planner helper' do
expect_get(@url+"planner_#{Time.now.strftime("%m%d")}#{(Time.now + 700000).strftime('%m%d')}/lang:DE/q/ME/Portland.json",{timeout: 30})
- @wunderground.get_planner_for(Time.now,(Time.now+700000),"ME","Portland", lang: 'DE')
+ @wunderground.planner_for(Time.now,(Time.now+700000),"ME","Portland", lang: 'DE')
end
should 'pass language through planner helper with IP' do
expect_get(@url+"planner_#{Time.now.strftime('%m%d')}#{(Time.now +
700000).strftime('%m%d')}/lang:DE/q/autoip.json?geo_ip=127.0.0.1",{timeout: 30})
- @wunderground.get_planner_for(Time.now,(Time.now+700000),lang: "DE",geo_ip: "127.0.0.1")
+ @wunderground.planner_for(Time.now,(Time.now+700000),lang: "DE",geo_ip: "127.0.0.1")
+ end
+ should 'encode string arguments' do
+ expect_get(@url+"planner_#{Time.now.strftime('%m%d')}#{(Time.now +
+ 700000).strftime('%m%d')}/lang:DE/q/autoip.json?geo_ip=127.0.0.1",{timeout: 30})
+ @wunderground.planner_for(Time.now,(Time.now+700000),lang: "DE",geo_ip: "127.0.0.1")
end
end
- context 'for get_history_for(date,location) helper' do
+ context 'for history_for(date,location) helper' do
should 'pass string dates straight to URL' do
expect_get(@url+"history_20110121/q/ME/Portland.json",{timeout: 30})
- @wunderground.get_history_for("20110121","ME","Portland")
+ @wunderground.history_for("20110121","ME","Portland")
end
should 'accept Time objects' do
expect_get(@url+"history_#{Time.now.strftime("%Y%m%d")}/q/ME/Portland.json",{timeout: 30})
- @wunderground.get_history_for(Time.now,"ME","Portland")
+ @wunderground.history_for(Time.now,"ME","Portland")
end
should 'accept Date objects' do
expect_get(@url+"history_#{Time.now.strftime("%Y%m%d")}/q/ME/Portland.json",{timeout: 30})
- @wunderground.get_history_for(Time.now.to_date,"ME","Portland")
+ @wunderground.history_for(Time.now.to_date,"ME","Portland")
end
should 'accept Date object and pass optional hash object' do
expect_get(@url+"history_#{Time.now.strftime("%Y%m%d")}/lang:FR/q/autoip.json?geo_ip=127.0.0.1",{timeout: 30})
- @wunderground.get_history_for(Time.now.to_datetime,geo_ip: '127.0.0.1',lang: 'FR')
+ @wunderground.history_for(Time.now.to_datetime,geo_ip: '127.0.0.1',lang: 'FR')
end
end
- context 'for get_planner_for helper' do
+ context 'for planner_for helper' do
should 'pass string date ranges through' do
expect_get(@url+"planner_03130323/q/ME/Portland.json",timeout: 30)
- @wunderground.get_planner_for("03130323","ME","Portland")
+ @wunderground.planner_for("03130323","ME","Portland")
end
should 'turn two date objects into a properly formatted string' do
expect_get(@url+"planner_#{Time.now.strftime('%m%d')}#{(Time.now +
700000).strftime('%m%d')}/lang:FR/q/autoip.json?geo_ip=127.0.0.1",timeout: 30)
- @wunderground.get_planner_for(Time.now,(Time.now + 700000),geo_ip: '127.0.0.1',lang:'FR')
+ @wunderground.planner_for(Time.now,(Time.now + 700000),geo_ip: '127.0.0.1',lang:'FR')
end
end
context 'timeout passed through optional hash' do
should 'work for helper' do
expect_get(@url+"planner_#{Time.now.strftime('%m%d')}#{(Time.now+700000).strftime('%m%d')}/lang:FR/q/autoip.json?geo_ip=127.0.0.1",timeout: 60)
- @wunderground.get_planner_for(Time.now,(Time.now + 700000),geo_ip: '127.0.0.1',lang:'FR',timeout: 60)
+ @wunderground.planner_for(Time.now,(Time.now + 700000),geo_ip: '127.0.0.1',lang:'FR',timeout: 60)
end
should 'work for regular calls' do
expect_get(@url+"forecast/q/pws:WHAT.json",timeout: 60)
- @wunderground.get_forecast_for("pws:WHAT", timeout: 60)
+ @wunderground.forecast_for("pws:WHAT", timeout: 60)
end
end
end
@@ -169,7 +174,7 @@ class TestWunderground < Test::Unit::TestCase
end
- should 'throw exception if non-standard get_function_for(location) method is called' do
+ should 'throw exception if non-standard function_for(location) method is called' do
assert_raise NoMethodError do
@wunderground.scramble
end
@@ -179,7 +184,7 @@ class TestWunderground < Test::Unit::TestCase
@wunderground.throws_exceptions = true
Wunderground.stubs(:get).returns(Struct.new(:body).new({response:{'error' => 'bad things'}}.to_json))
assert_raise Wunderground::APIError do
- @wunderground.get_forecast_for("CA","San_Fransisco")
+ @wunderground.forecast_for("CA","San_Fransisco")
end
end
end

0 comments on commit 4d1c1c9

Please sign in to comment.