Module: Deliveries::Couriers::CorreosExpress

Extended by:
Deliveries::Courier
Defined in:
lib/deliveries/couriers/correos_express.rb,
lib/deliveries/couriers/correos_express/address.rb,
lib/deliveries/couriers/correos_express/pickups/trace.rb,
lib/deliveries/couriers/correos_express/pickups/create.rb,
lib/deliveries/couriers/correos_express/labels/generate.rb,
lib/deliveries/couriers/correos_express/shipments/trace.rb,
lib/deliveries/couriers/correos_express/shipments/create.rb,
lib/deliveries/couriers/correos_express/pickups/cutoff_time.rb,
lib/deliveries/couriers/correos_express/pickups/create/defaults.rb,
lib/deliveries/couriers/correos_express/collection_points/search.rb,
lib/deliveries/couriers/correos_express/shipments/create/defaults.rb,
lib/deliveries/couriers/correos_express/pickups/create/format_params.rb,
lib/deliveries/couriers/correos_express/pickups/trace/format_response.rb,
lib/deliveries/couriers/correos_express/shipments/create/format_params.rb,
lib/deliveries/couriers/correos_express/shipments/trace/format_response.rb,
lib/deliveries/couriers/correos_express/pickups/cutoff_time/format_params.rb,
lib/deliveries/couriers/correos_express/collection_points/search/format_response.rb

Defined Under Namespace

Modules: CollectionPoints, Labels, Pickups, Shipments Classes: Address, Config

Constant Summary collapse

COLLECTION_POINTS_ENDPOINT_TEST =
'https://www.correosexpress.com/wspsc/apiRestOficina/v1/oficinas/listadoOficinasCoordenadas'.freeze
COLLECTION_POINTS_ENDPOINT_LIVE =
'https://www.correosexpress.com/wpsc/apiRestOficina/v1/oficinas/listadoOficinasCoordenadas'.freeze
SHIPMENTS_ENDPOINT_LIVE =
'https://www.correosexpress.com/wpsc/apiRestGrabacionEnvio/json/grabacionEnvio'.freeze
SHIPMENTS_ENDPOINT_TEST =
'https://test.correosexpress.com/wspsc/apiRestGrabacionEnvio/json/grabacionEnvio'.freeze
TRACKING_INFO_ENDPOINT_LIVE =
'https://www.correosexpress.com/wpsc/apiRestSeguimientoEnvios/rest/seguimientoEnvios'.freeze
TRACKING_INFO_ENDPOINT_TEST =
'https://test.correosexpress.com/wspsc/apiRestSeguimientoEnvios/rest/seguimientoEnvios'.freeze
LABELS_ENDPOINT_LIVE =
'https://www.cexpr.es/wspsc/apiRestEtiquetaTransporte/json/etiquetaTransporte'.freeze
LABELS_ENDPOINT_TEST =
'https://www.test.cexpr.es/wsps/apiRestEtiquetaTransporte/json/etiquetaTransporte'.freeze
PICKUPS_ENDPOINT_LIVE =
'https://www.correosexpress.com/wpsc/apiRestGrabacionRecogida/json/grabarRecogida'.freeze
PICKUPS_ENDPOINT_TEST =
'https://test.correosexpress.com/wpsn/apiRestGrabacionRecogida/json/grabarRecogida'.freeze
CUTOFF_TIME_ENDPOINT_LIVE =
'https://www.correosexpress.com/wpsc/apiRestGrabacionRecogida/json/horaCorte'.freeze

Class Method Summary collapse

Methods included from Deliveries::Courier

config, configure, configured?, live?, test?

Class Method Details

.create_pickup(sender:, receiver:, parcels:, reference_code:, pickup_date: nil, remarks: nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/deliveries/couriers/correos_express.rb', line 127

def create_pickup(sender:, receiver:, parcels:, reference_code:,
                  pickup_date: nil, remarks: nil, **)
  time_interval = nil
  begin
    params = Pickups::Create::FormatParams.new(
      sender: sender.courierize(:correos_express),
      receiver: receiver.courierize(:correos_express),
      parcels: parcels,
      reference_code: reference_code,
      pickup_date: pickup_date,
      remarks: remarks,
      time_interval: time_interval
    ).execute

    pickup_number = Pickups::Create.new(params: params).execute
  rescue InvalidTimeIntervalError => e
    raise e if time_interval
    raise e unless (cutoff_time = e.message[/\b(\d+):\d{2}\z/, 1])

    time_interval = 9..cutoff_time.to_i
    retry
  end

  Deliveries::Pickup.new(
    courier_id: 'correos_express',
    sender: sender,
    receiver: receiver,
    parcels: parcels,
    reference_code: reference_code,
    tracking_code: pickup_number,
    pickup_date: pickup_date
  )
end

.create_shipment(sender:, receiver:, parcels:, reference_code:, collection_point: nil, shipment_date: nil, remarks: nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/deliveries/couriers/correos_express.rb', line 115

def create_shipment(sender:, receiver:, parcels:, reference_code:, collection_point: nil, shipment_date: nil, remarks: nil, **)
  Shipments::Create.new(
    sender: sender,
    receiver: receiver,
    collection_point: collection_point,
    shipment_date: shipment_date,
    parcels: parcels,
    reference_code: reference_code,
    remarks: remarks
  ).execute
end

.get_collection_point(global_point_id:) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/deliveries/couriers/correos_express.rb', line 60

def get_collection_point(global_point_id:)
  global_point = Deliveries::CollectionPoint.parse_global_point_id(global_point_id: global_point_id)

  collection_points = get_collection_points(postcode: global_point.postcode)
  collection_point = collection_points.select { |col| col.point_id == global_point.point_id }.first

  if collection_point.blank?
    raise Deliveries::APIError.new(
      "Collection Point not found - #{global_point.point_id}",
      1
    )
  end

  collection_point
end

.get_collection_points(postcode:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/deliveries/couriers/correos_express.rb', line 46

def get_collection_points(postcode:, **)
  raise Deliveries::APIError, 'Postcode cannot be null' if postcode.blank?

  collection_points = []

  points = CollectionPoints::Search.new(postcode: postcode).execute
  points.each do |point|
    collection_point_params = CollectionPoints::Search::FormatResponse.new(response: point).execute
    collection_points << Deliveries::CollectionPoint.new(**collection_point_params)
  end

  collection_points
end

.get_label(tracking_code:) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/deliveries/couriers/correos_express.rb', line 95

def get_label(tracking_code:, **)
  pdf = Labels::Generate.new(
    tracking_codes: tracking_code
  ).execute.first

  Deliveries::Label.new(raw: pdf)
end

.get_labels(tracking_codes:) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/deliveries/couriers/correos_express.rb', line 103

def get_labels(tracking_codes:, **)
  labels = Deliveries::Labels.new

  Labels::Generate.new(
    tracking_codes: tracking_codes
  ).execute.each do |pdf|
    labels << pdf
  end

  labels
end

.pickup_info(tracking_code:) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/deliveries/couriers/correos_express.rb', line 85

def pickup_info(tracking_code:, **)
  response = Pickups::Trace.new(
    tracking_code: tracking_code
  ).execute

  tracking_info_params = Pickups::Trace::FormatResponse.new(response: response).execute

  Deliveries::TrackingInfo.new(**tracking_info_params)
end

.shipment_info(tracking_code:) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/deliveries/couriers/correos_express.rb', line 76

def shipment_info(tracking_code:, **)
  response = Shipments::Trace.new(
    tracking_code: tracking_code
  ).execute

  tracking_info_params = Shipments::Trace::FormatResponse.new(response: response).execute
  Deliveries::TrackingInfo.new(**tracking_info_params)
end