Created
January 12, 2011 02:52
-
-
Save afeld/775611 to your computer and use it in GitHub Desktop.
solution for 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
# Ride4Ruby solution by Aidan Feldman | |
# [email protected] | |
# contest: http://www.engineyard.com/blog/2011/january-contest-ride4ruby/ | |
# 1/11/10 | |
require 'rubygems' | |
require 'rest_client' | |
require 'active_support' | |
require 'active_support/core_ext' | |
GMAPS_KEY = ENV['GMAPS_KEY'] | |
STATES = %Q(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).split("\n") | |
class Place | |
attr_accessor :city, :state, :zip, :lat, :lng | |
def self.from_gmaps_hash(hash) | |
place = Place.new | |
place.state = hash['AddressDetails']['Country']['AdministrativeArea']['AdministrativeAreaName'] | |
place.city = hash['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['LocalityName'] | |
place.zip = hash['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['PostalCode']['PostalCodeNumber'] | |
place.lat, place.lng = hash['Point']['coordinates'].split(',').collect{|p| p.to_f} | |
place | |
end | |
end | |
places = [] | |
STATES.each do |state| | |
xml = RestClient.get "http://maps.google.com/maps/geo?q=#{URI.escape(state)}%20Ruby%20Street&output=xml&key=#{GMAPS_KEY}" | |
hash = Hash.from_xml xml | |
placemarks = hash['kml']['Response']['Placemark'] rescue [] | |
placemarks.each do |place_hash| | |
place = Place.from_gmaps_hash(place_hash) rescue nil | |
places << place unless place.nil? | |
end | |
end | |
min, max = places.minmax{|a,b| a.lat <=> b.lat} | |
puts "furthest west: #{min.city}, #{min.state}" | |
puts "furthest east: #{max.city}, #{max.state}" | |
xml = RestClient.get "http://maps.google.com/maps/api/directions/xml?origin=#{max.lng},#{max.lat}&destination=#{min.lng},#{min.lat}&sensor=false&key=#{GMAPS_KEY}" | |
hash = Hash.from_xml xml | |
distance = hash['DirectionsResponse']['route']['leg']['distance']['text'] | |
puts "Distance: #{distance}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment