Class: GoogleDistanceMatrix::Matrix
- Inherits:
-
Object
- Object
- GoogleDistanceMatrix::Matrix
- 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
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#destinations ⇒ Object
readonly
Returns the value of attribute destinations.
-
#origins ⇒ Object
readonly
Returns the value of attribute origins.
Instance Method Summary collapse
- #configure {|configuration| ... } ⇒ Object
-
#data ⇒ Object
Public: The data for this matrix.
-
#initialize(attributes = {}) ⇒ Matrix
constructor
A new instance of Matrix.
- #inspect ⇒ Object
- #loaded? ⇒ Boolean
- #reload ⇒ Object
- #reset! ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Matrix
Returns a new instance of Matrix.
52 53 54 55 56 57 58 |
# File 'lib/google_distance_matrix/matrix.rb', line 52 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
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
50 51 52 |
# File 'lib/google_distance_matrix/matrix.rb', line 50 def configuration @configuration end |
#destinations ⇒ Object (readonly)
Returns the value of attribute destinations.
50 51 52 |
# File 'lib/google_distance_matrix/matrix.rb', line 50 def destinations @destinations end |
#origins ⇒ Object (readonly)
Returns the value of attribute origins.
50 51 52 |
# File 'lib/google_distance_matrix/matrix.rb', line 50 def origins @origins end |
Instance Method Details
#configure {|configuration| ... } ⇒ Object
90 91 92 |
# File 'lib/google_distance_matrix/matrix.rb', line 90 def configure yield configuration end |
#data ⇒ Object
Public: The data for this matrix.
Returns a two dimensional array, the matrix’s data
72 73 74 |
# File 'lib/google_distance_matrix/matrix.rb', line 72 def data @data ||= load_matrix end |
#inspect ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/google_distance_matrix/matrix.rb', line 94 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
86 87 88 |
# File 'lib/google_distance_matrix/matrix.rb', line 86 def loaded? @data.present? end |
#reload ⇒ Object
76 77 78 79 80 |
# File 'lib/google_distance_matrix/matrix.rb', line 76 def reload clear_from_cache! @data = load_matrix self end |
#reset! ⇒ Object
82 83 84 |
# File 'lib/google_distance_matrix/matrix.rb', line 82 def reset! @data = nil end |