Class: PurolatorRuby::CreateShipmentRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/purolator_ruby/create_shipment_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CreateShipmentRequest

Returns a new instance of CreateShipmentRequest.



35
36
37
38
# File 'lib/purolator_ruby/create_shipment_request.rb', line 35

def initialize(options)
  @client = options.fetch(:client)
  @payload_opts = options.except(:client).merge(account_number: client., billing_number: client.billing_number)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



33
34
35
# File 'lib/purolator_ruby/create_shipment_request.rb', line 33

def client
  @client
end

#payload_optsObject (readonly)

Returns the value of attribute payload_opts.



33
34
35
# File 'lib/purolator_ruby/create_shipment_request.rb', line 33

def payload_opts
  @payload_opts
end

Instance Method Details

#sendObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/purolator_ruby/create_shipment_request.rb', line 48

def send
  payload = shipment_payload

  if payload.valid?
    shipment_pin = client.call(:create_shipment, message: payload.to_h)
      .body
      .fetch(:create_shipment_response)
      .fetch(:shipment_pin)
      .fetch(:value)

    CreateShipmentResponse.new(:ok, shipment_pin, payload, nil)
  else
    CreateShipmentResponse.new(:error, nil, payload, payload.errors)
  end
end

#send!Object



40
41
42
43
44
45
46
# File 'lib/purolator_ruby/create_shipment_request.rb', line 40

def send!
  send.tap do |resp|
    if resp.status == :error
      raise ArgumentError, resp.payload.errors.join(' ')
    end
  end
end