easypost 3.0.1 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +22 -0
  3. data/CHANGELOG +44 -3
  4. data/README.md +2 -1
  5. data/VERSION +1 -1
  6. data/easypost.gemspec +24 -20
  7. data/lib/easypost.rb +109 -118
  8. data/lib/easypost/address.rb +47 -47
  9. data/lib/easypost/batch.rb +34 -38
  10. data/lib/easypost/carrier_account.rb +3 -6
  11. data/lib/easypost/carrier_type.rb +2 -0
  12. data/lib/easypost/customs_info.rb +3 -2
  13. data/lib/easypost/customs_item.rb +3 -2
  14. data/lib/easypost/error.rb +25 -33
  15. data/lib/easypost/event.rb +3 -6
  16. data/lib/easypost/insurance.rb +1 -3
  17. data/lib/easypost/item.rb +4 -8
  18. data/lib/easypost/object.rb +111 -113
  19. data/lib/easypost/order.rb +20 -18
  20. data/lib/easypost/parcel.rb +1 -3
  21. data/lib/easypost/pickup.rb +19 -18
  22. data/lib/easypost/pickup_rate.rb +1 -3
  23. data/lib/easypost/postage_label.rb +1 -3
  24. data/lib/easypost/print_job.rb +1 -5
  25. data/lib/easypost/printer.rb +18 -22
  26. data/lib/easypost/rate.rb +1 -3
  27. data/lib/easypost/refund.rb +1 -3
  28. data/lib/easypost/report.rb +22 -24
  29. data/lib/easypost/resource.rb +58 -60
  30. data/lib/easypost/scan_form.rb +4 -6
  31. data/lib/easypost/shipment.rb +90 -92
  32. data/lib/easypost/tracker.rb +10 -12
  33. data/lib/easypost/user.rb +33 -37
  34. data/lib/easypost/util.rb +99 -136
  35. data/lib/easypost/webhook.rb +22 -21
  36. metadata +35 -62
  37. data/circle.yml +0 -3
  38. data/spec/address_spec.rb +0 -81
  39. data/spec/batch_spec.rb +0 -48
  40. data/spec/carrier_account_spec.rb +0 -121
  41. data/spec/insurance_spec.rb +0 -69
  42. data/spec/item_spec.rb +0 -105
  43. data/spec/order_spec.rb +0 -58
  44. data/spec/pickup_spec.rb +0 -83
  45. data/spec/report_spec.rb +0 -59
  46. data/spec/scan_form_spec.rb +0 -46
  47. data/spec/shipment_spec.rb +0 -144
  48. data/spec/spec_helper.rb +0 -10
  49. data/spec/support/constant.rb +0 -106
  50. data/spec/tracker_spec.rb +0 -94
  51. data/spec/user_spec.rb +0 -88
  52. data/spec/webhook_spec.rb +0 -75
@@ -1,53 +1,49 @@
1
- module EasyPost
2
- class Batch < Resource
1
+ class EasyPost::Batch < EasyPost::Resource
2
+ def self.create_and_buy(params={}, api_key=nil)
3
+ wrapped_params = {}
4
+ wrapped_params[self.class_name().to_sym] = params
5
+ response = EasyPost.make_request(:post, url + '/create_and_buy', api_key, wrapped_params)
3
6
 
4
- def self.create_and_buy(params={}, api_key=nil)
5
- wrapped_params = {}
6
- wrapped_params[self.class_name().to_sym] = params
7
- response, api_key = EasyPost.request(:post, url + '/create_and_buy', api_key, wrapped_params)
8
-
9
- return Util.convert_to_easypost_object(response, api_key)
10
- end
11
-
12
- def buy(params={})
13
- response, api_key = EasyPost.request(:post, url + '/buy', @api_key, params)
14
- self.refresh_from(response, @api_key, true)
7
+ return EasyPost::Util.convert_to_easypost_object(response, api_key)
8
+ end
15
9
 
16
- return self
17
- end
10
+ def buy(params={})
11
+ response = EasyPost.make_request(:post, url + '/buy', @api_key, params)
12
+ self.refresh_from(response, @api_key, true)
18
13
 
19
- def label(params={})
20
- response, api_key = EasyPost.request(:post, url + '/label', @api_key, params)
21
- self.refresh_from(response, @api_key, true)
14
+ return self
15
+ end
22
16
 
23
- return self
24
- end
17
+ def label(params={})
18
+ response = EasyPost.make_request(:post, url + '/label', @api_key, params)
19
+ self.refresh_from(response, @api_key, true)
25
20
 
26
- def remove_shipments(params={})
27
- response, api_key = EasyPost.request(:post, url + '/remove_shipments', @api_key, params)
28
- self.refresh_from(response, @api_key, true)
21
+ return self
22
+ end
29
23
 
