Skip to content

Instantly share code, notes, and snippets.

@alindeman
Created January 11, 2011 16:55
Show Gist options
  • Select an option

  • Save alindeman/774704 to your computer and use it in GitHub Desktop.

Select an option

Save alindeman/774704 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'nokogiri'
require 'open-uri'
class Ride4Ruby
def find_distance_between_farthest
farthest_west, farthest_east = find_farthest_west_and_east
$stderr.puts "Farthest west: #{farthest_west}"
$stderr.puts "Farthest east: #{farthest_east}"
result = Nokogiri::XML.parse(open("http://maps.google.com/maps/api/directions/xml?origin=#{farthest_east.latitude},#{farthest_east.longitude}&destination=#{farthest_west.latitude},#{farthest_west.longitude}&sensor=false&key=#{API_KEY}"))
result.xpath('//route/leg/distance/text').first.content
end
private
Location = Struct.new(:latitude, :longitude)
API_KEY = '#INSERT KEY HERE#'
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}
def find_farthest_west_and_east
find_ruby_streets if @ruby_streets.nil?
@ruby_streets.minmax_by(&:longitude)
end
def find_ruby_streets
@ruby_streets = []
STATES.each do |state|
result = Nokogiri::XML.parse(open("http://maps.google.com/maps/geo?q=#{state}%20Ruby%20Street&output=xml&key=#{API_KEY}"))
result.xpath('//xmlns:Placemark').each do |placemark|
next unless placemark.xpath('xmlns:address').first.content =~ /USA$/
points = placemark.xpath('xmlns:Point/xmlns:coordinates').first.content.split(',')
# Create Location object
@ruby_streets.push Location.new(points[1].to_f, points[0].to_f)
end
end
end
end
ride4ruby = Ride4Ruby.new
puts ride4ruby.find_distance_between_farthest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment