Module: Google::Maps::Configuration

Included in:
Google::Maps
Defined in:
lib/google_maps/configuration.rb

Overview

Defines constants and methods related to configuration

Constant Summary collapse

VALID_OPTIONS_KEYS =

An array of valid keys in the options hash when configuring an API

%i[
  end_point authentication_mode client_id client_secret format
  directions_service places_service geocode_service distance_matrix_service
  api_key default_language place_details_service default_params
].freeze
API_KEY =
'api_key'
DIGITAL_SIGNATURE =
'digital_signature'
DEFAULT_END_POINT =

By default, set “maps.googleapis.com/maps/api/” as the server

'https://maps.googleapis.com/maps/api/'
DEFAULT_DIRECTIONS_SERVICE =
'directions'
DEFAULT_PLACES_SERVICE =
'place/autocomplete'
DEFAULT_PLACE_DETAILS_SERVICE =
'place/details'
DEFAULT_GEOCODE_SERVICE =
'geocode'
DEFAULT_DISTANCE_MATRIX_SERVICE =
'distancematrix'
DEFAULT_FORMAT =
'json'
DEFAULT_LANGUAGE =

default language

:en
DEFAULT_PARAMS =

params to send which each request configured per service ie.: {location: “52.0910,5.1220”, radius: 300000}

{}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

When this module is extended, set all configuration options to their default values



40
41
42
# File 'lib/google_maps/configuration.rb', line 40

def self.extended(base)
  base.reset
end

Instance Method Details

#configure {|_self| ... } ⇒ Object

Convenience method to allow configuration options to be set in a block

Yields:

  • (_self)

Yield Parameters:



45
46
47
48
# File 'lib/google_maps/configuration.rb', line 45

def configure
  yield self
  validate_config
end

#optionsObject

Create a hash of options and their values



67
68
69
70
71
# File 'lib/google_maps/configuration.rb', line 67

def options
  VALID_OPTIONS_KEYS.inject({}) do |option, key|
    option.merge!(key => send(key))
  end
end

#resetObject

Reset all configuration options to defaults



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/google_maps/configuration.rb', line 74

def reset
  self.end_point = DEFAULT_END_POINT
  self.format = DEFAULT_FORMAT
  self.directions_service = DEFAULT_DIRECTIONS_SERVICE
  self.places_service = DEFAULT_PLACES_SERVICE
  self.place_details_service = DEFAULT_PLACE_DETAILS_SERVICE
  self.geocode_service = DEFAULT_GEOCODE_SERVICE
  self.distance_matrix_service = DEFAULT_DISTANCE_MATRIX_SERVICE
  self.default_language = DEFAULT_LANGUAGE
  self.default_params = DEFAULT_PARAMS
  self.authentication_mode = nil
  self.api_key = nil
  self.client_id = nil
  self.client_secret = nil
  self
end

#validate_api_keyObject



57
58
59
# File 'lib/google_maps/configuration.rb', line 57

def validate_api_key
  raise Google::Maps::InvalidConfigurationError, 'No API key provided' unless api_key
end

#validate_configObject



50
51
52
53
54
55
# File 'lib/google_maps/configuration.rb', line 50

def validate_config
  return validate_api_key if authentication_mode == API_KEY
  return validate_digital_signature if authentication_mode == DIGITAL_SIGNATURE

  raise Google::Maps::InvalidConfigurationError, 'No valid authentication mode provided'
end

#validate_digital_signatureObject



61
62
63
64
# File 'lib/google_maps/configuration.rb', line 61

def validate_digital_signature
  raise Google::Maps::InvalidConfigurationError, 'No client id provided' unless client_id
  raise Google::Maps::InvalidConfigurationError, 'No client secret provided' unless client_secret
end