Class: GoogleDirections
- Inherits:
-
Object
- Object
- GoogleDirections
- Defined in:
- lib/google_directions.rb
Constant Summary collapse
- @@base_url =
'http://maps.googleapis.com/maps/api/directions/xml'
- @@default_options =
{ :language => :en, :alternative => :true, :sensor => :false, :mode => :driving, }
Instance Attribute Summary collapse
-
#destination ⇒ Object
readonly
Returns the value of attribute destination.
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#origin ⇒ Object
readonly
Returns the value of attribute origin.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Instance Method Summary collapse
-
#distance ⇒ Object
the distance.value field always contains a value expressed in meters.
- #distance_in_miles ⇒ Object
- #distance_text ⇒ Object
- #drive_time_in_minutes ⇒ Object
-
#initialize(origin, destination, opts = @@default_options) ⇒ GoogleDirections
constructor
A new instance of GoogleDirections.
- #public_url ⇒ Object
- #steps ⇒ Object
- #xml_call ⇒ Object
Constructor Details
#initialize(origin, destination, opts = @@default_options) ⇒ GoogleDirections
Returns a new instance of GoogleDirections.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/google_directions.rb', line 20 def initialize(origin, destination, opts=@@default_options) @origin = origin @destination = destination @options = opts.merge({:origin => transcribe(@origin), :destination => transcribe(@destination)}) @url = @@base_url + '?' + @options.to_query @xml = open(@url).read @doc = Nokogiri::XML(@xml) @status = @doc.css('status').text end |
Instance Attribute Details
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
9 10 11 |
# File 'lib/google_directions.rb', line 9 def destination @destination end |
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
9 10 11 |
# File 'lib/google_directions.rb', line 9 def doc @doc end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/google_directions.rb', line 9 def @options end |
#origin ⇒ Object (readonly)
Returns the value of attribute origin.
9 10 11 |
# File 'lib/google_directions.rb', line 9 def origin @origin end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
9 10 11 |
# File 'lib/google_directions.rb', line 9 def status @status end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
9 10 11 |
# File 'lib/google_directions.rb', line 9 def xml @xml end |
Instance Method Details
#distance ⇒ Object
the distance.value field always contains a value expressed in meters.
48 49 50 51 52 53 54 55 |
# File 'lib/google_directions.rb', line 48 def distance return @distance if @distance unless @status == 'OK' @distance = 0 else @distance = @doc.css("distance value").last.text end end |
#distance_in_miles ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/google_directions.rb', line 66 def distance_in_miles if @status != "OK" distance_in_miles = 0 else meters = distance distance_in_miles = (meters.to_f / 1610.22).round distance_in_miles end end |
#distance_text ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/google_directions.rb', line 57 def distance_text return @distance_text if @distance_text unless @status == 'OK' @distance_text = "0 km" else @distance_text = @doc.css("distance text").last.text end end |
#drive_time_in_minutes ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/google_directions.rb', line 38 def drive_time_in_minutes if @status != "OK" drive_time = 0 else drive_time = @doc.css("duration value").last.text convert_to_minutes(drive_time) end end |
#public_url ⇒ Object
76 77 78 |
# File 'lib/google_directions.rb', line 76 def public_url "http://maps.google.com/maps?saddr=#{transcribe(@origin)}&daddr=#{transcribe(@destination)}&hl=#{@options[:language]}&ie=UTF8" end |
#steps ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/google_directions.rb', line 80 def steps if @status == 'OK' @doc.css('html_instructions').map {|a| a.text } else [] end end |
#xml_call ⇒ Object
31 32 33 |
# File 'lib/google_directions.rb', line 31 def xml_call @url end |