Module: EasyPost::Util

Defined in:
lib/easypost/util.rb

Overview

Internal utilities helpful for this libraries operation.

Class Method Summary collapse

Class Method Details

.build_dict_key(keys) ⇒ Object

Build a dict key from a list of keys. Example: [code, number] -> code



24
25
26
27
28
29
30
31
32
# File 'lib/easypost/util.rb', line 24

def self.build_dict_key(keys)
  result = keys[0].to_s

  keys[1..-1].each do |key|
    result += "[#{key}]"
  end

  result
end

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

Convert data to an EasyPost Object.



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/easypost/util.rb', line 57

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

  prefixes = {
    'adr' => EasyPost::Address,
    'batch' => EasyPost::Batch,
    'brd' => EasyPost::Brand,
    'ca' => EasyPost::CarrierAccount,
    'cstinfo' => EasyPost::CustomsInfo,
    'cstitem' => EasyPost::CustomsItem,
    'es' => EasyPost::Beta::EndShipper,
    'evt' => EasyPost::Event,
    'hook' => EasyPost::Webhook,
    'ins' => EasyPost::Insurance,
    'order' => EasyPost::Order,
    'pickup' => EasyPost::Pickup,
    'pickuprate' => EasyPost::PickupRate,
    'pl' => EasyPost::PostageLabel,
    'plrep' => EasyPost::Report,
    'prcl' => EasyPost::Parcel,
    'rate' => EasyPost::Rate,
    'refrep' => EasyPost::Report,
    'rfnd' => EasyPost::Refund,
    'sf' => EasyPost::ScanForm,
    'shp' => EasyPost::Shipment,
    'shpinvrep' => EasyPost::Report,
    'shprep' => EasyPost::Report,
    'trk' => EasyPost::Tracker,
    'trkrep' => EasyPost::Report,
    'user' => EasyPost::User,
  }

  case response
  when Array
    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 = EasyPost::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 = EasyPost::EasyPostObject
      elsif (cls_prefix = response['id'][0..response['id'].index('_')])
        cls = prefixes[cls_prefix[0..-2]]
      end
    end

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

.form_encode_params(hash, parent_keys = [], parent_dict = {}) ⇒ Object

Form-encode a multi-layer dictionary to a one-layer dictionary.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/easypost/util.rb', line 6

def self.form_encode_params(hash, parent_keys = [], parent_dict = {})
  result = parent_dict or {}
  keys = parent_keys or []

  hash.each do |key, value|
    if value.instance_of?(Hash)
      keys << key
      result = form_encode_params(value, keys, result)
    else
      dict_key = build_dict_key(keys + [key])
      result[dict_key] = value
    end
  end
  result
end

.get_lowest_object_rate(easypost_object, carriers = [], services = [], rates_key = 'rates') ⇒ Object

Gets the lowest rate of an EasyPost object such as a Shipment, Order, or Pickup. You can exclude by having ‘’!‘` as the first element of your optional filter lists

Raises:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/easypost/util.rb', line 148

def self.get_lowest_object_rate(easypost_object, carriers = [], services = [], rates_key = 'rates')
  lowest_rate = nil

  carriers = EasyPost::Util.normalize_string_list(carriers)
  negative_carriers = []
  carriers_copy = carriers.clone
  carriers_copy.each do |carrier|
    if carrier[0, 1] == '!'
      negative_carriers << carrier[1..-1]
      carriers.delete(carrier)
    end
  end

  services = EasyPost::Util.normalize_string_list(services)
  negative_services = []
  services_copy = services.clone
  services_copy.each do |service|
    if service[0, 1] == '!'
      negative_services << service[1..-1]
      services.delete(service)
    end
  end

  easypost_object[rates_key].each do |rate|
    rate_carrier = rate.carrier.downcase
    if carriers.size.positive? && !carriers.include?(rate_carrier)
      next
    end
    if negative_carriers.size.positive? && negative_carriers.include?(rate_carrier)
      next
    end

    rate_service = rate.service.downcase
    if services.size.positive? && !services.include?(rate_service)
      next
    end
    if negative_services.size.positive? && negative_services.include?(rate_service)
      next
    end

    if lowest_rate.nil? || rate.rate.to_f < lowest_rate.rate.to_f
      lowest_rate = rate
    end
  end

  raise EasyPost::Error.new('No rates found.') if lowest_rate.nil?

  lowest_rate
end

.normalize_string_list(lst) ⇒ Object

Normalizes a list of strings.



51
52
53
54
# File 'lib/easypost/util.rb', line 51

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

Converts an object to an object ID.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/easypost/util.rb', line 35

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