Class: Wunderground

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/wunderground.rb

Defined Under Namespace

Classes: APIError, MissingAPIKey

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, extra_params = {}) ⇒ Wunderground

Returns a new instance of Wunderground.



16
17
18
19
20
21
# File 'lib/wunderground.rb', line 16

def initialize(api_key = nil, extra_params = {})
  @api_key = api_key || ENV['WUNDERGROUND_API_KEY'] || ENV['WUNDERGROUND_APIKEY'] || self.class.api_key
  @timeout = extra_params[:timeout] || 30
  @throws_exceptions = extra_params[:throws_exceptions] || false
  @language = extra_params[:language]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (protected)

Raises:

  • (NoMethodError)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wunderground.rb', line 55

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_","/")
  url << "/lang:#{@language}" if @language
  if args.last.instance_of? Hash
    opts = args.pop 
    url = url.sub(/\/lang:.*/,'') and url << "/lang:#{opts[:lang]}" if opts[:lang] 
    ip_address = opts[:geo_ip] and args.push("autoip") if opts[:geo_ip]
    timeout = opts[:timeout]
  end
  call(url <<'/q/'<< args.join('/') << ".json" << (ip_address ? "?geo_ip=#{ip_address}" : ''),timeout)
end

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



69
70
71
# File 'lib/wunderground.rb', line 69

def api_key
  @api_key
end

.timeoutObject

Returns the value of attribute timeout.



69
70
71
# File 'lib/wunderground.rb', line 69

def timeout
  @timeout
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



14
15
16
# File 'lib/wunderground.rb', line 14

def api_key
  @api_key
end

#languageObject

Returns the value of attribute language.



14
15
16
# File 'lib/wunderground.rb', line 14

def language
  @language
end

#throws_exceptionsObject

Returns the value of attribute throws_exceptions.



14
15
16
# File 'lib/wunderground.rb', line 14

def throws_exceptions
  @throws_exceptions
end

#timeoutObject

Returns the value of attribute timeout.



14
15
16
# File 'lib/wunderground.rb', line 14

def timeout
  @timeout
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



70
71
72
# File 'lib/wunderground.rb', line 70

def method_missing(sym, *args, &block)
  new(self.api_key, self.attributes).send(sym, *args, &block)
end

Instance Method Details

#base_api_urlObject



23
24
25
# File 'lib/wunderground.rb', line 23

def base_api_url
  "http://api.wunderground.com/api/#{api_key}/"
end

#get_history_for(date, *args) ⇒ Object



26
27
28
29
# File 'lib/wunderground.rb', line 26

def get_history_for(date,*args)
  history = (date.class == String ? "history_#{date}" : "history_#{date.strftime("%Y%m%d")}")
  send("get_#{history}_for",*args)
end

#get_planner_for(date, *args) ⇒ Object



30
31
32
33
34
35
# File 'lib/wunderground.rb', line 30

def get_planner_for(date,*args)
  send("get_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)
end