Class: Oca::Epak::Client

Inherits:
BaseClient show all
Defined in:
lib/oca-epak/epak/client.rb

Constant Summary collapse

ONE_STRING =
"1".freeze
USER_STRING =
"usr".freeze
PASSWORD_STRING =
"psw".freeze
WSDL_URL =
"#{BASE_WSDL_URL}/epak_tracking/Oep_TrackEPak.asmx?wsdl".freeze
CUIT_PATTERN =
/[0-9]{2}-[0-9]{8}-[0-9]/

Constants inherited from BaseClient

BaseClient::BASE_WSDL_URL, BaseClient::FALSE_STRING

Instance Attribute Summary

Attributes inherited from BaseClient

#client, #password, #username

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client



10
11
12
13
14
# File 'lib/oca-epak/epak/client.rb', line 10

def initialize(username, password)
  super
  @opts = { wsdl: WSDL_URL }.merge(Oca::Logger.options)
  @client = Savon.client(@opts)
end

Instance Method Details

#check_credentialsBoolean

Checks if the user has input valid credentials



19
20
21
22
23
24
# File 'lib/oca-epak/epak/client.rb', line 19

def check_credentials
  method = :get_epack_user
  opts = { USER_STRING => username, PASSWORD_STRING => password }
  response = client.call(method, message: opts)
  parse_results_table(response, method).first[:existe] == ONE_STRING
end

#create_pickup_order(opts = {}) ⇒ Hash?

Creates a Pickup Order, which lets OCA know you want to make a delivery.

Options Hash (opts):

  • :pickup_data (Oca::Epak::PickupData)

    Pickup Data object

  • :confirm_pickup (Boolean)

    Confirm Pickup? Defaults to false

  • :days_to_pickup (Integer)

    Days OCA should wait before pickup, default: 1

  • :pickup_range (Integer)

    Range to be used when picking it up, default: 1

See Also:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/oca-epak/epak/client.rb', line 36

def create_pickup_order(opts = {})
  confirm_pickup = opts.fetch(:confirm_pickup, FALSE_STRING)
  days_to_pickup = opts.fetch(:days_to_pickup, ONE_STRING)
  pickup_range = opts.fetch(:pickup_range, ONE_STRING)
  rendered_xml = opts[:pickup_data].to_xml

  message = { USER_STRING => username, PASSWORD_STRING => password,
              "xml_Datos" => rendered_xml,
              "ConfirmarRetiro" => confirm_pickup.to_s,
              "DiasHastaRetiro" => days_to_pickup,
              "idFranjaHoraria" => pickup_range }
  response = client.call(:ingreso_or, message: message)
  parse_result(response, :ingreso_or)
end

#get_operation_codesArray?

Returns all operation codes



101
102
103
104
105
106
# File 'lib/oca-epak/epak/client.rb', line 101

def get_operation_codes
  method = :get_operativas_by_usuario
  opts = { USER_STRING => username, PASSWORD_STRING => password }
  response = client.call(method, message: opts)
  parse_results_table(response, method)
end

#get_shipping_rate(opts = {}) ⇒ Hash?

Get rates and delivery estimate for a shipment

Options Hash (opts):

  • :total_weight (String)

    Total Weight e.g: 20

  • :total_volume (String)

    Total Volume e.g: 0.0015 (0.1mts * 0.15mts * 0.1mts)

  • :origin_zip_code (String)

    Origin ZIP Code

  • :destination_zip_code (String)

    Destination ZIP Code

  • :declared_value (String)

    Declared Value

  • :package_quantity (String)

    Quantity of Packages

  • :cuit (String)

    Client's CUIT e.g: 30-99999999-7

  • :operation_code (String)

    Operation Type



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/oca-epak/epak/client.rb', line 64

def get_shipping_rate(opts = {})
  cuit_number = normalize_cuit(opts[:cuit])

  method = :tarifar_envio_corporativo
  message = { "PesoTotal" => opts[:total_weight],
              "VolumenTotal" => opts[:total_volume],
              "CodigoPostalOrigen" => opts[:origin_zip_code],
              "CodigoPostalDestino" => opts[:destination_zip_code],
              "ValorDeclarado" => opts[:declared_value],
              "CantidadPaquetes" => opts[:package_quantity],
              "Cuit" => cuit_number,
              "Operativa" => opts[:operation_code] }
  response = client.call(method, message: message)
  parse_results_table(response, method).first
end

#list_shipments(cuit, from_date, to_date) ⇒ Array?

Given a client's CUIT with a range of dates, returns a list with all shipments made within the given period.



115
116
117
118
119
120
121
122
123
# File 'lib/oca-epak/epak/client.rb', line 115

def list_shipments(cuit, from_date, to_date)
  cuit_number = normalize_cuit(cuit)

  method = :list_envios
  opts = { "CUIT" => cuit_number, "FechaDesde" => from_date,
           "FechaHasta" => to_date }
  response = client.call(method, message: opts)
  parse_results_table(response, method)
end

#provincesArray?

Returns all provinces in Argentina



128
129
130
131
132
133
# File 'lib/oca-epak/epak/client.rb', line 128

def provinces
  response = client.call(:get_provincias)
  if body = response.body[:get_provincias_response]
    body[:get_provincias_result][:provincias][:provincia]
  end
end

#taxation_centersArray?

Returns all existing Taxation Centers



83
84
85
86
87
# File 'lib/oca-epak/epak/client.rb', line 83

def taxation_centers
  method = :get_centros_imposicion
  response = client.call(method)
  parse_results_table(response, method)
end

#taxation_centers_with_servicesArray?

Returns all existing Taxation Centers with services



92
93
94
95
96
# File 'lib/oca-epak/epak/client.rb', line 92

def taxation_centers_with_services
  method = :get_centros_imposicion_con_servicios
  response = client.call(method)
  parse_result(response, method)
end

#taxation_centers_with_services_by_zipcode(opts = {}) ⇒ Array?

Returns all existing Taxation Centers with services

Options Hash (opts):

  • :codigo_postal (String)

    ZipCode



171
172
173
174
175
176
177
178
# File 'lib/oca-epak/epak/client.rb', line 171

def taxation_centers_with_services_by_zipcode(opts = {})
  message = {
    "CodigoPostal" => opts[:codigo_postal]
  }
  method = :get_centros_imposicion_con_servicios_by_cp
  response = client.call(method, message: message)
  parse_result(response, method)
end

#tracking_object(opts = {}) ⇒ Hash?

Get the tracking history of an object

Options Hash (opts):

  • :cuit (String)

    Client's CUIT e.g: 30-99999999-7

  • :pieza (String)

    Tracking number



141
142
143
144
145
146
147
148
149
150
# File 'lib/oca-epak/epak/client.rb', line 141

def tracking_object(opts = {})
  message = {
    "Cuit" => normalize_cuit(opts[:cuit]),
    "Pieza" => opts[:pieza]
  }

  response = client.call(:tracking_pieza, message: message)

  parse_result(response, :tracking_pieza)
end

#tracking_object_with_ids(opts = {}) ⇒ Hash?

Get the tracking history of an object with the ids

Options Hash (opts):

  • :pieza (String)

    Tracking number



157
158
159
160
161
162
163
164
# File 'lib/oca-epak/epak/client.rb', line 157

def tracking_object_with_ids(opts = {})
  message = {
    "NumeroEnvio" => opts[:pieza]
  }

  response = client.call(:tracking_pieza_con_id_estado, message: message)
  parse_result(response, :tracking_pieza_con_id_estado)
end