Module: EasyPost::Util

Defined in:
lib/easypost/util.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#os_archObject

Returns the value of attribute os_arch.



2
3
4
# File 'lib/easypost/util.rb', line 2

def os_arch
  @os_arch
end

#os_nameObject

Returns the value of attribute os_name.



2
3
4
# File 'lib/easypost/util.rb', line 2

def os_name
  @os_name
end

#os_versionObject

Returns the value of attribute os_version.



2
3
4
# File 'lib/easypost/util.rb', line 2

def os_version
  @os_version
end

Class Method Details

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



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/easypost/util.rb', line 45

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

  prefixes = {
    'adr' => EasyPost::Address,
    'batch' => EasyPost::Batch,
    'ca' => EasyPost::CarrierAccount,
    'cstinfo' => EasyPost::CustomsInfo,
    'cstitem' => EasyPost::CustomsItem,
    '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,
    'printer' => EasyPost::Printer,
    'printjob' => EasyPost::PrintJob,
    '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
    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 = 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
    return cls.construct_from(response, api_key, parent, name)
  else
    return response
  end
end

.normalize_string_list(lst) ⇒ Object



40
41
42
43
# File 'lib/easypost/util.rb', line 40

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/easypost/util.rb', line 25

def self.objects_to_ids(obj)
  case obj
  when EasyPost::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

.os_archObject



21
22
23
# File 'lib/easypost/util.rb', line 21

def self.os_arch
  Gem::Platform.local.cpu
end

.os_nameObject



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

def self.os_name
  case RUBY_PLATFORM
  when /linux/i
    'Linux'
  when /darwin/i
    'Darwin'
  when /cygwin|mswin|mingw|bccwin|wince|emx/i
    'Windows'
  else
    'Unknown'
  end
end

.os_versionObject



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

def self.os_version
  Gem::Platform.local.version
end