Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Switch to options hash for locale.

More flexible for future options. Also, moved weather options to method
so people can subclass and override if they want.
  • Loading branch information...
commit c18bf060e0a47e0d92860cfea7217f296343aadf 1 parent 1718b90
@jnunemaker authored
Showing with 21 additions and 6 deletions.
  1. +20 −5 lib/google_weather.rb
  2. +1 −1  test/google_weather_test.rb
View
25 lib/google_weather.rb
@@ -4,17 +4,21 @@
class GoogleWeather
include HTTParty
base_uri "www.google.com"
+ Path = "/ig/api"
attr_reader :param
- attr_reader :locale
- def initialize(value, locale = :en)
- @param = prep_param(value)
- @locale = locale
+ def initialize(value, options={})
+ @param = prep_param(value)
+ @options = options
+ end
+
+ def locale
+ @options[:locale] || :en
end
def weather
- @weather ||= self.class.get("/ig/api", :query => {:weather => @param, :hl => @locale, :oe => 'utf-8'}, :format => :xml)['xml_api_reply']['weather']
+ @weather ||= self.class.get(Path, weather_options)['xml_api_reply']['weather']
end
def forecast_information
@@ -31,6 +35,17 @@ def forecast_conditions
private
+ def weather_options
+ opts = {
+ :query => {
+ :weather => param,
+ :hl => locale,
+ :oe => 'utf-8'
+ },
+ :format => :xml,
+ }
+ end
+
def prep_param(value)
if value.kind_of?(Array)
value = value.inject([]) do |result, element|
View
2  test/google_weather_test.rb
@@ -17,7 +17,7 @@ class GoogleWeatherTest < Test::Unit::TestCase
end
should "set specified locale if it's passed" do
- GoogleWeather.new('Vologda', :ru).locale.should == :ru
+ GoogleWeather.new('Vologda', :locale => :ru).locale.should == :ru
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.