Skip to content

Instantly share code, notes, and snippets.

@heliostatic
Created January 11, 2011 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heliostatic/775331 to your computer and use it in GitHub Desktop.
Save heliostatic/775331 to your computer and use it in GitHub Desktop.
The least OO of all possible solutions.
# Ride4Ruby challenge
# details: http://www.engineyard.com/blog/2011/january-contest-ride4ruby/
require 'open-uri'
require 'JSON'
require 'pp'
require 'net/http'
States = %w{Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware District of Columbia Florida Georgia Idaho Illinois Indiana Iowa Kansas Kentucky Louisiana Maine Maryland Massachusetts Michigan Minnesota Mississippi Missouri Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota Ohio Oklahoma Oregon Pennsylvania Rhode Island South Carolina South Dakota Tennessee Texas Utah Vermont Virginia Washington West Virginia Wisconsin Wyoming}
API_KEY = "YOUR KEY HERE"
@places = {}
States.each do |state|
data = JSON.parse(Net::HTTP.get_response(URI.parse("http://maps.google.com/maps/geo?q=#{state}%20Ruby%20Street&output=json&key=#{API_KEY}")).body, {:symbolize_names => true})
data[:Placemark].each do |place|
address = place[:address]
lon = place[:Point][:coordinates][0]
lat = place[:Point][:coordinates][1]
if address =~ /USA/ then
@places[lon] = {:lat => lat, :lon => lon, :address => address}
end
end
end
@places_sorted = @places.sort
@farthest_west = @places_sorted.first
@farthest_east = @places_sorted.last
puts "Farthest west: #{@farthest_west}"
puts "Farthest east: #{@farthest_east}"
result = JSON.parse(Net::HTTP.get_response(URI.parse("http://maps.google.com/maps/api/directions/json?origin=#{@farthest_east[1][:lat]},#{@farthest_east[1][:lon]}&destination=#{@farthest_west[1][:lat]},#{@farthest_west[1][:lon]}&sensor=false&key=#{API_KEY}")).body)
pp result["routes"][0]["legs"][0]["distance"]["text"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment