Class: DigiwinDsp::Resources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/digiwin_dsp/resources/base.rb

Overview

Subclasses declare two constants:

PATH       — endpoint path (e.g. "/v1/SalesOrder/add")
SERIALIZER — a module/class responding to `.serialize(records, digi_header:)`

Base then handles the .create class shortcut, the instance #create flow, and the response_detail safety check.

Direct Known Subclasses

Cancellation, Invoice, Order, Return

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = Client.new) ⇒ Base

Returns a new instance of Base.



16
17
18
# File 'lib/digiwin_dsp/resources/base.rb', line 16

def initialize(client = Client.new)
  @client = client
end

Class Method Details

.create(records, idempotency_key: nil, digi_header: nil) ⇒ Object



12
13
14
# File 'lib/digiwin_dsp/resources/base.rb', line 12

def self.create(records, idempotency_key: nil, digi_header: nil)
  new.create(records, idempotency_key: idempotency_key, digi_header: digi_header)
end

Instance Method Details

#create(records, idempotency_key: nil, digi_header: nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/digiwin_dsp/resources/base.rb', line 20

def create(records, idempotency_key: nil, digi_header: nil)
  body = self.class::SERIALIZER.serialize(records, digi_header: digi_header)
  response = @client.post(self.class::PATH, body, idempotency_key: idempotency_key)
  response.fetch("response_detail") do
    raise DigiwinDsp::ServerError, "DSP returned Status=Success without response_detail"
  end
end