oca-epak 0.2.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ecb3139fa51dce2eb589c4a685ce6a52c2d5080
4
- data.tar.gz: 8faa70640d360219b56952dba7115c273666da44
3
+ metadata.gz: 9c5f04a073183bc57a5503b8a5c677a01e45aa21
4
+ data.tar.gz: a24c7b70efa907c1b263caf863e2b7cd3e285a9e
5
5
  SHA512:
6
- metadata.gz: a2ecdf35529bef858fb6eb1324389a3218a97e682cb117b82eedb09840dbe809962fe008f7005378b212575a1df323c0fe6884f58f7de6fb36a225af7015471f
7
- data.tar.gz: 142c035cb508657669560b38e1f4aaef9d235954bf6aa7c894e2c11343e10f7696bf2c3d53a6b6939e95bc09da868e8b536716aeae832bab74d8d1389f60bf4c
6
+ metadata.gz: 19683eaa6a9815526daffbd659240d5e99cd0e55b15a8d657d46f97f7e25b8ba33741468173bc5c058aaad78a397847bd9050f0ee4c645fb57f035865b6c3a43
7
+ data.tar.gz: c9257ce2e186a08c5ffe1e67004f73504113c56007a27ffedc6d81be57cf005b38105dfa83d82baf8e958b587c2bcb53bee576eb7607a3adfd9b6591cf5e31fb
@@ -1,108 +1,160 @@
1
1
  require 'savon'
2
+ require 'erb'
3
+ require 'ostruct'
4
+ require 'oca-epak/pickup_data'
5
+ require 'oca-epak/errors/error'
6
+ require 'oca-epak/errors/bad_request'
2
7
 
3
- class Oca
4
- attr_reader :client
8
+ module Oca
9
+ class Epak
10
+ attr_reader :client
11
+ attr_accessor :username, :password
5
12
 
6
- WSDL = 'http://webservice.oca.com.ar/oep_tracking/Oep_Track.asmx?WSDL'.freeze
13
+ WSDL = 'http://webservice.oca.com.ar/epak_tracking/Oep_TrackEPak.asmx?wsdl'.freeze
7
14
 
8
- def initialize
9
- @client = Savon.client(wsdl: WSDL)
10
- end
15
+ def initialize(username, password)
16
+ @client = Savon.client(wsdl: WSDL)
17
+ @username = username
18
+ @password = password
19
+ end
11
20
 
12
- # Checks if the user has input valid credentials
13
- #
14
- # @param [String] Username (Email)
15
- # @param [String] Password
16
- # @return [Boolean] Whether the credentials are valid or not
17
- def check_credentials(username, password)
18
- begin
21
+ # Checks if the user has input valid credentials
22
+ #
23
+ # @return [Boolean] Whether the credentials entered are valid or not
24
+ def check_credentials
25
+ method = :get_epack_user
19
26
  opts = { "usr" => username, "psw" => password }
20
- client.call(:generar_consolidacion_de_ordenes_de_retiro, message: opts)
21
- false
22
- rescue Savon::SOAPFault => e
23
- true
27
+ response = client.call(method, message: opts)
28
+ parse_results_table(response, method)[:existe] == "1"
24
29
  end
25
- end
26
30
 
27
- # Checks whether the operation is valid
28
- #
29
- # @param [String] Client's CUIT
30
- # @param [String] Operation Type
31
- # @return [Boolean]
32
- def check_operation(cuit, op)
33
- begin
34
- opts = { wt: "50", vol: "0.027", origin: "1414", destination: "5403",
35
- qty: "1", cuit: cuit, op: op }
36
- get_shipping_rates(opts)
37
- true
38
- rescue NoMethodError => e
39
- false
31
+ # Checks whether the operation is valid
32
+ #
33
+ # @param [String] Client's CUIT
34
+ # @param [String] Operation Type
35
+ # @return [Boolean]
36
+ def check_operation(cuit, op)
37
+ begin
38
+ opts = { wt: "50", vol: "0.027", origin: "1414", destination: "5403",
39
+ qty: "1", total: "123", cuit: cuit, op: op }
40
+ get_shipping_rates(opts)
41
+ true
42
+ rescue Oca::Epak::Error => e
43
+ false
44
+ end
40
45
  end
41
- end
42
46
 
43
- # Get rates and delivery estimate for a shipment
44
- #
45
- # @param [Hash] opts
46
- # @option [String] :wt Total Weight e.g: 20
47
- # @option [String] :vol Total Volume e.g: 0.0015 (0.1mts * 0.15mts * 0.1mts)
48
- # @option [String] :origin Origin ZIP Code
49
- # @option [String] :destination Destination ZIP Code
50
- # @option [String] :qty Quantity of Packages
51
- # @option [String] :cuit Client's CUIT e.g: 30-99999999-7
52
- # @option [String] :op Operation Type
53
- # @return [Hash, nil] Contains Total Price, Delivery Estimate
54
- def get_shipping_rates(opts = {})
55
- method = :tarifar_envio_corporativo
56
- message = { "PesoTotal" => opts[:wt], "VolumenTotal" => opts[:vol],
57
- "CodigoPostalOrigen" => opts[:origin],
58
- "CodigoPostalDestino" => opts[:destination],
59
- "CantidadPaquetes" => opts[:qty], "Cuit" => opts[:cuit],
60
- "Operativa" => opts[:op] }
61
- response = client.call(method, message: message)
62
- parse_results(method, response)
63
- end
47
+ # Creates a Pickup Order, which lets OCA know you want to make a delivery.
48
+ #
49
+ # @see [http://www.ombushop.com/oca/documentation.pdf] #TODO put it there
50
+ #
51
+ # @param [Hash] opts
52
+ # @option [Oca::Epak::PickupData] :pickup_data Pickup Data object
53
+ # @option [Boolean] :confirm_pickup Confirm Pickup? Defaults to false
54
+ # @option [Integer] :days_to_pickup Days OCA should wait before pickup, default: 1 (?) #TODO Confirm
55
+ # @option [Integer] :pickup_range Range to be used when picking it up, default: 1
56
+ # @return [Hash, nil]
57
+ def create_pickup_order(opts = {})
58
+ confirm_pickup = opts.fetch(:confirm_pickup, false)
59
+ days_to_pickup = opts.fetch(:days_to_pickup, "1")
60
+ pickup_range = opts.fetch(:pickup_range, "1")
61
+ rendered_xml = opts[:pickup_data].to_xml
64
62
 
65
- # Returns all existing Taxation Centers
66
- #
67
- # @return [Array, nil] Information for all the Oca Taxation Centers
68
- def taxation_centers
69
- method = :get_centros_imposicion
70
- response = client.call(method)
71
- parse_results(method, response)
72
- end
63
+ message = { "usr" => username, "psw" => password,
64
+ "xml_Datos" => rendered_xml,
65
+ "ConfirmarRetiro" => confirm_pickup.to_s,
66
+ "DiasHastaRetiro" => days_to_pickup,
67
+ "idFranjaHoraria" => pickup_range }
68
+ response = client.call(:ingreso_or, message: message)
69
+ parse_results_table(response, :ingreso_or)
70
+ end
73
71
 
74
- # Given a client's CUIT with a range of dates, returns a list with
75
- # all shipments made within the given period.
76
- #
77
- # @param [String] Client's CUIT
78
- # @param [String] "From date" in DD-MM-YYYY format
79
- # @param [String] "To date" in DD-MM-YYYY format
80
- # @return [Array, nil] Contains an array of hashes with NroProducto and NumeroEnvio
81
- def list_shipments(cuit, from_date, to_date)
82
- method = :list_envios
83
- opts = { "CUIT" => cuit, "FechaDesde" => from_date,
84
- "FechaHasta" => to_date }
85
- response = client.call(method, message: opts)
86
- parse_results(method, response)
87
- end
72
+ # Get rates and delivery estimate for a shipment
73
+ #
74
+ # @param [Hash] opts
75
+ # @option [String] :wt Total Weight e.g: 20
76
+ # @option [String] :vol Total Volume e.g: 0.0015 (0.1mts * 0.15mts * 0.1mts)
77
+ # @option [Integer] :origin Origin ZIP Code
78
+ # @option [Integer] :destination Destination ZIP Code
79
+ # @option [Integer] :qty Quantity of Packages
80
+ # @option [Integer] :total Declared Value
81
+ # @option [String] :cuit Client's CUIT e.g: 30-99999999-7
82
+ # @option [String] :op Operation Type
83
+ # @return [Hash, nil] Contains Total Price, Delivery Estimate
84
+ def get_shipping_rates(opts = {})
85
+ method = :tarifar_envio_corporativo
86
+ message = { "PesoTotal" => opts[:wt], "VolumenTotal" => opts[:vol],
87
+ "CodigoPostalOrigen" => opts[:origin],
88
+ "CodigoPostalDestino" => opts[:destination],
89
+ "ValorDeclarado" => opts[:total],
90
+ "CantidadPaquetes" => opts[:qty], "Cuit" => opts[:cuit],
91
+ "Operativa" => opts[:op] }
92
+ response = client.call(method, message: message)
93
+ parse_results_table(response, method)
94
+ end
88
95
 
89
- # Returns all provinces in Argentina
90
- #
91
- # @return [Array, nil] Provinces in Argentina with their ID and name as a Hash
92
- def provinces
93
- response = client.call(:get_provincias)
94
- if body = response.body[:get_provincias_response]
95
- body[:get_provincias_result][:provincias][:provincia]
96
+ # Returns all existing Taxation Centers
97
+ #
98
+ # @return [Array, nil] Information for all the Oca Taxation Centers
99
+ def taxation_centers
100
+ method = :get_centros_imposicion
101
+ response = client.call(method)
102
+ parse_results_table(response, method)
96
103
  end
97
- end
98
104
 
99
- private
105
+ # Returns all operation codes
106
+ #
107
+ # @return [Array, nil] Returns all operation codes available for the user
108
+ def get_operation_codes
109
+ method = :get_operativas_by_usuario
110
+ opts = { "usr" => username, "psw" => password }
111
+ response = client.call(method, message: opts)
112
+ parse_results_table(response, method)
113
+ end
100
114
 
101
- def parse_results(method, response)
102
- method_response = "#{method}_response".to_sym
103
- method_result = "#{method}_result".to_sym
104
- if body = response.body[method_response]
105
- body[method_result][:diffgram][:new_data_set][:table]
115
+ # Given a client's CUIT with a range of dates, returns a list with
116
+ # all shipments made within the given period.
117
+ #
118
+ # @param [String] Client's CUIT
119
+ # @param [String] "From date" in DD-MM-YYYY format
120
+ # @param [String] "To date" in DD-MM-YYYY format
121
+ # @return [Array, nil] Contains an array of hashes with NroProducto and NumeroEnvio
122
+ def list_shipments(cuit, from_date, to_date)
123
+ method = :list_envios
124
+ opts = { "CUIT" => cuit, "FechaDesde" => from_date,
125
+ "FechaHasta" => to_date }
126
+ response = client.call(method, message: opts)
127
+ parse_results_table(response, method)
128
+ end
129
+
130
+ # Returns all provinces in Argentina
131
+ #
132
+ # @return [Array, nil] Provinces in Argentina with their ID and name as a Hash
133
+ def provinces
134
+ response = client.call(:get_provincias)
135
+ if body = response.body[:get_provincias_response]
136
+ body[:get_provincias_result][:provincias][:provincia]
106
137
  end
107
138
  end
139
+
140
+ private
141
+
142
+ def parse_result(response, method)
143
+ method_response = "#{method}_response".to_sym
144
+ method_result = "#{method}_result".to_sym
145
+ if body = response.body[method_response]
146
+ body[method_result]
147
+ end
148
+ end
149
+
150
+ def parse_results_table(response, method)
151
+ if result = parse_result(response, method)
152
+ if result[:diffgram][:new_data_set]
153
+ result[:diffgram][:new_data_set][:table]
154
+ else
155
+ raise Oca::Epak::BadRequest.new("Oca WS responded with:\n#{response.body}")
156
+ end
157
+ end
158
+ end
159
+ end
108
160
  end
@@ -0,0 +1,4 @@
1
+ module Oca
2
+ class Epak::BadRequest < Epak::Error
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Oca
2
+ class Epak::Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,29 @@
1
+ module Oca
2
+ class Epak::PickupData
3
+
4
+ attr_accessor :account_number, :pickup, :shipments
5
+
6
+ # Creates a Pickup Data object for creating a pickup order in OCA.
7
+ #
8
+ # @param [Hash] opts
9
+ # @option [String] :account_number Account Number (SAP)
10
+ # @option [Hash] :pickup Pickup Hash
11
+ # @option [Array<Hash>] :shipments Shipments Hash
12
+ def initialize(opts = {})
13
+ self.account_number = opts[:account_number]
14
+ self.pickup = opts[:pickup]
15
+ self.shipments = opts[:shipments]
16
+ end
17
+
18
+ def to_xml
19
+ or_template.result(binding)
20
+ end
21
+
22
+ private
23
+
24
+ def or_template
25
+ path_to_xml = File.expand_path("../retiro.xml.erb", __FILE__)
26
+ ERB.new(File.read(path_to_xml), nil, "-")
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module Oca
2
+ class Epak
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oca-epak
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mauro Otonelli
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-10-29 00:00:00.000000000 Z
12
+ date: 2015-11-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: savon
@@ -104,6 +104,10 @@ extensions: []
104
104
  extra_rdoc_files: []
105
105
  files:
106
106
  - lib/oca-epak.rb
107
+ - lib/oca-epak/errors/bad_request.rb
108
+ - lib/oca-epak/errors/error.rb
109
+ - lib/oca-epak/pickup_data.rb
110
+ - lib/oca-epak/version.rb
107
111
  homepage: https://github.com/ombulabs/oca-epak
108
112
  licenses: []
109
113
  metadata: {}