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(*args) ⇒ Wunderground

Returns a new instance of Wunderground.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wunderground.rb', line 16

def initialize(*args)
  extra_params = {}
  if !args.nil?
    api_key = args.first if args.first.is_a?(String)
    extra_params = args.last if args.last.is_a?(Hash)
  end
  
  @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)



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wunderground.rb', line 67

def method_missing(method, *args)
  super(method, *args) unless method_missing_match?(method)
  url = method.to_s.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/'<< URI.escape(args.join('/')) << ".json" << (ip_address ? "?geo_ip=#{ip_address}" : ''),timeout)
end

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



81
82
83
# File 'lib/wunderground.rb', line 81

def api_key
  @api_key
end

.timeoutObject

Returns the value of attribute timeout.



81
82
83
# File 'lib/wunderground.rb', line 81

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



82
83
84
# File 'lib/wunderground.rb', line 82

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

Instance Method Details

#base_api_urlObject



29
30
31
# File 'lib/wunderground.rb', line 29

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

#history_for(date, *args) ⇒ Object



33
34
35
36
# File 'lib/wunderground.rb', line 33

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

#planner_for(date, *args) ⇒ Object



38
39
40
41
42
43
# File 'lib/wunderground.rb', line 38

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("planner_#{range}_for",*args)
end

#respond_to?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/wunderground.rb', line 45

def respond_to?(method, include_all = false)
  method_missing_match?(method) || super
end