class Wunderground

Attributes

api_key[RW]
timeout[RW]
api_key[RW]
language[RW]
throws_exceptions[RW]
timeout[RW]

Public Class Methods

new(*args) click to toggle source
# 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

Protected Class Methods

method_missing(sym, *args, &block) click to toggle source
# File lib/wunderground.rb, line 82
def method_missing(sym, *args, &block)
  new(self.api_key, self.attributes).send(sym, *args, &block)
end

Public Instance Methods

base_api_url() click to toggle source
# File lib/wunderground.rb, line 29
def base_api_url
  "http://api.wunderground.com/api/#{api_key}/"
end
history_for(date,*args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
Calls superclass method
# File lib/wunderground.rb, line 45
def respond_to?(method, include_all = false)
  method_missing_match?(method) || super
end

Protected Instance Methods

call(method, timeout) click to toggle source
# File lib/wunderground.rb, line 51
def call(method, timeout)
  raise MissingAPIKey if @api_key.nil?
  response = self.class.get(base_api_url << method, :timeout => (timeout || @timeout))
  begin
    response = JSON.parse(response.body)
  rescue
    response = response.body
  end

  if @throws_exceptions && response.is_a?(Hash) && response["response"]["error"]
    raise APIError, "#{response["response"]["error"]["type"]}: #{response["response"]["error"]["description"]})"
  end

  response
end
method_missing(method, *args) click to toggle source
Calls superclass method
# 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

Private Instance Methods

method_missing_match?(method) click to toggle source
# File lib/wunderground.rb, line 89
def method_missing_match?(method)
  method.to_s.end_with?("_for")
end