Class: ErpIntegration::Fulfil::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, merchant_id:, logger: nil) ⇒ Client

Returns a new instance of Client.



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

def initialize(api_key:, merchant_id:, logger: nil)
  @api_key = api_key
  @merchant_id = merchant_id
  @logger = logger
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#connectionFaraday::Connection

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

Returns:

  • (Faraday::Connection)

    The configured Faraday connection



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/erp_integration/fulfil/client.rb', line 29

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

    # Notice that logging headers will expose sensitive information
    # like api-key. To avoid use filters:
    # https://lostisland.github.io/faraday/middleware/logger
    faraday.response :logger, @logger, { headers: false, bodies: true } if @logger

    # Custom error handling for the error response
    faraday.use ErpIntegration::Middleware::ErrorHandling

    # 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



23
24
25
# File 'lib/erp_integration/fulfil/client.rb', line 23

def faraday_adapter
  @faraday_adapter ||= Faraday.default_adapter
end

#merchant_idObject (readonly)

Returns the value of attribute merchant_id.



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

def merchant_id
  @merchant_id
end

Instance Method Details

#base_urlString

Generates the url prefix for the Faraday connection client.

Returns:

  • (String)

    The base url for the Fulfil HTTP client



17
18
19
# File 'lib/erp_integration/fulfil/client.rb', line 17

def base_url
  "https://#{merchant_id}.fulfil.io/"
end

#versionString

Sets the default version for the Fulfil API endpoints.

Returns:

  • (String)

    The Fulfil API version to be used



64
65
66
# File 'lib/erp_integration/fulfil/client.rb', line 64

def version
  @version ||= 'v2'
end