class MapboxDirections::ResponseParser

Public Class Methods

directions(body) click to toggle source
# File lib/mapbox_directions/response_parser.rb, line 5
def self.directions(body)
  new(body).directions
end
new(body) click to toggle source
# File lib/mapbox_directions/response_parser.rb, line 9
def initialize(body)
  @body = body
end

Public Instance Methods

directions() click to toggle source
# File lib/mapbox_directions/response_parser.rb, line 13
def directions
  Response.new(
    origin:      origin,
    destination: destination,
    waypoints:   @body["waypoints"],
    routes:      routes,
    message:     @body["message"],
    error:       @body["error"]
  )
end

Private Instance Methods

destination() click to toggle source
# File lib/mapbox_directions/response_parser.rb, line 30
def destination
  Location.from_geojson(@body["destination"]) if @body["destination"]
end
maneuver(maneuver) click to toggle source
# File lib/mapbox_directions/response_parser.rb, line 60
def maneuver(maneuver)
  coordinates = maneuver["location"]["coordinates"]
  Maneuver.new(
    type:        maneuver["type"],
    location:    Point.new(coordinates.last, coordinates.first),
    instruction: maneuver["instruction"]
  )
end
origin() click to toggle source
# File lib/mapbox_directions/response_parser.rb, line 26
def origin
  Location.from_geojson(@body["origin"]) if @body["origin"]
end
routes() click to toggle source
# File lib/mapbox_directions/response_parser.rb, line 34
def routes
  return [] unless @body["routes"] && @body["routes"].any?
  @body["routes"].map do |route|
    Route.new(
      distance: route["distance"],
      duration: route["duration"],
      summary:  route["summary"],
      geometry: route["geometry"],
      steps:    steps(route["steps"])
    )
  end
end
steps(steps) click to toggle source
# File lib/mapbox_directions/response_parser.rb, line 47
def steps(steps)
  steps.map do |step|
    Step.new(
      distance:  step["distance"],
      duration:  step["duration"],
      way_name:  step["way_name"],
      direction: step["direction"],
      heading:   step["heading"],
      maneuver:  maneuver(step["maneuver"])
    )
  end
end