diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a1b93f3 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source :rubygems + +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..e9afc1d --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,18 @@ +PATH + remote: . + specs: + google_directions (0.1.3) + +GEM + remote: http://rubygems.org/ + specs: + extlib (0.9.15) + nokogiri (1.5.0) + +PLATFORMS + ruby + +DEPENDENCIES + extlib (>= 0.9.15) + google_directions! + nokogiri (>= 1.5.0) diff --git a/google_directions.gemspec b/google_directions.gemspec index 62a5e39..0c96522 100644 --- a/google_directions.gemspec +++ b/google_directions.gemspec @@ -17,18 +17,21 @@ Gem::Specification.new do |s| s.rubyforge_project = %q{google_directions} s.rubygems_version = %q{1.3.7} s.summary = %q{Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places} - s.test_files = ["test/test_helper.rb", "test/unit/google_directions_test.rb"] + s.test_files = ["test/test_helper.rb", "test/unit/google_directions_test.rb", "test/mocks/google_directions_samle_xml.xml"] if s.respond_to? :specification_version then current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION s.specification_version = 3 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then - s.add_development_dependency(%q, [">= 1.4.1"]) + s.add_development_dependency(%q, [">= 1.5.0"]) + s.add_development_dependency(%q, [">= 0.9.15"]) else - s.add_dependency(%q, [">= 1.4.1"]) + s.add_dependency(%q, [">= 1.5.0"]) + s.add_dependency(%q, [">= 0.9.15"]) end else - s.add_dependency(%q, [">= 1.4.1"]) + s.add_dependency(%q, [">= 1.5.0"]) + s.add_dependency(%q, [">= 0.9.15"]) end end diff --git a/lib/google_directions.rb b/lib/google_directions.rb index 75c9d6d..f4c3356 100644 --- a/lib/google_directions.rb +++ b/lib/google_directions.rb @@ -1,46 +1,39 @@ # encoding: UTF-8 require 'cgi' require 'net/http' +require 'open-uri' require 'nokogiri' -require 'google_directions' +require 'extlib/hash' class GoogleDirections - - def initialize(location_1, location_2) - @base_url = "http://maps.google.com/maps/api/directions/xml?sensor=false&" - @location_1 = location_1 - @location_2 = location_2 - options = "origin=#{transcribe(@location_1)}&destination=#{transcribe(@location_2)}" - @xml_call = @base_url + options - @status = find_status - end - # an example URL to be generated - #http://maps.google.com/maps/api/directions/xml?origin=St.+Louis,+MO&destination=Nashville,+TN&sensor=false&key=ABQIAAAAINgf4OmAIbIdWblvypOUhxSQ8yY-fgrep0oj4uKpavE300Q6ExQlxB7SCyrAg2evsxwAsak4D0Liiv - - def find_status - doc = Nokogiri::XML(xml) - doc.css("status").text - end + attr_reader :status, :doc, :xml - def xml - unless @xml.nil? - @xml - else - @xml ||= get_url(@xml_call) - end + @@base_url = 'http://maps.googleapis.com/maps/api/directions/xml' + + def initialize(origin, destination, opts={:language => :en, :alternative => :true, :sensor => :false, :mode => :driving}) + @origin = transcribe(origin) + @destination = transcribe(destination) + options = opts.merge({:origin => @origin, :destination => @destination}) + + @url = @@base_url + '?' + options.to_params + @xml = open(@url).read + @doc = Nokogiri::XML(@xml) + @status = @doc.css('status').text end def xml_call - @xml_call + @url end + # an example URL to be generated + #http://maps.google.com/maps/api/directions/xml?origin=St.+Louis,+MO&destination=Nashville,+TN&sensor=false&key=ABQIAAAAINgf4OmAIbIdWblvypOUhxSQ8yY-fgrep0oj4uKpavE300Q6ExQlxB7SCyrAg2evsxwAsak4D0Liiv + def drive_time_in_minutes if @status != "OK" drive_time = 0 else - doc = Nokogiri::XML(xml) - drive_time = doc.css("duration value").last.text + drive_time = @doc.css("duration value").last.text convert_to_minutes(drive_time) end end @@ -49,29 +42,29 @@ def distance_in_miles if @status != "OK" distance_in_miles = 0 else - doc = Nokogiri::XML(xml) - meters = doc.css("distance value").last.text + meters = @doc.css("distance value").last.text distance_in_miles = (meters.to_f / 1610.22).round distance_in_miles end end - - def status - @status + + + def steps + if @status == 'OK' + @doc.css('html_instructions').map {|a| a.text } + else + [] + end end - + private - + def convert_to_minutes(text) (text.to_f / 60).round end - + def transcribe(location) CGI::escape(location) end - def get_url(url) - Net::HTTP.get(::URI.parse(url)) - end - end diff --git a/test/unit/google_directions_test.rb b/test/unit/google_directions_test.rb index 94985fc..776cb9d 100644 --- a/test/unit/google_directions_test.rb +++ b/test/unit/google_directions_test.rb @@ -1,17 +1,14 @@ # encoding: UTF-8 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) +# TODO: mocks class GoogleDirectionsTest < Test::Unit::TestCase - def test_the_truth - assert true - end - def test_happy_case directions = GoogleDirections.new("121 Gordonsville Highway, 37030", "499 Gordonsville Highway, 38563") assert_equal(4, directions.distance_in_miles) assert_equal(6, directions.drive_time_in_minutes) - assert_equal('http://maps.google.com/maps/api/directions/xml?sensor=false&origin=121+Gordonsville+Highway%2C+37030&destination=499+Gordonsville+Highway%2C+38563', directions.xml_call) + assert_equal('http://maps.googleapis.com/maps/api/directions/xml?language=en&alternative=true&sensor=false&mode=driving&origin=121+Gordonsville+Highway%2C+37030&destination=499+Gordonsville+Highway%2C+38563', directions.xml_call) # end_location > lat assert_not_nil(directions.xml =~ /36\.1772300/) end @@ -36,4 +33,10 @@ def test_french_direction directions = GoogleDirections.new("15 rue poissonnière, 75002 Paris", "169 11th Street CA 94103 San Francisco United States") end end + + def test_steps + directions = GoogleDirections.new("rue poissonnière, 75002 Paris", "51 rue de Turbigo, 75003 Paris France") + assert_equal Array, directions.steps.class + assert_equal 3, directions.steps.size + end end