Created
January 11, 2011 21:21
-
-
Save anolson/775168 to your computer and use it in GitHub Desktop.
January Contest: Ride4Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'rexml/document' | |
module Ride4Ruby | |
GOOGLE_MAPS_API_KEY = "your google key" | |
@states = ["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"] | |
class Location | |
attr_accessor :address, :lng | |
def initialize(options = {}) | |
options.each do |name, value| | |
send("#{name}=", value) | |
end | |
end | |
def is_farther_east_than(location) | |
self.lng > location.lng | |
end | |
def is_farther_west_than(location) | |
self.lng < location.lng | |
end | |
def distance_between(location) | |
uri = "http://maps.google.com/maps/api/directions/xml?origin=#{URI.escape @address}&destination=#{URI.escape location.address}&sensor=false&key=#{GOOGLE_MAPS_API_KEY}" | |
get_document(uri).elements["/DirectionsResponse/route/leg/distance/text"].text.to_s | |
end | |
def self.find_all(state) | |
uri = "http://maps.google.com/maps/geo?q=#{URI.escape state}%20Ruby%20Street&output=xml&key=#{GOOGLE_MAPS_API_KEY}" | |
locations = [] | |
get_document(uri).each_element("//Placemark") do |e| | |
country = e.elements["./AddressDetails/Country/CountryName"].text.to_s | |
locations << Location.new( | |
{ :address => e.elements["./address"].text.to_s, | |
:lng => e.elements[".//coordinates"].text.to_s.split(',').first.to_f }) if country == "USA" | |
end | |
locations | |
end | |
def get_document(uri) | |
self.class.get_document(uri) | |
end | |
def self.get_document(uri) | |
response = Net::HTTP.get(URI.parse(uri)) | |
doc = REXML::Document.new(response) | |
end | |
end | |
def self.total_route_distance | |
farthest_east, farthest_west = Location.new({:address => "", :lng => -180.0}), Location.new({:address => "", :lng => 0.0}) | |
@states.each do |state| | |
hops = Location.find_all(state) | |
hops.each do |hop| | |
farthest_east = hop if(hop.is_farther_east_than(farthest_east)) | |
farthest_west = hop if(hop.is_farther_west_than(farthest_west)) | |
end | |
end | |
farthest_east.distance_between(farthest_west) | |
end | |
end | |
puts "The total distance of the Ride4Ruby is #{Ride4Ruby.total_route_distance}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment