Class: Deliveries::Couriers::MondialRelayDual::Shipments::Create

Inherits:
Object
  • Object
show all
Defined in:
lib/deliveries/couriers/mondial_relay_dual/shipments/create.rb,
lib/deliveries/couriers/mondial_relay_dual/shipments/create/format_params.rb

Defined Under Namespace

Classes: FormatParams

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:) ⇒ Create

Returns a new instance of Create.



10
11
12
# File 'lib/deliveries/couriers/mondial_relay_dual/shipments/create.rb', line 10

def initialize(params:)
  self.params = params
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



8
9
10
# File 'lib/deliveries/couriers/mondial_relay_dual/shipments/create.rb', line 8

def params
  @params
end

Instance Method Details

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/deliveries/couriers/mondial_relay_dual/shipments/create.rb', line 14

def execute
  builder = Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml|
    xml.ShipmentCreationRequest('xmlns:xsi0' => 'http://www.w3.org/2001/XMLSchema-instance',
                                'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema', 'xmlns' => 'http://www.example.org/Request') do
      xml.Context do
        xml.Login Deliveries::Couriers::MondialRelayDual.config(:dual_carrier_login)
        xml.Password Deliveries::Couriers::MondialRelayDual.config(:dual_carrier_password)
        xml.CustomerId Deliveries::Couriers::MondialRelayDual.config(:dual_carrier_customer_id)
        xml.Culture params[:culture]
        xml.VersionAPI '1.0'
      end

      xml.OutputOptions do
        xml.OutputFormat '10x15'
        xml.OutputType 'PdfUrl'
      end

      xml.ShipmentsList do
        xml.Shipment do
          xml.OrderNo params[:order_no]
          xml.CustomerNo
          xml.ParcelCount params[:parcel_count]
          xml.DeliveryMode(Mode: params.dig(:delivery_mode, :mode),
                           Location: params.dig(:delivery_mode,
                                                :location))
          xml.CollectionMode(Mode: params.dig(:collection_mode, :mode),
                             Location: params.dig(:collection_mode,
                                                  :location))
          xml.Parcels do
            params[:parcels].each do |parcel|
              xml.Parcel do
                xml.Content parcel[:content]
                xml.Weight(Value: parcel.dig(:weight, :value), Unit: parcel.dig(:weight, :unit))
              end
            end
          end
          xml.DeliveryInstruction params[:deliver_instruction]
          xml.Sender do
            xml.Address do
              xml.Title
              xml.Firstname
              xml.Lastname
              xml.Streetname params.dig(:sender, :streetname)
              xml.HouseNo
              xml.CountryCode params.dig(:sender, :country_code)
              xml.PostCode params.dig(:sender, :post_code)
              xml.City params.dig(:sender, :city)
              xml.AddressAdd1 params.dig(:sender, :address_add_1)
              xml.AddressAdd2
              xml.AddressAdd3
              xml.PhoneNo params.dig(:sender, :phone_no)
              xml.MobileNo
              xml.Email params.dig(:sender, :email)
            end
          end
          xml.Recipient do
            xml.Address do
              xml.Title
              xml.Firstname
              xml.Lastname
              xml.Streetname params.dig(:recipient, :streetname)
              xml.HouseNo
              xml.CountryCode params.dig(:recipient, :country_code)
              xml.PostCode params.dig(:recipient, :post_code)
              xml.City params.dig(:recipient, :city)
              xml.AddressAdd1 params.dig(:recipient, :address_add_1)
              xml.AddressAdd2
              xml.AddressAdd3
              xml.PhoneNo params.dig(:recipient, :phone_no)
              xml.MobileNo
              xml.Email params.dig(:recipient, :email)
            end
          end
        end
      end
    end
  end

  response = HTTParty.post(
    api_endpoint,
    body: builder.to_xml,
    headers: { 'Content-Type': 'application/xml', Accept: 'application/xml' },
    debug_output: Deliveries.debug ? Deliveries.logger : nil
  )

  xml_doc = Nokogiri::XML(response.body, &:strict)
  xml_doc.remove_namespaces!

  shipment_number = xml_doc.xpath('//ShipmentCreationResponse/ShipmentsList/Shipment/@ShipmentNumber').first&.content
  pdf_url = xml_doc.xpath('//ShipmentCreationResponse/ShipmentsList/Shipment/LabelList/Label/Output').first&.content

  unless shipment_number && pdf_url
    error_node = xml_doc.xpath('//ShipmentCreationResponse/StatusList/Status').find do |n|
      %w[Critical\ Error Error].include?(n[:Level])
    end
    raise Deliveries::APIError.new(error_node[:Message], error_node[:Code]) if error_node

    raise Deliveries::ClientError, 'Cannot obtain error code from the API response'
  end

  {
    tracking_code: shipment_number,
    pdf_url: pdf_url
  }
end