Class: GoogleMaps::Services::DistanceMatrix
- Inherits:
-
Object
- Object
- GoogleMaps::Services::DistanceMatrix
- Defined in:
- lib/googlemaps/services/distancematrix.rb
Overview
Performs requests to the Google Maps Distance Matrix API.
Instance Attribute Summary collapse
-
#client ⇒ Symbol
The HTTP client.
Instance Method Summary collapse
-
#initialize(client) ⇒ DistanceMatrix
constructor
A new instance of DistanceMatrix.
-
#query(origins:, destinations:, mode: nil, language: nil, avoid: nil, units: nil, departure_time: nil, arrival_time: nil, transit_mode: nil, transit_routing_preference: nil, traffic_model: nil, region: nil) ⇒ Hash, Nokogiri::XML::Document
Gets travel distance and time for a matrix of origins and destinations.
Constructor Details
#initialize(client) ⇒ DistanceMatrix
Returns a new instance of DistanceMatrix.
16 17 18 |
# File 'lib/googlemaps/services/distancematrix.rb', line 16 def initialize(client) self.client = client end |
Instance Attribute Details
#client ⇒ Symbol
Returns the HTTP client.
14 15 16 |
# File 'lib/googlemaps/services/distancematrix.rb', line 14 def client @client end |
Instance Method Details
#query(origins:, destinations:, mode: nil, language: nil, avoid: nil, units: nil, departure_time: nil, arrival_time: nil, transit_mode: nil, transit_routing_preference: nil, traffic_model: nil, region: nil) ⇒ Hash, Nokogiri::XML::Document
Gets travel distance and time for a matrix of origins and destinations.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/googlemaps/services/distancematrix.rb', line 40 def query(origins:, destinations:, mode: nil, language: nil, avoid: nil, units: nil, departure_time: nil, arrival_time: nil, transit_mode: nil, transit_routing_preference: nil, traffic_model: nil, region: nil) params = { 'origins' => Convert.piped_location(origins), 'destinations' => Convert.piped_location(destinations) } if mode raise StandardError, 'Invalid travel mode.' unless Constants::TRAVEL_MODES.include? mode params['mode'] = mode end if language params['language'] = language end if avoid raise StandardError, 'Invalid route restriction.' unless Constants::AVOID_FEATURES.include? avoid params['avoid'] = avoid end if units params['units'] = units end if departure_time params['departure_time'] = Convert.unix_time(departure_time) end if arrival_time params['arrival_time'] = Convert.unix_time(arrival_time) end if departure_time && arrival_time raise StandardError, 'Should not specify both departure_time and arrival_time.' end if transit_mode params['transit_mode'] = Convert.join_array('|', transit_mode) end if transit_routing_preference params['transit_routing_preference'] = transit_routing_preference end if traffic_model params['traffic_model'] = traffic_model end if region params['region'] = region end self.client.request(url: "/maps/api/distancematrix/#{self.client.response_format}", params: params) end |