Adding Postgres and ActiveRecord
Getting started - adding dependencies to your project
Guides, tutorials and labs to accompany CMU's Programming for Online Prototypes (49-714).
Everything you need to know about building microservices for the web with Ruby and Sinatra.
DarkSky’s Forecast Gem is a gem for retrieving data from the Forecast Weather API.
It’s an incredibly useful gem that allows you to get lots of awesome stuff about the weather.
https://darksky.net/forecast/40.4439,-79.9561/us12/en
The documentation for the API (“easiest, most advanced, weather API on the web”) can be found at: https://darksky.net/dev
You can make 1000 calls per day for free (which is awesome). But it does start to cost $0.00001 per request after that point. 1000 calls per day should be more than enough to let you prototype with weather info…
Visit https://darksky.net/dev and register
You’ll be given a secret key (a big long string).
Add this to your projects .env
file:
DARK_SKY_SECRET=XXXXXXX
And don’t forget to add it to your heroku instance too.
The API is dead simple. You just need to call a url like so:
https://api.darksky.net/forecast/[DEVELOPER_SECRET_KEY]/[LAT],[LON]
And then you’ll get a super long response containing a JSON formatted string that has current weather info, prediction for the next hour and day as well as weather alerts for the area (flood warnings, etc.)
You can take a look at the full response in the docs
Thankfully they also provide a Gem which makes it even easier to work with the API and gives us native ruby responses. In one line of code you can use the API to look up weather for any location: forecast = ForecastIO.forecast(37.8267, -122.423)
In your Gemfile
add:
gem 'forecast_io'
Install it with bundle install
Then in your app
require "sinatra"
require 'sinatra/reloader' if development?
require 'forecast_io'
get 'get_weather/:lat/:lon' do
weather = get_weather_for 40.4406, 79.9959
puts weather["currently"]
"It's currently #{weather["currently"]["summary"]} with a temperature of #{weather["currently"]["temperature"]} degrees and a wind speed of #{weather["currently"]["windSpeed"]}."
end
def get_weather_for lat, lon
ForecastIO.api_key = ENV['DARK_SKY_WEATHER_API']
forecast = ForecastIO.forecast(lat, lon)
forecast
end