Class: SpreePacketa::Soap::Client

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

Constant Summary collapse

MAX_RETRIES =
3
RETRY_DELAY =

second

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



13
14
15
# File 'lib/spree_packeta/soap/client.rb', line 13

def initialize
  @client = build_client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/spree_packeta/soap/client.rb', line 11

def client
  @client
end

Instance Method Details

#call(operation, message = {}) ⇒ Savon::Response

Call a SOAP operation with automatic error handling and retries

Parameters:

  • operation (Symbol)

    The SOAP operation name

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

    The SOAP message body

Returns:

  • (Savon::Response)

    The SOAP response

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spree_packeta/soap/client.rb', line 26

def call(operation, message = {})
  retries = 0

  begin
    # Always include API password in the message
    message_with_auth = message.merge(api_password: api_password)

    response = client.call(operation, message: message_with_auth)
    handle_response(response)
  rescue Savon::SOAPFault => e
    handle_soap_fault(e)
  rescue Savon::HTTPError, Savon::InvalidResponseError => e
    retries += 1
    if retries <= MAX_RETRIES
      sleep(RETRY_DELAY * retries)
      retry
    else
      raise NetworkError, "Network error after #{MAX_RETRIES} retries: #{e.message}"
    end
  rescue StandardError => e
    raise ApiError, "Unexpected error: #{e.message}"
  end
end