cd ~/workspaceand hit enter. That should get you back to the starting point.
cd ~and hit enter. This command should take you to the home folder of your workspace.
touch .bash_profileand hit enter. This command creates a hidden file called
.bash_profilein your home folder.
ls -aand hit enter. You'll see a list of all the files in your current directory, including hidden files (the ones whose names start with a
.bash_profileand click it. The file should open up in your editor.
cd ~/workspaceto go back to your main folder, or just close that tab.
cd ~, hit enter, then type
ls -a, hit enter and click on the file to open it.
.bash_profilefile become key-value pairs in a special Hash that we can access anywhere in our Rails environment called
ENV. For example, to access this sensitive info, in a NEW Terminal tab (it won't work in old tabs) we can open a
rails consoleand type in:
YOUR_API_KEYwith the one provided to you. Paste that URL into a Chrome tab; you should see something like this:
address_componentssection to make the value of the
geometrykey stand out, since that is where our target information lives:
lngkeys within the
locationhash. Notice that JSON uses curly braces for Hashes and square brackets for Arrays just like Ruby does.
?address=with the address we want geocoded:
+s. If you tried typing the address with spaces in it, you'll have noticed that Google encodes spaces automatically with
open()method to read Google's page. The
open()method takes one
Stringargument, which should contain the URL of the page you want to open. I'm going to copy-paste the URL within
" "and store it in a variable
urlto make it easier to work with:
openthat URL and
readthe body of the page:
rails console, you can use return to scroll one line at a time, Space to scroll one page at a time, or Q to just get back to the prompt to enter a new Ruby expression.
opened the page at the location in
url, and the return value was the HTTP response. The HTTP response is actually a complicated object, with headers and status codes and other things we haven't talked about yet.
.readmethod to pull that out. However, we just dropped that string on the ground; let's instead store the result in a variable called
"lat", perhaps. But then what? We could probably figure it out, but there's a much better way.
JSON, similar to the
CSVclass, which makes parsing a string that has data in JSON format a snap:
geometrykey. We can remind ourselves what keys are in the hash:
fthat has a key called
"geometry", which, as we learned from our initial research above, is what contains our target. Let's keep going:
digthat can help us drill down into nested Hash/Array structures like this a bit more concisely:
longitudethat I need.
bin/serverand navigate to the homepage in a Chrome tab.
app/controllers/geocoding_controller.rb. Your job is to write some code in the
street_to_coordsmethod, where indicated, and put the correct value in the
5807 S Woodlawn Aveat the Street → Coords form, I should see something like
app/controllers/forecast_controller.rb, you will do something similar; but instead of using Google's Geocoding API, you will use The Forecast API. We will exchange a latitude/longitude pair for weather information. Forecast is an amazing API that gives you minute-by-minute meteorological information.
street_to_coordsand try it out.
coords_to_weathermethod, where indicated, and put the correct value in the instance variables at the end.
-87.59at the Coords → Weather form, I should see something like
nils. If you run into issues, using
digmight be helpful, since it doesn't throw an error if a key is missing along the way:
app/controllers/meteorologist_controller.rb. Use both the Google Geocoding API and the Forecast API so that if I type in
5807 S Woodlawn Aveat the Street → Weather form, I should see something like
rails grade:allat a Terminal prompt when you're ready for feedback and your score. You can run it as many times as you want.
<link>to Bootstrap or a Bootswatch in the
<head>of your pages (located in
app/views/layouts/application.html.erb), and make things look prettier.