In this project, we'll write a Ruby program that uses the Google Maps API and Dark Sky API to tell the user whether or not they should carry an umbrella with them when they leave home.
There is a solution in the file called solution.rb
.
-
You can run it to see how the program should behave:
ruby solution.rb
-
Before it will work, you need to create two environment variables in your Gitpod dashboard.
- Read how to create environment variables here.
- You need to create env vars called
GMAPS_KEY
andDARK_SKY_KEY
. You'll find the values to assign in the assignment in Canvas. - When asked for "Organization/Repository", say
*/*
. This will make the env vars available across all of your workspaces. - Don't forget to restart your workspace after the variables have been saved
- In your Gitpod dashboard, find the workspace.
- Click on the three dots next to its name.
- Stop.
- Once it has shut down, Open it again.
-
Then, try running
solution.rb
and enter some rainy locations — you should be able to find some using this live radar. -
Don't peek at the solution until you've tried things yourself.
Here is a suggested outline for your program:
-
Ask the user for their location.
-
Get and store the user's location.
-
Get the user's latitude and longitude from the Google Maps API.
-
Get the weather at the user's coordinates from the Dark Sky API.
-
Display the current temperature and summary of the weather for the next hour.
-
For each of the next twelve hours, check if the precipitation probability is greater than 10%.
- If so, print a message saying how many hours from now and what the precipitation probability is.
-
If any of the next twelve hours has a precipitation probability greater than 10%, print "You might want to carry an umbrella!"
If not, print "You probably won't need an umbrella today."
Some handy links:
-
Hoppscotch: a great, free, online tool that makes it easy to experiment with API calls.
-
Tools to format/indent JSON to make it easier to read:
- JSONVue Chrome extension: this is what I use but you have to install it in Chrome.
- Online JSON Viewer: you don't have to install anything, you can copy-paste the JSON into it.
-
Dark Sky forecast at the Merchandise Mart for machines:
https://api.darksky.net/forecast/REPLACE_THIS_PATH_SEGMENT_WITH_YOUR_API_TOKEN/41.8887,-87.6355
You'll need an access token to view this page. Find it in the assignment on Canvas.
-
Map of Merchandise Mart for machines:
https://maps.googleapis.com/maps/api/geocode/json?address=Merchandise%20Mart%20Chicago&key=REPLACE_THIS_QUERY_STRING_PARAMETER_WITH_YOUR_API_TOKEN
You'll need an access token to view this page. Find it in the assignment on Canvas.
Most of working with APIs boils down to working with Array
s and Hash
es.
You will likely also need to use if
statements and loops (most useful programs do).
Here are some less familiar methods that will be useful:
-
URI.open()
: The argument toURI.open
should be aString
containing a URL. The method will then read the page at the provided URL and return it as aTempfile
.- In order to use this method, we must
require "open-uri"
.
- In order to use this method, we must
-
Tempfile#read
: If you call.read
on an instance ofTempfile
, it will return the contents of the file as aString
. -
JSON.parse()
: The argument toJSON.parse
should be aString
containing valid JSON. The method will transform the JSON objects into Ruby objects.- In order to use this method, we must
require "json"
.
- In order to use this method, we must
-
Time.at()
: The argument toTime.at
should be anInteger
representing the Epoch time. The method will transform theInteger
into an instance ofTime
.You could also try this online epoch time converter.
-
You can use a
Range
along with the[]
method to access a specific set of elements within anArray
:array = [8, 3, 1, 19, 23, 3] p array[2..4] # => [1, 19, 23]
Use the ascii_charts gem to produce output that includes a chart, like this:
========================================
Will you need an umbrella today?
========================================
Where are you?
brooklyn
Checking the weather at Brooklyn....
Your coordinates are 40.6781784, -73.9441579.
It is currently 51.33°F.
Next hour: Possible light rain starting in 25 min.
Hours from now vs Precipitation probability
80|
75| *
70| *
65| *
60| *
55| *
50| *
45| *
40| *
35| *
30| *
25| * * *
20| * * * *
15| * * * * * * * *
10| * * * * * * * * *
5| * * * * * * * * * * * *
0+-*--*--*--*--*--*--*--*--*--*--*--*-
1 2 3 4 5 6 7 8 9 10 11 12
You might want to take an umbrella!