Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Created January 11, 2011 05:46
Show Gist options
  • Save vaskoz/774079 to your computer and use it in GitHub Desktop.
Save vaskoz/774079 to your computer and use it in GitHub Desktop.
January 2011 Engine Yard Ruby Puzzle
require 'net/http'
require 'rexml/document'
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"]
map_key = "#{YOUR GOOGLE MAP API KEY}"
Position = Struct.new(:address, :latitude, :longitude)
western_most = Position.new("", 0.0, 180.0)
eastern_most = Position.new("", 0.0, -180.0)
states.each do |state|
cities_url = "http://maps.google.com/maps/geo?q=#{URI.escape state}%20Ruby%20Street&output=xml&key=#{map_key}"
xml_data = Net::HTTP.get_response(URI.parse(cities_url)).body
doc = REXML::Document.new(xml_data)
doc.elements.each("kml/Response/Placemark") do |place|
next unless place.elements["AddressDetails/Country/CountryNameCode"].text == "US"
coord = place.elements["Point/coordinates"].text.split ','
position = Position.new(place.elements["address"].text, Float(coord[1]), Float(coord[0]))
western_most = position if position.longitude < western_most.longitude
eastern_most = position if position.longitude > eastern_most.longitude
end
end
directions_url = "http://maps.google.com/maps/api/directions/xml?origin=#{eastern_most.latitude},#{eastern_most.longitude}&destination=#{western_most.latitude},#{western_most.longitude}&sensor=false&key=#{map_key}"
xml_data = Net::HTTP.get_response(URI.parse(directions_url)).body
doc = REXML::Document.new(xml_data)
doc.elements.each("DirectionsResponse/route/leg/distance/text") do |distance|
puts "Eastern most address is #{eastern_most.address} and western most address is #{western_most.address} and distance between them is #{distance.text}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment