Class: GoogleMapsApis::Client

Inherits:
Object
  • Object
show all
Includes:
Services::Directions, Services::DistanceMatrix, Services::Elevation, Services::Geocoding, Services::Places, Services::Roads, Services::TimeZone
Defined in:
lib/google_maps_apis/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.

[GoogleMapsApis::Error::ServerError, GoogleMapsApis::Error::RateLimitError]

Constants included from Services::Roads

Services::Roads::ROADS_BASE_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Services::Places

#places_autocomplete

Methods included from Services::TimeZone

#timezone

Methods included from Services::Roads

#nearest_roads, #snap_to_roads, #snapped_speed_limits, #speed_limits

Methods included from Services::Geocoding

#geocode, #reverse_geocode

Methods included from Services::Elevation

#elevation, #elevation_along_path

Methods included from Services::DistanceMatrix

#distance_matrix

Methods included from Services::Directions

#directions

Constructor Details

#initialize(**options) ⇒ Client

Construct Google Maps Web Service API 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 = GoogleMapsApis::Client.new(key: 'Add your key here')

Setup client IDs

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

Setup time out and QPS limit

gmaps = GoogleMapsApis::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 = GoogleMapsApis::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 = GoogleMapsApis::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):



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

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] || GoogleMapsApis.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)


43
44
45
# File 'lib/google_maps_apis/client.rb', line 43

def client_id
  @client_id
end

#client_secretString

Client secret for using Maps API for Work services.

Returns:

  • (String)


47
48
49
# File 'lib/google_maps_apis/client.rb', line 47

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)


39
40
41
# File 'lib/google_maps_apis/client.rb', line 39

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)


57
58
59
# File 'lib/google_maps_apis/client.rb', line 57

def queries_per_second
  @queries_per_second
end

#retry_timeoutInteger

Timeout across multiple retriable requests, in seconds.

Returns:

  • (Integer)


51
52
53
# File 'lib/google_maps_apis/client.rb', line 51

def retry_timeout
  @retry_timeout
end

Instance Method Details

#clientFaraday::Client

Get the current HTTP client.

Returns:

  • (Faraday::Client)


124
125
126
# File 'lib/google_maps_apis/client.rb', line 124

def client
  @client ||= new_client
end