Class: FulfilApi::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.

Parameters:



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

def initialize(configuration)
  @configuration = configuration
end

Class Method Details

.connection_for(cache_key) ⇒ Faraday::Connection

Looks up a memoized Faraday::Connection for the given cache key, or

builds and stores one by yielding the block.

The cache is process-wide, so the underlying ‘net_http_persistent` adapter

can actually reuse its TCP/TLS connection pool across requests. Without
this, each `FulfilApi.client` call would instantiate a fresh Faraday
connection, defeating the purpose of the persistent adapter.

Parameters:

  • cache_key (Array)

    A key uniquely identifying the connection.

Yield Returns:

  • (Faraday::Connection)

    The newly built connection.

Returns:

  • (Faraday::Connection)


23
24
25
26
27
# File 'lib/fulfil_api/client.rb', line 23

def connection_for(cache_key)
  @connection_cache_mutex.synchronize do
    @connection_cache[cache_key] ||= yield
  end
end

.reset_connection_cache!void

This method returns an undefined value.

Clears the memoized connection cache. Intended for use in test suites

that need to isolate connection state between tests.


33
34
35
# File 'lib/fulfil_api/client.rb', line 33

def reset_connection_cache!
  @connection_cache_mutex.synchronize { @connection_cache.clear }
end

Instance Method Details

#delete(relative_path) ⇒ Array, ...

Performs an HTTP DELETE request to a Fulfil API endpoint.

Parameters:

  • relative_path (String)

    The relative path to the API resource.

Returns:

  • (Array, Hash, String)

    The parsed response body.



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

def delete(relative_path)
  request(:delete, relative_path)
end

#get(relative_path, url_parameters: nil) ⇒ Array, ...

Performs an HTTP GET request to a Fulfil API endpoint.

Parameters:

  • relative_path (String)

    The relative path to the API resource.

  • url_parameters (Hash, nil) (defaults to: nil)

    The optional URL parameters for the API endpoint.

Returns:

  • (Array, Hash, String)

    The parsed response body.



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

def get(relative_path, url_parameters: nil)
  request(:get, relative_path, url_parameters)
end

#post(relative_path, body: {}) ⇒ Array, ...

Performs an HTTP POST request to a Fulfil API endpoint.

Parameters:

  • relative_path (String)

    The relative path to the API resource.

  • body (Array, Hash, nil) (defaults to: {})

    The request body for the POST HTTP request.

Returns:

  • (Array, Hash, String)

    The parsed response body.



65
66
67
# File 'lib/fulfil_api/client.rb', line 65

def post(relative_path, body: {})
  request(:post, relative_path, body)
end

#put(relative_path, body: nil) ⇒ Array, ...

Performs an HTTP PUT request to a Fulfil API endpoint.

Parameters:

  • relative_path (String)

    The relative path to the API resource.

  • body (Array, Hash, nil) (defaults to: nil)

    The optional request body for the PUT HTTP request.

Returns:

  • (Array, Hash, String)

    The parsed response body.



74
75
76
77
78
# File 'lib/fulfil_api/client.rb', line 74

def put(relative_path, body: nil)
  return request(:put, relative_path) if body.nil?

  request(:put, relative_path, body)
end