30
- return self
31
- end
24
+ def remove_shipments(params={})
25
+ response = EasyPost.make_request(:post, url + '/remove_shipments', @api_key, params)
26
+ self.refresh_from(response, @api_key, true)
32
27
 
33
- def add_shipments(params={})
34
- response, api_key = EasyPost.request(:post, url + '/add_shipments', @api_key, params)
35
- self.refresh_from(response, @api_key, true)
28
+ return self
29
+ end
36
30
 
37
- return self
38
- end
31
+ def add_shipments(params={})
32
+ response = EasyPost.make_request(:post, url + '/add_shipments', @api_key, params)
33
+ self.refresh_from(response, @api_key, true)
39
34
 
40
- def stamp_and_barcode_by_reference(params={})
41
- response, api_key = EasyPost.request(:get, url + '/stamp_and_barcode_by_reference', @api_key, params)
35
+ return self
36
+ end
42
37
 
43
- return response
44
- end
38
+ def stamp_and_barcode_by_reference(params={})
39
+ response = EasyPost.make_request(:get, url + '/stamp_and_barcode_by_reference', @api_key, params)
45
40
 
46
- def create_scan_form(params={})
47
- response, api_key = EasyPost.request(:post, url + '/scan_form', @api_key, params)
41
+ return response
42
+ end
48
43
 
49
- return response
50
- end
44
+ def create_scan_form(params={})
45
+ response = EasyPost.make_request(:post, url + '/scan_form', @api_key, params)
51
46
 
47
+ return response
52
48
  end
53
49
  end
@@ -1,8 +1,5 @@
1
- module EasyPost
2
- class CarrierAccount < Resource
3
- def self.types
4
- response, api_key = EasyPost.request(:get, "/carrier_types", @api_key)
5
- return Util.convert_to_easypost_object(response, api_key)
6
- end
1
+ class EasyPost::CarrierAccount < EasyPost::Resource
2
+ def self.types
3
+ EasyPost::CarrierType.all
7
4
  end
8
5
  end
@@ -0,0 +1,2 @@
1
+ class EasyPost::CarrierType < EasyPost::Resource
2
+ end
@@ -1,4 +1,5 @@
1
- module EasyPost
2
- class CustomsInfo < Resource
1
+ class EasyPost::CustomsInfo < EasyPost::Resource
2
+ def self.all(filters={}, api_key=nil)
3
+ raise NotImplementedError.new('CustomsInfo.all not implemented.')
3
4
  end
4
5
  end
@@ -1,4 +1,5 @@
1
- module EasyPost
2
- class CustomsItem < Resource
1
+ class EasyPost::CustomsItem < EasyPost::Resource
2
+ def self.all(filters={}, api_key=nil)
3
+ raise NotImplementedError.new('CustomsItem.all not implemented.')
3
4
  end
4
5
  end
@@ -1,39 +1,31 @@
1
- module EasyPost
2
- class Error < StandardError
3
- attr_reader :message
4
- attr_reader :http_status
5
- attr_reader :http_body
6
- attr_reader :json_body
7
- attr_reader :param
8
- attr_reader :code
9
- attr_reader :errors
1
+ class EasyPost::Error < StandardError
2
+ attr_reader :message
3
+ attr_reader :status
4
+ attr_reader :http_status # deprecated
5
+ attr_reader :http_body
6
+ attr_reader :code
7
+ attr_reader :errors
10
8
 
11
- def initialize(message=nil, http_status=nil, http_body=nil, json_body={})
12
- @message = message
13
- @http_status = http_status
14
- @http_body = http_body
15
- @json_body = json_body
9
+ def initialize(message = nil, status = nil, code = nil, errors = nil, http_body = nil)
10
+ @message = message
11
+ @status = status
12
+ @http_status = status # deprecated
13
+ @code = code
14
+ @errors = errors
15
+ @http_body = http_body
16
16
 
17
- @param = @json_body.fetch(:error, {}).fetch(:param, nil)
18
- @code = @json_body.fetch(:error, {}).fetch(:code, nil)
19
- @errors = @json_body.fetch(:error, {}).fetch(:errors, nil)
17
+ super(message)
18
+ end
20
19
 
21
- super(message)
22
- end
20
+ def to_s
21
+ "#{code} (#{status}): #{message} #{errors}".strip
22
+ end
23
23
 
24
- def to_s
25
- s = "#{@code} (#{@http_status}): #{@message}";
26
- if @errors
27
- s += "\nField errors:\n"
28
- @errors.each do |field_error|
29
- field_error.each do |k, v|
30
- s += " #{k}: #{v}\n"
31
- end
32
- s += "\n"
33
- end
34
- end
35
- s
36
- end
24
+ def ==(other)
25
+ other.is_a?(EasyPost::Error) &&
26
+ message == other.message &&
27
+ status == other.status &&
28
+ code == other.code &&
29
+ errors == other.errors
37
30
  end
38
31
  end
39
-
@@ -1,10 +1,7 @@
1
1
  require 'json'
2
2
 
3
- module EasyPost
4
- class Event < Resource
5
-
6
- def self.receive(values)
7
- return EasyPost::Util::convert_to_easypost_object(JSON.parse(values), nil)
8
- end
3
+ class EasyPost::Event < EasyPost::Resource
4
+ def self.receive(values)
5
+ return EasyPost::Util::convert_to_easypost_object(JSON.parse(values), nil)
9
6
  end
10
7
  end
@@ -1,4 +1,2 @@
1
- module EasyPost
2
- class Insurance < Resource
3
- end
1
+ class EasyPost::Insurance < EasyPost::Resource
4
2
  end
data/lib/easypost/item.rb CHANGED
@@ -1,10 +1,6 @@
1
- module EasyPost
2
- class Item < Resource
3
-
4
- def self.retrieve_reference(params={}, api_key=nil)
5
- response, api_key = EasyPost.request(:get, url + '/retrieve_reference', api_key, params)
6
- return EasyPost::Util::convert_to_easypost_object(response, api_key) if response
7
- end
8
-
1
+ class EasyPost::Item < EasyPost::Resource
2
+ def self.retrieve_reference(params={}, api_key=nil)
3
+ response = EasyPost.make_request(:get, url + '/retrieve_reference', api_key, params)
4
+ return EasyPost::Util::convert_to_easypost_object(response, api_key) if response
9
5
  end
10
6
  end
@@ -1,149 +1,147 @@
1
- module EasyPost
2
- class EasyPostObject
3
- include Enumerable
4
-
5
- attr_accessor :parent, :name, :api_key, :unsaved_values
6
- @@immutable_values = Set.new([:api_key, :id])
7
-
8
- def initialize(id=nil, api_key=nil, parent=nil, name=nil)
9
- @api_key = api_key
10
- @values = {}
11
- @unsaved_values = Set.new
12
- @transient_values = Set.new
13
- @parent = parent
14
- @name = name
15
- self.id = id if id
16
- end
17
-
18
- def self.construct_from(values, api_key=nil, parent=nil, name=nil)
19
- obj = self.new(values[:id], api_key, parent, name)
20
- obj.refresh_from(values, api_key)
21
- obj
22
- end
1
+ require "set"
2
+
3
+ class EasyPost::EasyPostObject
4
+ include Enumerable
5
+
6
+ attr_accessor :parent, :name, :api_key, :unsaved_values
7
+ @@immutable_values = Set.new([:api_key, :id])
8
+
9
+ def initialize(id=nil, api_key=nil, parent=nil, name=nil)
10
+ @api_key = api_key
11
+ @values = {}
12
+ @unsaved_values = Set.new
13
+ @transient_values = Set.new
14
+ @parent = parent
15
+ @name = name
16
+ self.id = id if id
17
+ end
23
18
 
24
- def to_s(*args)
25
- MultiJson.dump(@values, :pretty => true)
26
- end
19
+ def self.construct_from(values, api_key=nil, parent=nil, name=nil)
20
+ obj = self.new(values[:id], api_key, parent, name)
21
+ obj.refresh_from(values, api_key)
22
+ obj
23
+ end
27
24
 
28
- def inspect()
29
- id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
30
- "#<#{self.class}:#{id_string}> JSON: " + MultiJson.dump(@values, :pretty => true)
31
- end
25
+ def to_s(*args)
26
+ JSON.dump(@values)
27
+ end
32
28
 
33
- def refresh_from(values, api_key, partial=false)
34
- @api_key = api_key
29
+ def inspect
30
+ id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
31
+ "#<#{self.class}:#{id_string}> JSON: " + to_json
32
+ end
35
33
 
36
- added = Set.new(values.keys - @values.keys)
34
+ def refresh_from(values, api_key, partial=false)
35
+ @api_key = api_key
37
36
 
38
- instance_eval do
39
- add_accessors(added)
40
- end
37
+ added = Set.new(values.keys - @values.keys)
41
38
 
42
- values.each do |k, v|
43
- @values[k] = Util.convert_to_easypost_object(v, api_key, self, k)
44
- @transient_values.delete(k)
45
- @unsaved_values.delete(k)
46
- end
39
+ instance_eval do
40
+ add_accessors(added)
47
41
  end
48
42
 
49
- def [](k)
50
- k = k.to_sym if k.is_a?(String)
51
- @values[k]
43
+ values.each do |k, v|
44
+ @values[k.to_s] = EasyPost::Util.convert_to_easypost_object(v, api_key, self, k)
45
+ @transient_values.delete(k)
46
+ @unsaved_values.delete(k)
52
47
  end
48
+ end
53
49
 
54
- def []=(k, v)
55
- send(:"#{k}=", v)
56
- end
50
+ def [](k)
51
+ @values[k.to_s]
52
+ end
57
53
 
58
- def keys
59
- @values.keys
60
- end
54
+ def []=(k, v)
55
+ send(:"#{k}=", v)
56
+ end
61
57
 
62
- def values
63
- @values.values
64
- end
58
+ def keys
59
+ @values.keys
60
+ end
65
61
 
66
- def to_json(options = {})
67
- MultiJson.dump(@values)
68
- end
62
+ def values
63
+ @values.values
64
+ end
69
65
 
70
- def as_json(options = {})
71
- @values.as_json
72
- end
66
+ def to_json(options = {})
67
+ JSON.dump(@values)
68
+ end
73
69
 
74
- def to_hash
75
- @values
76
- end
70
+ def as_json(options = {})
71
+ @values.as_json
72
+ end
77
73
 
78
- def each(&blk)
79
- @values.each(&blk)
80
- end
74
+ def to_hash
75
+ @values
76
+ end
81
77
 
82
- def id=(id)
83
- @values[:id] = id
84
- end
78
+ def each(&blk)
79
+ @values.each(&blk)
80
+ end
85
81
 
86
- def id
87
- @values[:id]
88
- end
82
+ def id=(id)
83
+ @values[:id] = id
84
+ end
89
85
 
90
- protected
86
+ def id
87
+ @values[:id]
88
+ end
91
89
 
92
- def flatten_unsaved
93
- values = {}
94
- for key in @unsaved_values
95
- value = @values[key]
90
+ protected
96
91
 
97
- values[key] = value
92
+ def flatten_unsaved
93
+ values = {}
94
+ for key in @unsaved_values
95
+ value = @values[key]
98
96
 
99
- if value.is_a?(EasyPost::EasyPostObject)
100
- values[key] = flatten_unsaved(value)
101
- end
102
- end
97
+ values[key] = value
103
98
 
104
- return values
99
+ if value.is_a?(EasyPost::EasyPostObject)
100
+ values[key] = flatten_unsaved(value)
101
+ end
105
102
  end
106
103
 
107
- def metaclass
108
- class << self; self; end
109
- end
104
+ return values
105
+ end
110
106
 
111
- def remove_accessors(keys)
112
- metaclass.instance_eval do
113
- keys.each do |k|
114
- next if @@immutable_values.include?(k)
115
- k_eq = :"#{k}="
116
- remove_method(k) if method_defined?(k)
117
- remove_method(k_eq) if method_defined?(k_eq)
118
- end
107
+ def metaclass
108
+ class << self; self; end
109
+ end
110
+
111
+ def remove_accessors(keys)
112
+ metaclass.instance_eval do
113
+ keys.each do |k|
114
+ next if @@immutable_values.include?(k)
115
+ k_eq = :"#{k}="
116
+ remove_method(k) if method_defined?(k)
117
+ remove_method(k_eq) if method_defined?(k_eq)
119
118
  end
120
119
  end
120
+ end
121
121
 
122
- def add_accessors(keys)
123
- metaclass.instance_eval do
124
- keys.each do |k|
125
- next if @@immutable_values.include?(k)
126
- k_eq = :"#{k}="
127
- define_method(k) { @values[k] }
128
- define_method(k_eq) do |v|
129
- @values[k] = v
130
- @unsaved_values.add(k)
131
-
132
- cur = self
133
- cur_parent = self.parent
134
- param = {}
135
- while cur_parent
136
- if cur.name
137
- cur_parent.unsaved_values.add(cur.name)
138
- end
139
-
140
- cur = cur_parent
141
- cur_parent = cur.parent
122
+ def add_accessors(keys)
123
+ metaclass.instance_eval do
124
+ keys.each do |k|
125
+ next if @@immutable_values.include?(k)
126
+ k = k.to_s
127
+ k_eq = :"#{k}="
128
+ define_method(k) { @values[k] }
129
+ define_method(k_eq) do |v|
130
+ @values[k] = v
131
+ @unsaved_values.add(k)
132
+
133
+ cur = self
134
+ cur_parent = self.parent
135
+ while cur_parent
136
+ if cur.name
137
+ cur_parent.unsaved_values.add(cur.name)
142
138
  end
139
+
140
+ cur = cur_parent
141
+ cur_parent = cur.parent
143
142
  end
144
143
  end
145
144
  end
146
145
  end
147
-
148
146
  end
149
147
  end