Class: Wunderground

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

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ Wunderground

Returns a new instance of Wunderground.



7
8
9
10
11
12
13
# File 'lib/wunderground/wunderground.rb', line 7

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

Instance Method Details

#forecastObject



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

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

#simple_forecast(days = 3) ⇒ Object



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

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

#to_params(params) ⇒ Object



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

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

#todayObject



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

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