class Forecast::Weather

Public Class Methods

new(zip_code) click to toggle source
# File lib/forecast_api/weather.rb, line 6
def initialize(zip_code)

  lat_lon = Unirest.get("http://maps.googleapis.com/maps/api/geocode/json?address=#{zip_code}",
                      headers: { "Accept" => "application/json" }).body

  lat = lat_lon["results"][0]["geometry"]["location"]["lat"]
  long = lat_lon["results"][0]["geometry"]["location"]["lng"]
  city = lat_lon["results"][0]["address_components"][1]["long_name"]
  state = lat_lon["results"][0]["address_components"][2]["short_name"]

  weather = Unirest.get("https://api.forecast.io/forecast/f48f53dad752fdd952eac2d70d9fad13/#{lat},#{long}", 
                      headers: { "Accept" => "application/json" }).body
  
  puts "Weather for #{city}, #{state}"
  puts weather["daily"]["summary"]
  puts "Today's weather is " + weather["daily"]["data"][0]["summary"] + " The high is " + weather["daily"]["data"][0]["temperatureMax"].to_s + "°F with a low of " + weather["daily"]["data"][0]["temperatureMin"].to_s + "°F."
  puts "Tomorrow is " + weather["daily"]["data"][1]["summary"] + " The high is " + weather["daily"]["data"][1]["temperatureMax"].to_s + "°F with a low of " + weather["daily"]["data"][1]["temperatureMin"].to_s + "°F."
  puts "3 day forecast is " + weather["daily"]["data"][3]["summary"] + " The high is " + weather["daily"]["data"][3]["temperatureMax"].to_s + "°F with a low of " + weather["daily"]["data"][3]["temperatureMin"].to_s + "°F."
  puts "7 day forecast is " + weather["daily"]["data"][7]["summary"] + " The high is " + weather["daily"]["data"][7]["temperatureMax"].to_s + "°F with a low of " + weather["daily"]["data"][7]["temperatureMin"].to_s + "°F."
end