Guest User

Weather App

a guest
Jan 27th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.08 KB | None | 0 0
  1. # fetch_weather.rb model
  2. class FetchWeather < ActiveRecord::Base
  3.  
  4.  def fetch_weather
  5.     HTTParty.get("http://api.wunderground.com/api/0299ecb9aa94e332/hourly/q/" + "37064" + ".xml")
  6.  end
  7.  
  8.  attr_accessor :temperature, :condition
  9.  
  10.  def initialize
  11.   weather_hash = fetch_weather
  12.   assign_values(weather_hash)
  13.  end
  14.  
  15.  def assign_values(weather_hash)
  16.       hourly_forecast_response = weather_hash.parsed_response['response']['hourly_forecast']['forecast'].first
  17.       self.temperature = hourly_forecast_response['temp']['english']
  18.       self.condition = hourly_forecast_response['condition']
  19.  end
  20.  
  21. end
  22.  
  23. # weather_controller.rb
  24. class WeatherController < ApplicationController
  25.   def weather
  26.     @weather_lookup = FetchWeather.new
  27.   end
  28. end
  29.  
  30. # weather view of weather controller (weather#weather)
  31. <h2>CITY</h2>
  32. <h3><%= @weather_lookup.temperature %>° - <%= @weather_lookup.condition %></h3>
  33. <h3><%= params[:zip] %></h3>
  34.  
  35. # routes.rb
  36. Eightbitwx::Application.routes.draw do
  37.  
  38.   get "weather/weather"
  39.   match ":zip" => "weather#weather"
  40.   root :to => 'pages#home'
  41.  
  42. end
Advertisement
Add Comment
Please, Sign In to add comment