Exception: Helper

Inherits:
CorreiosException show all
Defined in:
lib/auxiliars/helper.rb

Instance Method Summary collapse

Methods inherited from CorreiosException

#generate_exception

Instance Method Details

#array_to_string(array) ⇒ Object



123
124
125
126
127
# File 'lib/auxiliars/helper.rb', line 123

def array_to_string(array)
  return nil if array.nil?

  array.join('')
end

#array_to_string_comma(array) ⇒ Object



117
118
119
120
121
# File 'lib/auxiliars/helper.rb', line 117

def array_to_string_comma(array)
  return nil if array.nil?

  array.join(',')
end

#bool_to_int(bool) ⇒ Object



84
85
86
# File 'lib/auxiliars/helper.rb', line 84

def bool_to_int(bool)
  bool ? 1 : 0
end

#bool_to_string(bool) ⇒ Object



80
81
82
# File 'lib/auxiliars/helper.rb', line 80

def bool_to_string(bool)
  bool ? 'S' : 'N'
end

#calculate_reverse_shipping_deadline(value, type) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/auxiliars/helper.rb', line 47

def calculate_reverse_shipping_deadline(value, type)
  if type == :authorization
    return value if value.is_a?(Numeric)

    Date.today - value
  else
    return (Date.today + value).strftime('%d/%m/%Y') if value.is_a?(Numeric)

    value.strftime('%d/%m/%Y')
  end
end

#calculate_shipping_deadline(days, saturday, date = Date.today) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/auxiliars/helper.rb', line 59

def calculate_shipping_deadline(days, saturday, date = Date.today)
  if saturday
    days.to_i.times do
      date += 1.days
      date += 1.days if date.sunday?
    end
  else
    days.to_i.times do
      date += 1.days
      date += 1.days if deadline.sunday? || deadline.saturday?
    end
  end
  date
end

#date_to_string(date) ⇒ Object



101
102
103
# File 'lib/auxiliars/helper.rb', line 101

def date_to_string(date)
  date.strftime('%d/%m/%Y')
end

#decimal_to_string(decimal) ⇒ Object



111
112
113
114
115
# File 'lib/auxiliars/helper.rb', line 111

def decimal_to_string(decimal)
  return nil if decimal.nil?

  decimal.to_s.tr('.', ',')
end

#generate_http_exception(status) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/auxiliars/helper.rb', line 13

def generate_http_exception(status)
  case status
  when 400
    generate_exception("Bad request. Status code #{status}")
  when 401
    generate_exception("Access unauthorized. Status code #{status}")
  when 404
    generate_exception("Data or method not found. Status code #{status}")
  when 500
    generate_exception("Internal server error. Status code #{status}")
  when 503
    generate_exception("Service unavailable. Status code #{status}")
  when 504
    generate_exception("Gateway timeout. Status code #{status}")
  else
    generate_exception("Unknown HTTP error. Status code #{status}")
  end
end

#generate_revese_logistics_exception(response) ⇒ Object



32
33
34
35
36
# File 'lib/auxiliars/helper.rb', line 32

def generate_revese_logistics_exception(response)
  unless response[:cod_erro].to_i.zero?
    generate_exception(response[:msg_erro].capitalize)
  end
end

#generate_soap_fault_exception(message) ⇒ Object



7
8
9
10
11
# File 'lib/auxiliars/helper.rb', line 7

def generate_soap_fault_exception(message)
  message = message.to_s.gsub('(soap:Server)', '')
  message = message.strip
  generate_exception(message.capitalize)
end

#generate_sro_exception(objects) ⇒ Object



38
39
40
# File 'lib/auxiliars/helper.rb', line 38

def generate_sro_exception(objects)
  generate_exception(objects.first[:erro]) if objects.first[:numero] == 'Erro'
end

#inverse_card_status(status) ⇒ Object

Inverted enumerizers



263
264
265
266
267
268
269
270
# File 'lib/auxiliars/helper.rb', line 263

def inverse_card_status(status)
  case status
  when 'Normal'
    :ok
  when 'Cancelado'
    :canceled
  end
