Class: GoogleDistanceMatrix::Matrix

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/google_distance_matrix/matrix.rb

Overview

Public: Represents a distance matrix.

Enables you to set up a origins and destinations and get a distance matrix from Google. For documentation see developers.google.com/maps/documentation/distancematrix

Examples

origin_1 = GoogleDistanceMatrix::Place.new address: "Karl Johans gate, Oslo"
origin_2 = GoogleDistanceMatrix::Place.new address: "Askerveien 1, Asker"

destination_1 = GoogleDistanceMatrix::Place.new address: "Drammensveien 1, Oslo"
destination_2 = GoogleDistanceMatrix::Place.new lat: 1.4, lng: 1.3

matrix = GoogleDistanceMatrix::Matrix.new(
  origins: [origin_1, origin_2],
  destinations: [destination_1, destination_2]
)

You may configure the matrix. See GoogleDistanceMatrix::Configuration for options.

matrix.configure do |config|
  config.sensor = true
  config.mode = "walking"
end

You can set default configuration by doing: GoogleDistanceMatrix.configure_defaults { |c| c.sensor = true }

Query API and get the matrix back

matrix.data   # Returns a two dimensional array.
              # Rows are ordered according to the values in the origins.
              # Each row corresponds to an origin, and each element within that row corresponds to
              # a pairing of the origin with a destination.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Matrix

Returns a new instance of Matrix.



48
49
50
51
52
53
54
# File 'lib/google_distance_matrix/matrix.rb', line 48

def initialize(attributes = {})
  attributes = attributes.with_indifferent_access

  @origins = Places.new attributes[:origins]
  @destinations = Places.new attributes[:destinations]
  @configuration = attributes[:configuration] || GoogleDistanceMatrix.default_configuration.dup
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



46
47
48
# File 'lib/google_distance_matrix/matrix.rb', line 46

def configuration
  @configuration
end

#destinationsObject (readonly)

Returns the value of attribute destinations.



46
47
48
# File 'lib/google_distance_matrix/matrix.rb', line 46

def destinations
  @destinations
end

#originsObject (readonly)

Returns the value of attribute origins.



46
47
48
# File 'lib/google_distance_matrix/matrix.rb', line 46

def origins
  @origins
end

Instance Method Details

#configure {|configuration| ... } ⇒ Object

Yields:



86
87
88
# File 'lib/google_distance_matrix/matrix.rb', line 86

def configure
  yield configuration
end

#dataObject

Public: The data for this matrix.

Returns a two dimensional array, the matrix’s data



66
67
68
# File 'lib/google_distance_matrix/matrix.rb', line 66

def data
  @data ||= load_matrix
end

#inspectObject



95
96
97
98
99
100
101
# File 'lib/google_distance_matrix/matrix.rb', line 95

def inspect
  attributes = %w[origins destinations]
  attributes << "data" if loaded?
  inspection = attributes.map { |a| "#{a}: #{public_send(a).inspect}" }.join ', '

  "#<#{self.class} #{inspection}>"
end

#loaded?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/google_distance_matrix/matrix.rb', line 80

def loaded?
  @data.present?
end

#reloadObject



70
71
72
73
74
# File 'lib/google_distance_matrix/matrix.rb', line 70

def reload
  clear_from_cache!
  @data = load_matrix
  self
end

#reset!Object



76
77
78
# File 'lib/google_distance_matrix/matrix.rb', line 76

def reset!
  @data = nil
end

#urlObject



90
91
92
# File 'lib/google_distance_matrix/matrix.rb', line 90

def url
  UrlBuilder.new(self).url
end