Class: Forecast::Weather
- Inherits:
-
Object
- Object
- Forecast::Weather
- Defined in:
- lib/forecast_api/weather.rb
Instance Method Summary collapse
-
#initialize(zip_code) ⇒ Weather
constructor
A new instance of Weather.
Constructor Details
#initialize(zip_code) ⇒ Weather
Returns a new instance of Weather.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 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 |