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.



9
10
11
12
13
14
# File 'lib/purolator_ruby/create_shipment_request.rb', line 9

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/purolator_ruby/create_shipment_request.rb', line 7

def client
  @client
end

#payload_optsObject (readonly)

Returns the value of attribute payload_opts.



7
8
9
# File 'lib/purolator_ruby/create_shipment_request.rb', line 7

def payload_opts
  @payload_opts
end

Instance Method Details

#sendObject



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

def send
  payload = shipment_payload

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

    if response[:shipment_pin]
      CreateShipmentResponse.new(:ok, response[:shipment_pin][:value], payload, nil)
    else
      begin
        errors = response[:response_information][:errors].map {|k, v| v[:description]}
      rescue TypeError
        errors = response[:response_information][:errors].map(&:to_s)
      end
      CreateShipmentResponse.new(:error, nil, payload, errors)
    end
  else
    CreateShipmentResponse.new(:error, nil, payload, payload.errors)
  end
end

#send!Object



16
17
18
19
20
21
22
23
# File 'lib/purolator_ruby/create_shipment_request.rb', line 16

def send!
  send.tap do |resp|
    if resp.status == :error
      puts resp
      raise ArgumentError, resp.errors
    end
  end
end