Class: GoogleDistanceMatrix::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/google_distance_matrix/client.rb

Constant Summary collapse

CLIENT_ERRORS =
%w[
  INVALID_REQUEST
  MAX_ELEMENTS_EXCEEDED
  OVER_QUERY_LIMIT
  REQUEST_DENIED
  UNKNOWN_ERROR
]

Instance Method Summary collapse

Instance Method Details

#get(url, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/google_distance_matrix/client.rb', line 11

def get(url, options = {})
  uri = URI.parse url
  instrumentation = {url: url}.merge(options[:instrumentation] || {})

  response = ActiveSupport::Notifications.instrument "client_request_matrix_data.google_distance_matrix", instrumentation do
    Net::HTTP.get_response uri
  end

  case response
  when Net::HTTPSuccess
    inspect_for_client_errors! response
  when Net::HTTPRequestURITooLong
    fail MatrixUrlTooLong.new url, UrlBuilder::MAX_URL_SIZE, response
  when Net::HTTPClientError
    fail ClientError.new response
  when Net::HTTPServerError
    fail ServerError.new response
  else # Handle this as a request error for now. Maybe fine tune this more later.
    fail ServerError.new response
  end
rescue Timeout::Error => error
  fail ServerError.new error
end