end

#inverse_object_type(type) ⇒ Object



296
297
298
299
300
301
302
303
304
305
# File 'lib/auxiliars/helper.rb', line 296

def inverse_object_type(type)
  case type
  when '1'
    :letter_envelope
  when '2'
    :box_prism
  when '3'
    :cylinder
  end
end

#inverse_payment_method(method) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/auxiliars/helper.rb', line 279

def inverse_payment_method(method)
  case method
  when '1'
    :postal_vouncher
  when '2'
    :postal_refound
  when '3'
    :exchange_contract
  when '4'
    :credit_card
  when '5'
    :other
  when nil
    :to_bill
  end
end

#inverse_reverse_shipping_type(type) ⇒ Object



328
329
330
331
332
333
334
335
# File 'lib/auxiliars/helper.rb', line 328

def inverse_reverse_shipping_type(type)
  case type
  when 'A'
    :authorization
  when 'C'
    :pickup
  end
end

#inverse_service_availability(availability) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/auxiliars/helper.rb', line 307

def inverse_service_availability(availability)
  case availability.to_i
  when 0, 11
    :available
  when -2, -3
    :invalid_zip_code
  when -33
    :system_down
  when -34, -35, 1
    :incorrect_data
  when -36, -37, -38
    :unauthorized
  when -888, 6, 7, 8, 9, 12
    :unavailable
  when 10
    :partially_available
  when 99
    :error
  end
end

#inverse_shipping_cancellation(status) ⇒ Object



272
273
274
275
276
277
# File 'lib/auxiliars/helper.rb', line 272

def inverse_shipping_cancellation(status)
  case status
  when 'Registro gravado'
    :ok
  end
end

#inverse_tracking_event_status(event) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/auxiliars/helper.rb', line 337

def inverse_tracking_event_status(event)
  type = event['tipo'] || event[:tipo]
  status = event['status'] || event[:status]
  status = status.to_i

  case type
  when 'BDE', 'BDI', 'BDR'
    case status
    when 0, 1
      :delivered
    when 2
      :not_delivered
    when 4, 5, 6, 8, 10, 21, 22, 26, 33, 36, 40, 42, 48, 49, 56
      :returning
    when 7, 19, 25, 32, 38, 41, 45, 46, 47, 53, 57, 66, 69
      :in_transit
    when 9, 50, 51, 52
      :stolen_lost
    when 3, 12, 24
      :awaiting_pick_up
    when 20, 34, 35
      :not_delivered
    when 23
      :returned
    when 28, 37
      :damaged
    when 43
      :arrested
    when 54, 55, 58, 59
      :taxing
    end
  when 'BLQ', 'PMT', 'CD', 'CMT', 'TRI', 'CUN', 'RO', 'DO', 'EST', 'PAR'
    case status
    when 0, 1, 2, 3, 4, 5, 6, 9, 15, 16, 17, 18
      :in_transit
    end
  when 'FC'
    case status
    when 1
      :returning
    when 2, 3, 5, 7
      :in_transit
    when 4
      :not_delivered
    end
  when 'IDC'
    case status
    when 1, 2, 3, 4, 5, 6, 7
      :stolen_lost
    end
  when 'LDI'
    case status
    when 0, 1, 2, 3, 14
      :awaiting_pick_up
    end
  when 'OEC', 'LDE'
    case status
    when 0, 1, 9
      :delivering
    end
  when 'PO', 'CO'
    case status
    when 0, 1, 9
      :posted
    end
  end
end

#object_type(type) ⇒ Object

Enumerizers



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/auxiliars/helper.rb', line 131

def object_type(type)
  case type
  when :letter_envelope
    '001'
  when :box
    '002'
  when :prism
    '002'
  when :cylinder
    '003'
  else
    generate_exception('Object type not in list.')
  end
end

#payment_method(method) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/auxiliars/helper.rb', line 161

