Class: TollBooth::Route

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/toll_booth/route.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#distanceObject

Returns the value of attribute distance.



4
5
6
# File 'lib/toll_booth/route.rb', line 4

def distance
  @distance
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/toll_booth/route.rb', line 4

def name
  @name
end

#stepsObject

Returns the value of attribute steps.



4
5
6
# File 'lib/toll_booth/route.rb', line 4

def steps
  @steps
end

#travel_timeObject

Returns the value of attribute travel_time.



4
5
6
# File 'lib/toll_booth/route.rb', line 4

def travel_time
  @travel_time
end

Class Method Details

.find(origin, destinations) ⇒ TollBooth::RouteCollection

get driving directions from the origin to the specified destination(s)

Parameters:

  • origin (TollBooth::Location)

    the starting point for the trip

  • destinations (Array)

    a list of destinations to drive to

Returns:



16
17
18
19
20
21
22
23
# File 'lib/toll_booth/route.rb', line 16

def find(origin, destinations)
  response = get("", 
    :query => {:q => "from: #{origin.description} " + 
      destinations.collect{|d| " to: #{d.description}"}.join("")}) 
  routes = parse(response)

  routes
end

.parse(response) ⇒ TollBooth::RouteCollection

parses response from google

Parameters:

  • the

    response received from google

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/toll_booth/route.rb', line 28

def parse(response)
  resp = JSON.parse(response.body)

  code = response["Status"]["code"] 
  routes = TollBooth::RouteCollection.new
  if code == 200
    resp["Directions"]["Routes"].each do |r|
      route = new 
      route.distance = r["Distance"]["html"].gsub(" mi", "").to_f
      route.travel_time = ChronicDuration.parse(r["Duration"]["html"])
      route.steps = TollBooth::RouteStep.parse(r["Steps"])

      routes << route
    end 

  else
    routes.errors = [error_for(code)]
  end
  routes
end