Created
January 11, 2011 19:28
-
-
Save nithinbekal/774967 to your computer and use it in GitHub Desktop.
Ride4Ruby
This file contains hidden or 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
require 'hpricot' | |
require 'net/http' | |
require 'CGI' | |
class Place | |
attr_accessor :city, :state, :zip_code, :longitude, :latitude | |
def initialize city, state, zip_code, latitude, longitude | |
@city, @state, @zip_code = city, state, zip_code | |
@latitude, @longitude = latitude, longitude | |
end | |
def <=> other_place | |
self.latitude <=> other_place.latitude | |
end | |
end | |
city_path = 'addressdetails/country/administrativearea/subadministrativearea/subadministrativeareaname' | |
zip_code_path = 'addressdetails/country/administrativearea/subadministrativearea/locality/postalcode/postalcodenumber' | |
country_path = 'addressdetails/country/countryname' | |
coords_path = 'point/coordinates' | |
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'] | |
places = [] | |
states.each do |state| | |
url = "http://maps.google.com/maps/geo?q=#{CGI.escape(state)}%20Ruby%20Street&output=xml" | |
xml = Net::HTTP.get_response(URI.parse url).body | |
map_data = Hpricot(xml) | |
(map_data/'kml/Response/Placemark').each do |place| | |
city = (place/city_path).inner_html | |
zip_code = (place/zip_code_path).inner_html | |
country = (place/country_path).inner_html | |
latitude, longitude = (place/coords_path).inner_html.split(',').map(&:to_f) | |
places << Place.new(city, state, zip_code, latitude, longitude) if country == "USA" | |
end | |
end | |
places.sort! | |
puts "Eastern : #{places[-1].city}, #{places[-1].state} (#{places[-1].latitude}, #{places[-1].longitude})" | |
puts "Western : #{places[0].city }, #{places[0].state } (#{places[0].latitude}, #{places[0].longitude})" | |
url = "http://maps.google.com/maps/api/directions/xml?origin=#{CGI.escape(places[-1].city + places[-1].state)}&destination=#{CGI.escape(places[0].city + places[0].state)}&sensor=false" | |
xml = Net::HTTP.get_response(URI.parse url).body | |
dist_data = Hpricot(xml) | |
distance = (dist_data/'directionsresponse/route/leg/distance/text').inner_html | |
puts "Distance between the places is #{distance}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment