Class: Loqate::Client

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

Overview

Responsible for the HTTP interactions. The only entity aware of HTTP concerns such as status codes and headers.

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Instantiates a new client

Examples:

configuration = Configuration.new(api_key: 'EA69-CD23-CV99-HL18')
client = Client.new(configuration)

Parameters:



16
17
18
# File 'lib/loqate/client.rb', line 16

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#get(endpoint, params = {}) ⇒ APIResult

Performs a GET request to Loqate’s API. Every request returns the status code 200.

Examples:

Retrieving a resource

client = Client.new
result = client.get('/Capture/Interactive/Find/v1.00/json3.ws')

Retrieving a resource using options

client = Client.new
result = client.get('/Capture/Interactive/Find/v1.00/json3.ws', countries: 'GB', text: 'Scrubs Lane')

Parameters:

  • endpoint (String)

    URL of the API endpoint where the GET request

  • params (Hash) (defaults to: {})

    Options for the GET request

Returns:

  • (APIResult)

    Generic response of a request to Loqate’s API



35
36
37
38
39
40
41
42
43
# File 'lib/loqate/client.rb', line 35

def get(endpoint, params = {})
  authenticated_params = authenticate_params(params)
  formatted_params = format_params(authenticated_params)

  response = HTTP.headers(headers).get(configuration.host + endpoint, params: formatted_params)

  body = JSON.parse(response.body)
  APIResult.new(body.fetch('Items'))
end