Class: ErpIntegration::Fulfil::Client

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

Constant Summary collapse

FORMATTER =
ErpIntegration::Middleware::Formatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_keys_pool:, base_url:, logger: nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
# File 'lib/erp_integration/fulfil/client.rb', line 11

def initialize(api_keys_pool:, base_url:, logger: nil)
  @api_keys_pool = api_keys_pool
  @base_url = base_url.strip
  @logger = logger
end

Instance Attribute Details

#api_keys_poolObject (readonly)

Returns the value of attribute api_keys_pool.



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

def api_keys_pool
  @api_keys_pool
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#connectionFaraday::Connection

Sets up the Faraday connection to talk to the Fulfil API.

Returns:

  • (Faraday::Connection)

    The configured Faraday connection



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/erp_integration/fulfil/client.rb', line 25

def connection
  @connection ||= Faraday.new(url: base_url) do |faraday|
    faraday.headers = default_headers

    faraday.request :json # Encode request bodies as JSON
    faraday.request :retry # Retry transient failures

    faraday.response :follow_redirects
    faraday.response :json # Decode response bodies as JSON

    # Custom error handling for the error response
    faraday.use ErpIntegration::Middleware::ErrorHandling
    # Custom middleware for rotating API keys
    faraday.use ErpIntegration::Middleware::ApiKeysRotation, rotation_options
    # Custom middleware for logging requests and responses
    faraday.use ErpIntegration::Middleware::Logger, @logger, formatter: FORMATTER if @logger

    # Adapter definition should be last in order to make the json parsers be loaded correctly
    faraday.adapter faraday_adapter
  end
end

#faraday_adapterSymbol

Sets the default adapter for the Faraday Connection.

Returns:

  • (Symbol)

    The default Faraday adapter



19
20
21
# File 'lib/erp_integration/fulfil/client.rb', line 19

def faraday_adapter
  @faraday_adapter ||= Faraday.default_adapter
end

#rotate_statuses=(value) ⇒ Object (writeonly)

Sets the attribute rotate_statuses

Parameters:

  • value

    the value to set the attribute rotate_statuses to.



9
10
11
# File 'lib/erp_integration/fulfil/client.rb', line 9

def rotate_statuses=(value)
  @rotate_statuses = value
end

Instance Method Details

#versionString

Sets the default version for the Fulfil API endpoints.

Returns:

  • (String)

    The Fulfil API version to be used



61
62
63
# File 'lib/erp_integration/fulfil/client.rb', line 61

def version
  @version ||= 'v2'
end