Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor in order to mock xml call from an app

  • Loading branch information...
commit d38aaf2cbac950434ed5f13c1f47aa40147cb23e 1 parent 8c04d3f
Josh Crews authored
Showing with 13 additions and 10 deletions.
  1. +1 −1  Rakefile
  2. +2 −2 google_directions.gemspec
  3. +10 −7 lib/google_directions.rb
View
2  Rakefile
@@ -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"
View
4 google_directions.gemspec
@@ -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{josh@joshcrews.com}
s.extra_rdoc_files = ["README.textile", "lib/google_directions.rb"]
View
17 lib/google_directions.rb
@@ -10,7 +10,6 @@ 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
@@ -18,13 +17,17 @@ def initialize(location_1, location_2)
#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
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.