Skip to content

Commit

Permalink
refactor in order to mock xml call from an app
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Crews committed Jul 21, 2010
1 parent 8c04d3f commit d38aaf2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require 'rubygems'
require 'rake'
require 'echoe'

Echoe.new('google_directions', '0.1.2') do |p|
Echoe.new('google_directions', '0.1.3') do |p|
p.description = "Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places"
p.url = "http://github.com/joshcrews/Google-Directions-Ruby"
p.author = "Josh Crews"
Expand Down
4 changes: 2 additions & 2 deletions google_directions.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Gem::Specification.new do |s|
s.name = %q{google_directions}
s.version = "0.1.2"
s.version = "0.1.3"

s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Josh Crews"]
s.date = %q{2010-07-15}
s.date = %q{2010-07-20}
s.description = %q{Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places}
s.email = %q{[email protected]}
s.extra_rdoc_files = ["README.textile", "lib/google_directions.rb"]
Expand Down
17 changes: 10 additions & 7 deletions lib/google_directions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ def initialize(location_1, location_2)
@location_2 = location_2
options = "origin=#{transcribe(@location_1)}&destination=#{transcribe(@location_2)}"
@xml_call = @base_url + options
@xml = get_url(@xml_call)
@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 = Nokogiri::XML(xml)
doc.css("status").text
end

def xml
@xml
end
def xml
unless @xml.nil?
@xml
else
@xml ||= get_url(@xml_call)
end
end

def xml_call
@xml_call
Expand All @@ -34,7 +37,7 @@ def drive_time_in_minutes
if @status != "OK"
drive_time = 0
else
doc = Nokogiri::XML(@xml)
doc = Nokogiri::XML(xml)
drive_time = doc.css("duration value").last.text
convert_to_minutes(drive_time)
end
Expand All @@ -44,7 +47,7 @@ def distance_in_miles
if @status != "OK"
distance_in_miles = 0
else
doc = Nokogiri::XML(@xml)
doc = Nokogiri::XML(xml)
meters = doc.css("distance value").last.text
distance_in_miles = (meters.to_f / 1610.22).round
distance_in_miles
Expand Down

0 comments on commit d38aaf2

Please sign in to comment.