Class: GoogleMapsAPI::DistanceMatrix::Request
- Inherits:
-
Object
- Object
- GoogleMapsAPI::DistanceMatrix::Request
- Defined in:
- lib/google_maps_api/distance_matrix/request.rb
Constant Summary collapse
- BASE_PATH =
"/maps/api/distancematrix/json"
Instance Attribute Summary collapse
-
#destinations ⇒ Object
Returns the value of attribute destinations.
-
#http_adapter ⇒ Object
Returns the value of attribute http_adapter.
-
#options ⇒ Object
Returns the value of attribute options.
-
#origins ⇒ Object
Returns the value of attribute origins.
Class Method Summary collapse
Instance Method Summary collapse
- #business_account? ⇒ Boolean
-
#initialize(origins, destinations, options = {}) ⇒ Request
constructor
A new instance of Request.
- #perform ⇒ Object
- #scheme ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(origins, destinations, options = {}) ⇒ Request
Returns a new instance of Request.
9 10 11 12 13 14 |
# File 'lib/google_maps_api/distance_matrix/request.rb', line 9 def initialize(origins, destinations, = {}) @origins = arrays_to_coordinate_string(origins) @destinations = arrays_to_coordinate_string(destinations) @options = @http_adapter = nil end |
Instance Attribute Details
#destinations ⇒ Object
Returns the value of attribute destinations.
7 8 9 |
# File 'lib/google_maps_api/distance_matrix/request.rb', line 7 def destinations @destinations end |
#http_adapter ⇒ Object
Returns the value of attribute http_adapter.
7 8 9 |
# File 'lib/google_maps_api/distance_matrix/request.rb', line 7 def http_adapter @http_adapter end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/google_maps_api/distance_matrix/request.rb', line 7 def @options end |
#origins ⇒ Object
Returns the value of attribute origins.
7 8 9 |
# File 'lib/google_maps_api/distance_matrix/request.rb', line 7 def origins @origins end |
Class Method Details
.build(origin, destination, options = {}) ⇒ Object
16 17 18 |
# File 'lib/google_maps_api/distance_matrix/request.rb', line 16 def self.build(origin, destination, = {}) self.new(origin, destination, ) end |
Instance Method Details
#business_account? ⇒ Boolean
61 62 63 |
# File 'lib/google_maps_api/distance_matrix/request.rb', line 61 def business_account? .key?(:key) && .key?(:client) end |
#perform ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/google_maps_api/distance_matrix/request.rb', line 20 def perform parsed_url = URI.parse(uri.to_s) http = http_adapter.new(parsed_url.host, parsed_url.port) http.use_ssl = (scheme == "https") response = http.request_get(parsed_url.request_uri) if response.is_a?(Net::HTTPSuccess) return GoogleMapsAPI::DistanceMatrix::Response.from_json(response.body) else msg = "The response was not successful (200). Call #response for details." exception = GoogleMapsAPI::DistanceMatrix::ResponseError.new(msg) exception.response = response raise exception end end |
#scheme ⇒ Object
53 54 55 |
# File 'lib/google_maps_api/distance_matrix/request.rb', line 53 def scheme [:https] ? "https" : "http" end |
#uri ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/google_maps_api/distance_matrix/request.rb', line 35 def uri base_host = GoogleMapsAPI::Core::BASE_HOST uri = "#{scheme}://#{base_host}#{BASE_PATH}" query_params = .merge( {origins: origins.join("|"), destinations: destinations.join("|")} ).reject { |key, value| [:client, :channel].include?(key) } if business_account? query_params = query_params.reject { |key, value| [:key].include?(key) } uri = "#{uri}?#{to_query(query_params)}" uri = sign_uri(uri) else uri = URI("#{uri}?#{URI.encode_www_form(query_params)}") end uri end |