Class: Mapbox::Matrix

Inherits:
Object
  • Object
show all
Extended by:
HashUtils
Includes:
APIOperations::Request
Defined in:
lib/mapbox/matrix.rb

Class Method Summary collapse

Methods included from HashUtils

xy_from_hash

Methods included from APIOperations::Request

included

Class Method Details

.assemble_params(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/mapbox/matrix.rb', line 6

def self.assemble_params(options={})
  opts = options.dup

  self.joinArrayParam(opts, :destinations)
  self.joinArrayParam(opts, :annotations, ',')
  self.joinArrayParam(opts, :approaches)
  self.joinArrayParam(opts, :sources)

  return "?#{URI.encode_www_form(opts)}"
end

.joinArrayParam(opts, name, joinSymbol = ';') ⇒ Object



17
18
19
20
21
# File 'lib/mapbox/matrix.rb', line 17

def self.joinArrayParam(opts, name, joinSymbol = ';')
  if opts[name].kind_of?(Array)
    opts[name] = opts[name].join(joinSymbol)
  end
end

.matrix(waypoints, profile, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/mapbox/matrix.rb', line 23

def self.matrix(waypoints, profile, options={})
  formatted_waypoints = waypoints.map {|p| xy_from_hash(p).join ','}.join ';'
  params = self.assemble_params(options)
  return request(
      :get,
      "/directions-matrix/v1/mapbox/#{profile}/#{formatted_waypoints}.json#{params}",
      nil)
end