manveru / simplweather

Project for simplweather.com

This URL has Read+Write access

simplweather / start.rb
100644 38 lines (28 sloc) 1.126 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env ruby
 
require 'rubygems'
require 'ramaze'
require 'open-uri'
require 'hpricot'
 
class MainController < Ramaze::Controller
  URI = "http://api.wunderground.com/auto/wui/geo/%s/index.xml?query=%s"
 
  def index
    @t = (request[:t] || 'C').upcase
    @q = request[:q] || guess_location
    @c = @t == 'C'
 
    current = Hpricot(open(URI % ['WXCurrentObXML', u(@q)]))
    @location = current.at("current_observation/display_location/full")
    @temperature = current.at(@c ? 'temp_c' : "temp_f")
    @condition = current.at("weather")
 
    forecast = Hpricot(open(URI % ['ForecastXML', u(@q)]))
    @days = forecast/"forecast/simpleforecast/forecastday/date/weekday/"
    @highs = forecast/"forecast/simpleforecast/forecastday/high/#{@c ? 'celsius' : 'fahrenheit'}/"
    @lows = forecast/"forecast/simpleforecast/forecastday/low/#{@c ? 'celsius' : 'fahrenheit'}/"
    @conditions = forecast/"forecast/simpleforecast/forecastday/conditions/"
  end
 
  private
 
  # TODO: do something super smart in here.
 
  def guess_location
    'Tokyo'
  end
end
 
Ramaze.start :adapter => :thin