Sha256: 5a498f2f0cb3581d6cd4349d049ede9592c00f19a6bb6194773ccdcb9618ff27

Contents?: true

Size: 930 Bytes

Versions: 1

Compression:

Stored size: 930 Bytes

Contents

require 'xmlsimple'
require 'andand'
require 'net/http'
require 'cgi'

class Wunderground
  def initialize(location)
    @location = location
    params = to_params(:query => @location)

    data = Net::HTTP.get(URI.parse "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?#{params}")
    @xml = XmlSimple.xml_in(data, { 'ForceArray' => false })
  end

  def to_params(params)
    params.map {|key, value| "#{CGI.escape key.to_s}=#{CGI.escape value.to_s}"}.join "&"
  end

  def today
    @xml.andand['txt_forecast'].andand['forecastday'].sort {|a, b| a['period'].to_i <=> b['period'].to_i}
  end

  def forecast
    @xml.andand['simpleforecast'].andand['forecastday']
  end

  def simple_forecast(days = 3)
    forecast.andand[0, days.to_i].andand.each do |day|
      printf "%s: %s, High %s Low %s\n", day['date']['weekday'], day['conditions'], day['high']['fahrenheit'], day['low']['fahrenheit']
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wunderground-api-0.2.0 lib/wunderground/wunderground.rb