def payment_method(method)
  case method
  when :postal_vouncher
    1
  when :postal_refound
    2
  when :exchange_contract
    3
  when :credit_card
    4
  when :other
    5
  when :to_bill
    nil
  else
    generate_exception('Payment method not in list.')
  end
end

#pricefier_object_type(type) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/auxiliars/helper.rb', line 146

def pricefier_object_type(type)
  case type
  when :letter_envelope
    3
  when :box
    1
  when :prism
    1
  when :cylinder
    2
  else
    generate_exception('Object type not in list.')
  end
end

#remove_label_digit_checker(label_number) ⇒ Object



42
43
44
45
# File 'lib/auxiliars/helper.rb', line 42

def remove_label_digit_checker(label_number)
  label_number.slice!(10)
  label_number
end

#reverse_shipping_service(service) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/auxiliars/helper.rb', line 204

def reverse_shipping_service(service)
  case service
  when :pac
    'LR'
  when :sedex
    'LS'
  when :e_sedex
    'LV'
  else
    service
  end
end

#reverse_shipping_type(type) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/auxiliars/helper.rb', line 180

def reverse_shipping_type(type)
  case type
  when :authorization
    'A'
  when :pickup
    'C'
  when :authorization_pickup
    'CA'
  else
    generate_exception('Shipping type not in list.')
  end
end

#reverse_tracking_result_type(result_type) ⇒ Object



217
218
219
220
221
222
223
224
225
226
# File 'lib/auxiliars/helper.rb', line 217

def reverse_tracking_result_type(result_type)
  case result_type
  when :last_event
    'U'
  when :all_events
    'H'
  else
    generate_exception('Tracking result type not in list.')
  end
end

#string_to_bool(string) ⇒ Object

Converters



76
77
78
# File 'lib/auxiliars/helper.rb', line 76

def string_to_bool(string)
  string.strip == 'S'
end

#string_to_date(date) ⇒ Object



97
98
99
# File 'lib/auxiliars/helper.rb', line 97

def string_to_date(date)
  Date.strptime(date, '%d/%m/%Y')
end

#string_to_decimal(string) ⇒ Object



105
106
107
108
109
# File 'lib/auxiliars/helper.rb', line 105

def string_to_decimal(string)
  return nil if string.nil?

  string.tr(',', '.').to_f
end

#string_to_time(date, time) ⇒ Object



88
89
90
91
# File 'lib/auxiliars/helper.rb', line 88

def string_to_time(date, time)
  time = time.strftime('%H:%M:%S')
  Time.strptime("#{date} #{time}", '%d-%m-%Y %H:%M:%S')
end

#string_to_time_no_second(date, time) ⇒ Object



93
94
95
# File 'lib/auxiliars/helper.rb', line 93

def string_to_time_no_second(date, time)
  Time.strptime("#{date} #{time}", '%d/%m/%Y %H:%M')
end

#ticket_type(type) ⇒ Object



193
194
195
196
197
198
199
200
201
202
# File 'lib/auxiliars/helper.rb', line 193

def ticket_type(type)
  case type
  when :authorization
    'AP'
  when :pickup
    'LR'
  else
    generate_exception('Tickect type not in list.')
  end
end

#tracking_language(language) ⇒ Object



250
251
252
253
254
255
256
257
258
259
# File 'lib/auxiliars/helper.rb', line 250

def tracking_language(language)
  case language
  when :portuguese
    '101'
  when :english
    '102'
  else
    generate_exception('Language not in list.')
  end
end

#tracking_query_type(query_type) ⇒ Object



228
229
230
231
232
233
234
235
236
237
# File 'lib/auxiliars/helper.rb', line 228

def tracking_query_type(query_type)
  case query_type
  when :list
    'L'
  when :range
    'F'
  else
    generate_exception('Query type not in list.')
  end
end

#tracking_result_type(result_type) ⇒ Object



239
240
241
242
243
244
245
246
247
248
# File 'lib/auxiliars/helper.rb', line 239

def tracking_result_type(result_type)
  case result_type
  when :last_event
    'U'
  when :all_events
    'T'
  else
    generate_exception('Query type not in list.')
  end
end