Module: EasyPost::Util

Defined in:
lib/easypost/util.rb

Class Method Summary collapse

Class Method Details

.convert_to_easypost_object(response, api_key, parent = nil, name = nil) ⇒ Object



23
24
25
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
52
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/easypost/util.rb', line 23

def self.convert_to_easypost_object(response, api_key, parent=nil, name=nil)
  types = {
    'Address' => Address,
    'ScanForm' => ScanForm,
    'CustomsItem' => CustomsItem,
    'CustomsInfo' => CustomsInfo,
    'Parcel' => Parcel,
    'Shipment' => Shipment,
    'Rate' => Rate,
    'Refund' => Refund,
    'Event' => Event,
    'Batch' => Batch,
    'Tracker' => Tracker,
    'Item' => Item,
    'Insurance' => Insurance,
    'Order' => Order,
    'Pickup' => Pickup,
    'PickupRate' => PickupRate,
    'PostageLabel' => PostageLabel,
    'Printer' => Printer,
    'PrintJob' => PrintJob,
    'CarrierAccount' => CarrierAccount,
    'User' => User,
    'Report' => Report,
    'ShipmentReport' => Report,
    'PaymentLogReport' => Report,
    'TrackerReport' => Report,
    'RefundReport' => Report,
    'ShipmentInvoiceReport' => Report,
    'Webhook' => Webhook
  }

  prefixes = {
    'adr' => Address,
    'sf' => ScanForm,
    'cstitem' => CustomsItem,
    'cstinfo' => CustomsInfo,
    'prcl' => Parcel,
    'shp' => Shipment,
    'rate' => Rate,
    'rfnd' => Refund,
    'evt' => Event,
    'batch' => Batch,
    'trk' => Tracker,
    'item' => Item,
    'ins' => Insurance,
    'order' => Order,
    'pickup' => Pickup,
    'pickuprate' => PickupRate,
    'pl' => PostageLabel,
    'printer' => Printer,
    'printjob' => PrintJob,
    'ca' => CarrierAccount,
    'user' => User,
    'shprep' => Report,
    'plrep' => Report,
    'trkrep' => Report,
    'refrep' => Report,
    'shpinvrep' => Report,
    'hook' => Webhook
  }

  case response
  when Array
    return response.map { |i| convert_to_easypost_object(i, api_key, parent) }
  when Hash
    if cls_name = response[:object]
      cls = types[cls_name]
    elsif response[:id]
      if response[:id].index('_').nil?
        cls = EasyPostObject
      elsif cls_prefix = response[:id][0..response[:id].index('_')]
        cls = prefixes[cls_prefix[0..-2]]
      end
    elsif response['id']
      if response['id'].index('_').nil?
        cls = EasyPostObject
      elsif cls_prefix = response['id'][0..response['id'].index('_')]
        cls = prefixes[cls_prefix[0..-2]]
      end
    end

    cls ||= EasyPostObject
    return cls.construct_from(response, api_key, parent, name)
  else
    return response
  end
end

.normalize_string_list(lst) ⇒ Object



18
19
20
21
# File 'lib/easypost/util.rb', line 18

def self.normalize_string_list(lst)
  lst = lst.is_a?(String) ? lst.split(',') : Array(lst)
  lst.map(&:to_s).map(&:downcase).map(&:strip)
end

.objects_to_ids(obj) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/easypost/util.rb', line 3

def self.objects_to_ids(obj)
  case obj
  when Resource
    return {:id => obj.id}
  when Hash
    result = {}
    obj.each { |k, v| result[k] = objects_to_ids(v) unless v.nil? }
    return result
  when Array
    return obj.map { |v| objects_to_ids(v) }
  else
    return obj
  end
end