Skip to content

Instantly share code, notes, and snippets.

@jhwhite
Created November 20, 2015 18:47
Show Gist options
  • Save jhwhite/e171b8fef88c0cbb043b to your computer and use it in GitHub Desktop.
Save jhwhite/e171b8fef88c0cbb043b to your computer and use it in GitHub Desktop.
Geocode and forecast
require 'forecast_io'
require 'geocoder'
require 'awesome_print'
require 'open-uri'
# Get public IP Address
remote_ip = open('http://whatismyip.akamai.com').read
#puts remote_ip
# Get geo data based on IP
result = Geocoder.search(remote_ip.to_s)
# Get lat and long coordinates out of the result object
lat = result[0].latitude
long = result[0].longitude
# Self explanatory?
ForecastIO.api_key = 'API_KEY'
# Forecast data based on lat and long
cast = ForecastIO.forecast(lat, long)
#Get current, day, and hourly data
currently = cast.currently
by_day = cast.daily
hourly = cast.hourly
# Get sunrise and sunset info returned in Unix time format and convert that to Ruby date/time using Time.at
sunrise = Time.at(by_day.data[0].sunriseTime)
sunset = Time.at(by_day.data[0].sunsetTime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment