Skip to content

Commit

Permalink
Refacto and add steps
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Arnoud <[email protected]>
  • Loading branch information
spk committed Sep 20, 2011
1 parent 98f72c9 commit f4dd954
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 47 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source :rubygems

gemspec
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 7 additions & 4 deletions google_directions.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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<nokogiri>, [">= 1.4.1"])
s.add_development_dependency(%q<nokogiri>, [">= 1.5.0"])
s.add_development_dependency(%q<extlib>, [">= 0.9.15"])
else
s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
s.add_dependency(%q<nokogiri>, [">= 1.5.0"])
s.add_dependency(%q<extlib>, [">= 0.9.15"])
end
else
s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
s.add_dependency(%q<nokogiri>, [">= 1.5.0"])
s.add_dependency(%q<extlib>, [">= 0.9.15"])
end
end
69 changes: 31 additions & 38 deletions lib/google_directions.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
13 changes: 8 additions & 5 deletions test/unit/google_directions_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit f4dd954

Please sign in to comment.