this is a command line version : # ruby myscript.rb "Lyon" "Paris"
You can easily change it for a Ruby on Rails project :)
Maybie we can upgrade this script using regexp.
require "open-uri" require "cgi" # Command line arguments ... $*.each {|argu| argu.gsub!("\"","")} # Defaults towns src = "Lyon" dest = "Paris" # If we have fill two aguments ... if $*.size == 2 then src = $*[0] dest = $*[1] end html = "" url = "http://maps.google.com/maps?f=d&hl=fr&saddr=" + CGI.escape(src) + "&daddr="+ CGI.escape(dest) # Read the html page open(url) {|f| f.each_line {|line| html += line} } pos1 = html.index("voiture") # French version pos2 = html.index("</td>",pos1) unless pos1 == nil pos1 = html.index(">",pos2+6)+1 unless pos2 == nil pos2 = html.index("</td>",pos2+1) -1 unless pos2 == nil ret = "" if pos1 != nil and pos2 != nil ret = html[pos1..pos2].gsub!(" ", " ") else ret = "0" end html = nil p ret