Class: GoogleMapsJuice::Client

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

Constant Summary collapse

API_HOST =
'https://maps.googleapis.com'
API_PATH =
'/maps/api'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: GoogleMapsJuice.config.api_key) ⇒ Client

Returns a new instance of Client.



14
15
16
17
# File 'lib/google_maps_juice/client.rb', line 14

def initialize(api_key: GoogleMapsJuice.config.api_key)
  @api_key = api_key
  @connection = Excon.new(API_HOST)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



3
4
5
# File 'lib/google_maps_juice/client.rb', line 3

def api_key
  @api_key
end

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/google_maps_juice/client.rb', line 3

def connection
  @connection
end

Class Method Details

.get(endpoint, params, api_key: GoogleMapsJuice.config.api_key) ⇒ Object



6
7
8
# File 'lib/google_maps_juice/client.rb', line 6

def get(endpoint, params, api_key: GoogleMapsJuice.config.api_key)
  self.new(api_key: api_key).get(endpoint, params)
end

Instance Method Details

#get(endpoint, params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/google_maps_juice/client.rb', line 19

def get(endpoint, params)
  full_params = (params || Hash.new).merge({ key: api_key })
  response = connection.get(
    path: "#{API_PATH}#{endpoint}",
    query: full_params
  )
  if healthy_http_status?(response.status)
    response.body
  else
    msg = "HTTP #{response.status}"
    msg += " - #{response.body}" if response.body.present?
    raise GoogleMapsJuice::ResponseError, msg
  end
end

#healthy_http_status?(status) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/google_maps_juice/client.rb', line 34

def healthy_http_status?(status)
  /^[123]\d{2}$/.match?("#{status}")
end