Class: GoogleGeocodeApi::Client

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

Overview

Client having API methods and api_key

Constant Summary collapse

BASE_URL =
"https://maps.googleapis.com/maps/api/geocode/json"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, adapter: Faraday.default_adapter, stubs: nil) ⇒ Client

#### Params: api_key: String - Required - API key for Google Geocode API (developers.google.com/maps/documentation/geocoding/get-api-key#creating-api-keys) adapter: Symbol - Optional - Default: :net_http - Adapter for Faraday connection stubs: Faraday::Adapter::Test::Stubs - Optional - Default: nil - Stubs for Faraday connection #### Example: client = GoogleGeocodeApi::Client.new api_key: “test” #### Description: Client for Google Geocode API



18
19
20
21
22
23
24
# File 'lib/google_geocode_api/client.rb', line 18

def initialize(api_key:, adapter: Faraday.default_adapter, stubs: nil)
  @api_key = api_key
  @adapter = adapter

  # Test stubs for requests
  @stubs = stubs
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/google_geocode_api/client.rb', line 8

def adapter
  @adapter
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/google_geocode_api/client.rb', line 8

def api_key
  @api_key
end

Instance Method Details

#find_address(address:) ⇒ Object

#### Params: address: String - Required - Address String for Google Geocode API #### Example: GoogleGeocode::Client.new(api_key: “fake”).find_address(address: “48 Pirrama Rd, Pyrmont NSW 2009, Australia”) #### Response: #<GoogleGeocodeApi::Response:0x0000000109ec58b0 @results=[ #<GoogleGeocodeApi::Address:0x000000010a197d40 @formatted_address=“48 Pirrama Rd, Pyrmont NSW 2009, Australia”, @latitude=-33.866489, @location_type=“ROOFTOP”, @longitude=151.1958561, @place_id=“ChIJN1t_tDeuEmsRUsoyG83frY4”> ]> #### Description: Find address by providing address string



56
57
58
# File 'lib/google_geocode_api/client.rb', line 56

def find_address(address:)
  Response.new get("", { address: address })
end

#find_lat_long(lat:, long:) ⇒ Object

#### Params: lat: Float - Required - Latitude for Google Geocode API long: Float - Required - Longitude for Google Geocode API #### Example: GoogleGeocode::Client.new(api_key: “fake”).find_lat_long(lat: -33.866489, long: 151.1958561) #### Response: #<GoogleGeocodeApi::Response:0x0000000109ec58b0 @results=[ #<GoogleGeocodeApi::Address:0x000000010a197d40 @formatted_address=“48 Pirrama Rd, Pyrmont NSW 2009, Australia”, @latitude=-33.866489, @location_type=“ROOFTOP”, @longitude=151.1958561, @place_id=“ChIJN1t_tDeuEmsRUsoyG83frY4”> ]> #### Description: Find address by providing latitude and longitude values



74
75
76
# File 'lib/google_geocode_api/client.rb', line 74

def find_lat_long(lat:, long:)
  Response.new get("", { latlng: "#{lat},#{long}" })
end

#find_place(place_id:) ⇒ Object

#### Params: place_id: String - Required - Place ID for Google Geocode API #### Example: GoogleGeocode::Client.new(api_key: “fake”).find_place(place_id: “ChIJN1t_tDeuEmsRUsoyG83frY4”) #### Response: #<GoogleGeocodeApi::Response:0x0000000109ec58b0 @results=[ #<GoogleGeocodeApi::Address:0x000000010a197d40 @formatted_address=“48 Pirrama Rd, Pyrmont NSW 2009, Australia”, @latitude=-33.866489, @location_type=“ROOFTOP”, @longitude=151.1958561, @place_id=“ChIJN1t_tDeuEmsRUsoyG83frY4”> ]> #### Description: Find address by place_id



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

def find_place(place_id:)
  Response.new get("", { place_id: place_id })
end