Class: GoogleDirections

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#destinationObject (readonly)

Returns the value of attribute destination.



9
10
11
# File 'lib/google_directions.rb', line 9

def destination
  @destination
end

#docObject (readonly)

Returns the value of attribute doc.



9
10
11
# File 'lib/google_directions.rb', line 9

def doc
  @doc
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/google_directions.rb', line 9

def options
  @options
end

#originObject (readonly)

Returns the value of attribute origin.



9
10
11
# File 'lib/google_directions.rb', line 9

def origin
  @origin
end

#statusObject (readonly)

Returns the value of attribute status.



9
10
11
# File 'lib/google_directions.rb', line 9

def status
  @status
end

#xmlObject (readonly)

Returns the value of attribute xml.



9
10
11
# File 'lib/google_directions.rb', line 9

def xml
  @xml
end

Instance Method Details

#distanceObject

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_milesObject



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_textObject



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_minutesObject



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_urlObject



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

#stepsObject



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_callObject



31
32
33
# File 'lib/google_directions.rb', line 31

def xml_call
  @url
end