Class: GoogleMapsService::Client

Inherits:
Object
  • Object
show all
Includes:
Apis::Directions, Apis::DistanceMatrix, Apis::Elevation, Apis::Geocoding, Apis::Roads, Apis::TimeZone
Defined in:
lib/google_maps_service/client.rb

Overview

Core client functionality, common across all API requests (including performing HTTP requests).

Constant Summary collapse

DEFAULT_BASE_URL =

Default Google Maps Web Service base endpoints

'https://maps.googleapis.com'
RETRIABLE_ERRORS =

Errors those could be retriable.

[GoogleMapsService::Error::ServerError, GoogleMapsService::Error::RateLimitError]

Constants included from Apis::Roads

Apis::Roads::ROADS_BASE_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Apis::TimeZone

#timezone

Methods included from Apis::Roads

#nearest_roads, #snap_to_roads, #snapped_speed_limits, #speed_limits

Methods included from Apis::Geocoding

#geocode, #reverse_geocode

Methods included from Apis::Elevation

#elevation, #elevation_along_path

Methods included from Apis::DistanceMatrix

#distance_matrix

Methods included from Apis::Directions

#directions

Constructor Details

#initialize(**options) ⇒ Client

Construct Google Maps Web Service API client.

This gem uses Hurley as internal HTTP client. You can configure Hurley::Client through request_options and ssl_options parameters. You can also directly get the Hurley::Client object via #client method.

Examples:

Setup API keys

gmaps = GoogleMapsService::Client.new(key: 'Add your key here')

Setup client IDs

gmaps = GoogleMapsService::Client.new(
    client_id: 'Add your client id here',
    client_secret: 'Add your client secret here'
)

Setup time out and QPS limit

gmaps = GoogleMapsService::Client.new(
    key: 'Add your key here',
    retry_timeout: 20,
    queries_per_second: 10
)

Request behind proxy

request_options = Hurley::RequestOptions.new
request_options.proxy = Hurley::Url.parse 'http://user:[email protected]:3128'

gmaps = GoogleMapsService::Client.new(
    key: 'Add your key here',
    request_options: request_options
)

Using Excon and Http Cache

require 'hurley-excon'       # https://github.com/lostisland/hurley-excon
require 'hurley/http_cache'  # https://github.com/plataformatec/hurley-http-cache

gmaps = GoogleMapsService::Client.new(
    key: 'Add your key here',
    connection: Hurley::HttpCache.new(HurleyExcon::Connection.new)
)

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):



111
112
113
114
115
116
117
118
119
# File 'lib/google_maps_service/client.rb', line 111

def initialize(**options)
  [:key, :client_id, :client_secret,
      :retry_timeout, :queries_per_second,
      :request_options, :ssl_options, :connection].each do |key|
    self.instance_variable_set("@#{key}".to_sym, options[key] || GoogleMapsService.instance_variable_get("@#{key}"))
  end

  initialize_query_tickets
end

Instance Attribute Details

#client_idString

Client id for using Maps API for Work services.

Returns:

  • (String)


41
42
43
# File 'lib/google_maps_service/client.rb', line 41

def client_id
  @client_id
end

#client_secretString

Client secret for using Maps API for Work services.

Returns:

  • (String)


45
46
47
# File 'lib/google_maps_service/client.rb', line 45

def client_secret
  @client_secret
end

#keyString

Secret key for accessing Google Maps Web Service. Can be obtained at https://developers.google.com/maps/documentation/geocoding/get-api-key#key.

Returns:

  • (String)


37
38
39
# File 'lib/google_maps_service/client.rb', line 37

def key
  @key
end

#queries_per_secondInteger (readonly)

Number of queries per second permitted. If the rate limit is reached, the client will sleep for the appropriate amount of time before it runs the current query.

Returns:

  • (Integer)


55
56
57
# File 'lib/google_maps_service/client.rb', line 55

def queries_per_second
  @queries_per_second
end

#retry_timeoutInteger

Timeout across multiple retriable requests, in seconds.

Returns:

  • (Integer)


49
50
51
# File 'lib/google_maps_service/client.rb', line 49

def retry_timeout
  @retry_timeout
end

Instance Method Details

#clientHurley::Client

Get the current HTTP client.

Returns:

  • (Hurley::Client)


123
124
125
# File 'lib/google_maps_service/client.rb', line 123

def client
  @client ||= new_client
end