Skip to content

Instantly share code, notes, and snippets.

@bokmann
Last active August 29, 2015 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bokmann/7c99a7b3dfe67c134942 to your computer and use it in GitHub Desktop.
Save bokmann/7c99a7b3dfe67c134942 to your computer and use it in GitHub Desktop.
The "Gasoline vs. Petrol" Exercises
These are three exercises that deal with the thought process of converting data from one format to another,
a problem we enounter often in software development. The last problem is about publishing this data, both
as a website and as a webservice that publishes json. Tackling that problem will make you think about
various things like "how do we get the data off of the websites we're referencing?" and "when do we update
that data?" that can lead to great architectural and structural conversations.
Exercise #1:
In the U.S., we measure fuel efficiency of automobiles in miles per gallon. In the U.K., they measure fuel
efficiency in liters per 100 km. Lets write a function to convert one to the other. like this:
My Honda Accord gets about 29 mpg. To convert that to liters per 100 km, I'd like to have a function that
lets me do this:
uk_efficiency = UStoUKEfficiency(29.0)
#uk_efficiency == 8.11
You can generate test data here:
http://www.markporthouse.net/rangie/fuelconsumptionconversion.htm
Bonus: Write the reciprocal function, converting UK to US.
Exercise #2:
you can get the average fuel price per gallon in the US by state here:
http://www.gasbuddy.com/US
or from AAA:
http://fuelgaugereport.aaa.com/
and the average fuel price by county in England from here:
http://www.fleetnews.co.uk/costs/fuel-prices/
from google, you can get currency conversion rates by searching for "1 dollar to pounds"
write a program that asks for (by keyboard input):
the U.S. location name
the U.S. price for gas
the U.K. location name
the U.K. price for gas
the current conversion rate
and it gives you a sentence back like:
"The price of gas in Virginia. is 117% more expensive than the price of gas in Oxford." - or whatever the
true answer is. You can change the structure of this answer as long at it conveys the point.
Exercise #3:
Build a website / web service that publishes the data we calculate in exercise 2. This is an open-ended
task - you could do this in Rails, Sinatra, or several other ways.
Step 1:
Write an app that displays a web page with a dropdown menu of State names and a dropdown menu of UK
Counties. Allow the user to select both, submit, and get an answer back.
In order to do this, you'll need access to the data we hand-entered from the website above. I don't know
of a good, up to date, usable web service for U.S. and U.K. data, so we are going to use a technique
called 'screen scraping' to get the data (we basically get the html data as a string and parse the data
we want out of it. It works, but breaks if the website ever changes the structure of their page. This
is ugly, but there are plenty of times in the real world where we resort to this technique). Here are
two great entries on using screen scraping in Ruby using a couple of different tools:
http://thaiwood.io/screen-scraping-with-a-saw-a-nokogiri-tutorial-with-examples/
http://ngauthier.com/2014/06/scraping-the-web-with-ruby.html
You'll also need the conversation rate data. We *do* have a web service for that, so you can use any
of several different methods to get the data, documented here:
http://www.webservicex.net/currencyconvertor.asmx?op=ConversionRate
I'd suggest using the RESTful 'GET' approach, like this, but you can use any technique there you can
figure out.
http://www.webservicex.net/currencyconvertor.asmx/ConversionRate?FromCurrency=USD&ToCurrency=GBP
Step 2:
Now that we have a functional website, lets turn it into a web service that returns json, just like
the currency conversion... something like this:
http://localhost:3000/PriceComparator?state=Virginia&county=Oxford
Note that there is a lot of freedom here and a lot of good discussion about how we should structure
this app. Once we figure out the screen scraping, when do we do it? How do we store it? When do
we refresh it?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment