Class: Dpd::Shipping::API

Inherits:
Object
  • Object
show all
Defined in:
lib/dpd_shipping/api.rb

Constant Summary collapse

DEFAULT_NAMESPACES =
{
  "xmlns:iloxx" => "http://iloxx.de/"
}
PPVAPI_WSDL =
"https://www.iloxx.de/iloxxapi/ppvapi.asmx?WSDL"
PPVAPI_ENDPOINT =
"https://www.iloxx.de/iloxxapi/ppvapi.asmx"
PPVAPI_TEST_WSDL =
"http://qa.www.iloxx.de/iloxxapi/ppvapi.asmx?WSDL"
PPVAPI_TEST_ENDPOINT =
"http://qa.www.iloxx.de/iloxxapi/ppvapi.asmx"
API_VERSION =
102

Instance Method Summary collapse

Constructor Details

#initialize(config, options = {}) ⇒ API

Returns a new instance of API.



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
# File 'lib/dpd_shipping/api.rb', line 26

def initialize(config, options = {})
  raise "User ID must be specified" if config[:user].nil?
  raise "User Token must be specified" if config[:token].nil?

  if options[:test]
    wsdl_url = PPVAPI_TEST_WSDL
    endpoint = PPVAPI_TEST_ENDPOINT
  else
    wsdl_url = PPVAPI_WSDL
    endpoint = PPVAPI_ENDPOINT
  end

  @user = config[:user]
  @token = config[:token]
  @partner_name = config[:partner_name] || 'iloxx.24'
  @partner_key = config[:partner_key] || '554F346F592B42757131367A64477A7A676362767A413D3D'

  @options = options
  @client = ::Savon::Client.new do |sc|
    sc.wsdl wsdl_url
    sc.endpoint endpoint
    sc.namespace DEFAULT_NAMESPACES['xmlns:iloxx']
    sc.soap_version 2
    sc.filters [:LabelPDFStream, :UserToken]
  end
end

Instance Method Details

#add_order(parcels, date = nil) ⇒ Object



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
# File 'lib/dpd_shipping/api.rb', line 53

def add_order(parcels, date = nil)
  shipping_date = date || Date.today

  if shipping_date > Date.today + 7
    raise ShippingDateError.new('Invalid shipping date. Min: today, max: +7 days')
  end

  if !parcels.is_a? Array
    parcels = [parcels]
  end
  parcels.each do |p|
    p.internal_reference = parcels.index p
  end

  request = OrderRequest.new(
    :version => API_VERSION,
    :auth => auth_hash,
    :shipping_date => shipping_date,
    :parcels => parcels
  )
  response = @client.call(:ppv_add_order, message: request.build!)

  result = response.body[:ppv_add_order_response][:ppv_add_order_result]
  if result[:ack] == 'Success'
    shipments = []
    if !result[:response_data_array].is_a? Array
      result[:response_data_array] = [result[:response_data_array][:response_data]]
    end
    result[:response_data_array].each { |rdata| shipments << rdata }
    {
      :label_data => result[:label_pdf_stream],
      :shipments => shipments
    }
  else
    handle_errors result
  end
end

#get_daily_transaction_list(date = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dpd_shipping/api.rb', line 91

def get_daily_transaction_list(date = nil)
  shipping_date = date || Date.today

  request = DailyTransactionRequest.new(
    :version => API_VERSION,
    :auth => auth_hash,
    :date => shipping_date,
    :type => :DPD
  )

  response = @client.call(:ppv_get_daily_transaction_list, message: request.build!)
  result = response.body[:ppv_get_daily_transaction_list_response][:ppv_add_order_result]
  if result[:DailyTransactionResponse] == 'Success'
    {
      :transaction_list => result[:transaction_list_pdf_stream]
    }
  else
    handle_errors result
  end
end