Class: GoogleMapsAPI::DistanceMatrix::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/google_maps_api/distance_matrix/request.rb

Constant Summary collapse

BASE_PATH =
"/maps/api/distancematrix/json"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @origins = arrays_to_coordinate_string(origins)
  @destinations = arrays_to_coordinate_string(destinations)
  @options = options
  @http_adapter = nil
end

Instance Attribute Details

#destinationsObject

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_adapterObject

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

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/google_maps_api/distance_matrix/request.rb', line 7

def options
  @options
end

#originsObject

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, options = {})
  self.new(origin, destination, options)
end

Instance Method Details

#business_account?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/google_maps_api/distance_matrix/request.rb', line 61

def business_account?
  options.key?(:key) && options.key?(:client)
end

#performObject



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

#schemeObject



53
54
55
# File 'lib/google_maps_api/distance_matrix/request.rb', line 53

def scheme
  options[:https] ? "https" : "http"
end

#uriObject



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 = prepared_options.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