easypost 3.5.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +19 -5
- data/.gitignore +1 -0
- data/.rubocop.yml +5 -0
- data/CHANGELOG.md +88 -88
- data/Gemfile +2 -0
- data/README.md +2 -10
- data/Rakefile +2 -1
- data/UPGRADE_GUIDE.md +62 -0
- data/VERSION +1 -1
- data/bin/easypost-irb +5 -3
- data/easycop.yml +180 -0
- data/easypost.gemspec +21 -19
- data/lib/easypost/address.rb +26 -26
- data/lib/easypost/api_key.rb +3 -0
- data/lib/easypost/batch.rb +31 -30
- data/lib/easypost/brand.rb +4 -0
- data/lib/easypost/carrier_account.rb +4 -0
- data/lib/easypost/carrier_type.rb +3 -0
- data/lib/easypost/customs_info.rb +5 -1
- data/lib/easypost/customs_item.rb +5 -1
- data/lib/easypost/error.rb +7 -7
- data/lib/easypost/event.rb +5 -1
- data/lib/easypost/insurance.rb +4 -0
- data/lib/easypost/object.rb +44 -28
- data/lib/easypost/order.rb +15 -11
- data/lib/easypost/parcel.rb +7 -0
- data/lib/easypost/pickup.rb +15 -9
- data/lib/easypost/pickup_rate.rb +3 -1
- data/lib/easypost/postage_label.rb +3 -0
- data/lib/easypost/rate.rb +7 -0
- data/lib/easypost/refund.rb +3 -0
- data/lib/easypost/report.rb +9 -16
- data/lib/easypost/resource.rb +41 -25
- data/lib/easypost/scan_form.rb +8 -3
- data/lib/easypost/shipment.rb +47 -51
- data/lib/easypost/tax_identifier.rb +4 -0
- data/lib/easypost/tracker.rb +9 -4
- data/lib/easypost/user.rb +14 -5
- data/lib/easypost/util.rb +21 -17
- data/lib/easypost/version.rb +3 -1
- data/lib/easypost/webhook.rb +18 -12
- data/lib/easypost.rb +59 -51
- metadata +74 -19
- data/lib/easypost/print_job.rb +0 -2
- data/lib/easypost/printer.rb +0 -24
data/lib/easypost/report.rb
CHANGED
@@ -1,29 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# A Report contains a csv that is a log of all the objects created within a certain time frame.
|
1
4
|
class EasyPost::Report < EasyPost::Resource
|
2
|
-
|
5
|
+
# Create a Report.
|
6
|
+
def self.create(params = {}, api_key = nil)
|
3
7
|
url = "#{self.url}/#{params[:type]}"
|
4
8
|
wrapped_params = {}
|
5
9
|
wrapped_params[class_name.to_sym] = params
|
6
10
|
|
7
11
|
response = EasyPost.make_request(:post, url, api_key, params)
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.retrieve(params, api_key=nil)
|
12
|
-
id = if params.is_a?(String)
|
13
|
-
params
|
14
|
-
else
|
15
|
-
params[:id]
|
16
|
-
end
|
17
|
-
|
18
|
-
instance = self.new(id, api_key)
|
19
|
-
instance.refresh
|
20
|
-
return instance
|
12
|
+
EasyPost::Util.convert_to_easypost_object(response, api_key)
|
21
13
|
end
|
22
14
|
|
23
|
-
|
15
|
+
# Retrieve a list of Report objects.
|
16
|
+
def self.all(filters = {}, api_key = nil)
|
24
17
|
url = "#{self.url}/#{filters[:type]}"
|
25
18
|
|
26
19
|
response = EasyPost.make_request(:get, url, api_key, filters)
|
27
|
-
|
20
|
+
EasyPost::Util.convert_to_easypost_object(response, api_key)
|
28
21
|
end
|
29
22
|
end
|
data/lib/easypost/resource.rb
CHANGED
@@ -1,75 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The Resource object is extended by each EasyPost object.
|
1
4
|
class EasyPost::Resource < EasyPost::EasyPostObject
|
5
|
+
# The class name of an EasyPost object.
|
2
6
|
def self.class_name
|
3
|
-
camel =
|
7
|
+
camel = name.split('::')[-1]
|
4
8
|
snake = camel[0..0] + camel[1..-1].gsub(/([A-Z])/, '_\1')
|
5
|
-
|
9
|
+
snake.downcase
|
6
10
|
end
|
7
11
|
|
12
|
+
# The instance url of the Resource.
|
8
13
|
def self.url
|
9
|
-
if
|
10
|
-
raise NotImplementedError.new(
|
14
|
+
if class_name == 'resource'
|
15
|
+
raise NotImplementedError.new(
|
16
|
+
'Resource is an abstract class. You should perform actions on its subclasses (Address, Shipment, etc.)',
|
17
|
+
)
|
11
18
|
end
|
12
|
-
|
13
|
-
|
19
|
+
|
20
|
+
if class_name[-1..-1] == 's' || class_name[-1..-1] == 'h'
|
21
|
+
"/v2/#{CGI.escape(class_name.downcase)}es"
|
14
22
|
else
|
15
|
-
|
23
|
+
"/v2/#{CGI.escape(class_name.downcase)}s"
|
16
24
|
end
|
17
25
|
end
|
18
26
|
|
27
|
+
# The url of the Resource.
|
19
28
|
def url
|
20
|
-
unless
|
21
|
-
raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{
|
29
|
+
unless id
|
30
|
+
raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}")
|
22
31
|
end
|
23
|
-
|
32
|
+
|
33
|
+
"#{self.class.url}/#{CGI.escape(id)}"
|
24
34
|
end
|
25
35
|
|
36
|
+
# Refresh an object from the API response.
|
26
37
|
def refresh
|
27
38
|
response = EasyPost.make_request(:get, url, @api_key, @retrieve_options)
|
28
39
|
refresh_from(response, api_key)
|
29
|
-
|
40
|
+
self
|
30
41
|
end
|
31
42
|
|
32
|
-
|
43
|
+
# Retrieve a list of EasyPost objects.
|
44
|
+
def self.all(filters = {}, api_key = nil)
|
33
45
|
response = EasyPost.make_request(:get, url, api_key, filters)
|
34
|
-
|
46
|
+
EasyPost::Util.convert_to_easypost_object(response, api_key)
|
35
47
|
end
|
36
48
|
|
37
|
-
|
38
|
-
|
49
|
+
# Retrieve an EasyPost object.
|
50
|
+
def self.retrieve(id, api_key = nil)
|
51
|
+
instance = new(id, api_key)
|
39
52
|
instance.refresh
|
40
|
-
|
53
|
+
instance
|
41
54
|
end
|
42
55
|
|
43
|
-
|
56
|
+
# Create an EasyPost object.
|
57
|
+
def self.create(params = {}, api_key = nil)
|
44
58
|
wrapped_params = {}
|
45
|
-
wrapped_params[
|
46
|
-
response = EasyPost.make_request(:post,
|
47
|
-
|
59
|
+
wrapped_params[class_name.to_sym] = params
|
60
|
+
response = EasyPost.make_request(:post, url, api_key, wrapped_params)
|
61
|
+
EasyPost::Util.convert_to_easypost_object(response, api_key)
|
48
62
|
end
|
49
63
|
|
64
|
+
# Delete an EasyPost object.
|
50
65
|
def delete
|
51
66
|
response = EasyPost.make_request(:delete, url, @api_key)
|
52
67
|
refresh_from(response, api_key)
|
53
|
-
|
68
|
+
self
|
54
69
|
end
|
55
70
|
|
71
|
+
# Save (update) and EasyPost object.
|
56
72
|
def save
|
57
|
-
if @unsaved_values.length
|
73
|
+
if @unsaved_values.length.positive?
|
58
74
|
values = {}
|
59
75
|
@unsaved_values.each { |k| values[k] = @values[k] }
|
60
76
|
|
61
|
-
|
77
|
+
@unsaved_values.each do |key| # rubocop:disable Style/CombinableLoops
|
62
78
|
value = values[key]
|
63
79
|
if value.is_a?(EasyPost::EasyPostObject)
|
64
80
|
values[key] = value.flatten_unsaved
|
65
81
|
end
|
66
82
|
end
|
67
83
|
|
68
|
-
wrapped_params = {self.class.class_name => values}
|
84
|
+
wrapped_params = { self.class.class_name => values }
|
69
85
|
|
70
86
|
response = EasyPost.make_request(:put, url, @api_key, wrapped_params)
|
71
87
|
refresh_from(response, api_key)
|
72
88
|
end
|
73
|
-
|
89
|
+
self
|
74
90
|
end
|
75
91
|
end
|
data/lib/easypost/scan_form.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# A ScanForm can be created to speed up and simplify the carrier pickup process. The ScanForm is one document that can
|
4
|
+
# be scanned to mark all included tracking codes as "Accepted for Shipment" by the carrier.
|
1
5
|
class EasyPost::ScanForm < EasyPost::Resource
|
2
|
-
|
3
|
-
|
4
|
-
|
6
|
+
# Create a ScanForm.
|
7
|
+
def self.create(params = {}, api_key = nil)
|
8
|
+
response = EasyPost.make_request(:post, url, api_key, params)
|
9
|
+
EasyPost::Util.convert_to_easypost_object(response, api_key)
|
5
10
|
end
|
6
11
|
end
|
data/lib/easypost/shipment.rb
CHANGED
@@ -1,88 +1,85 @@
|
|
1
|
-
|
2
|
-
def get_rates(params={})
|
3
|
-
response = EasyPost.make_request(:get, url + '/rates', @api_key, params)
|
4
|
-
self.refresh_from(response, @api_key, true)
|
5
|
-
|
6
|
-
return self
|
7
|
-
end
|
1
|
+
# frozen_string_literal: true
|
8
2
|
|
9
|
-
|
10
|
-
|
11
|
-
|
3
|
+
# The workhorse of the EasyPost API, a Shipment is made up of a "to" and "from" Address, the Parcel
|
4
|
+
# being shipped, and any customs forms required for international deliveries.
|
5
|
+
class EasyPost::Shipment < EasyPost::Resource
|
6
|
+
# Regenerate the rates of a Shipment.
|
7
|
+
def regenerate_rates(params = {})
|
8
|
+
response = EasyPost.make_request(:post, "#{url}/rerate", @api_key, params)
|
9
|
+
refresh_from(response, @api_key)
|
12
10
|
|
13
|
-
|
11
|
+
self
|
14
12
|
end
|
15
13
|
|
16
|
-
|
17
|
-
|
14
|
+
# Get the SmartRates of a Shipment.
|
15
|
+
def get_smartrates # rubocop:disable Naming/AccessorMethodName
|
16
|
+
response = EasyPost.make_request(:get, "#{url}/smartrate", @api_key)
|
18
17
|
|
19
|
-
|
18
|
+
response.fetch('result', [])
|
20
19
|
end
|
21
20
|
|
22
|
-
|
21
|
+
# Buy a Shipment.
|
22
|
+
def buy(params = {})
|
23
23
|
if params.instance_of?(EasyPost::Rate)
|
24
24
|
temp = params.clone
|
25
25
|
params = {}
|
26
26
|
params[:rate] = temp
|
27
27
|
end
|
28
28
|
|
29
|
-
response = EasyPost.make_request(:post, url
|
30
|
-
|
29
|
+
response = EasyPost.make_request(:post, "#{url}/buy", @api_key, params)
|
30
|
+
refresh_from(response, @api_key)
|
31
31
|
|
32
|
-
|
32
|
+
self
|
33
33
|
end
|
34
34
|
|
35
|
-
|
35
|
+
# Insure a Shipment.
|
36
|
+
def insure(params = {})
|
36
37
|
if params.is_a?(Integer) || params.is_a?(Float)
|
37
38
|
temp = params.clone
|
38
39
|
params = {}
|
39
40
|
params[:amount] = temp
|
40
41
|
end
|
41
42
|
|
42
|
-
response = EasyPost.make_request(:post, url
|
43
|
-
|
43
|
+
response = EasyPost.make_request(:post, "#{url}/insure", @api_key, params)
|
44
|
+
refresh_from(response, @api_key)
|
44
45
|
|
45
|
-
|
46
|
+
self
|
46
47
|
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
49
|
+
# Refund a Shipment.
|
50
|
+
def refund(params = {})
|
51
|
+
response = EasyPost.make_request(:get, "#{url}/refund", @api_key, params)
|
52
|
+
refresh_from(response, @api_key)
|
51
53
|
|
52
|
-
|
54
|
+
self
|
53
55
|
end
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
return params.print(self.postage_label)
|
58
|
-
end
|
59
|
-
return false
|
60
|
-
end
|
61
|
-
|
62
|
-
def label(params={})
|
57
|
+
# Convert the label format of a Shipment.
|
58
|
+
def label(params = {})
|
63
59
|
if params.is_a?(String)
|
64
60
|
temp = params.clone
|
65
61
|
params = {}
|
66
62
|
params[:file_format] = temp
|
67
63
|
end
|
68
64
|
|
69
|
-
response = EasyPost.make_request(:get, url
|
70
|
-
|
65
|
+
response = EasyPost.make_request(:get, "#{url}/label", @api_key, params)
|
66
|
+
refresh_from(response, @api_key)
|
71
67
|
|
72
|
-
|
68
|
+
self
|
73
69
|
end
|
74
70
|
|
75
|
-
|
71
|
+
# Get the lowest rate of a Shipment.
|
72
|
+
def lowest_rate(carriers = [], services = [])
|
76
73
|
lowest = nil
|
77
74
|
|
78
|
-
|
75
|
+
get_rates unless rates
|
79
76
|
|
80
77
|
carriers = EasyPost::Util.normalize_string_list(carriers)
|
81
78
|
|
82
79
|
negative_carriers = []
|
83
80
|
carriers_copy = carriers.clone
|
84
81
|
carriers_copy.each do |carrier|
|
85
|
-
if carrier[0,1] == '!'
|
82
|
+
if carrier[0, 1] == '!'
|
86
83
|
negative_carriers << carrier[1..-1]
|
87
84
|
carriers.delete(carrier)
|
88
85
|
end
|
@@ -93,37 +90,36 @@ class EasyPost::Shipment < EasyPost::Resource
|
|
93
90
|
negative_services = []
|
94
91
|
services_copy = services.clone
|
95
92
|
services_copy.each do |service|
|
96
|
-
if service[0,1] == '!'
|
93
|
+
if service[0, 1] == '!'
|
97
94
|
negative_services << service[1..-1]
|
98
95
|
services.delete(service)
|
99
96
|
end
|
100
97
|
end
|
101
98
|
|
102
|
-
|
103
|
-
|
99
|
+
rates.each do |k|
|
104
100
|
rate_carrier = k.carrier.downcase
|
105
|
-
if carriers.size
|
101
|
+
if carriers.size.positive? && !carriers.include?(rate_carrier)
|
106
102
|
next
|
107
103
|
end
|
108
|
-
if negative_carriers.size
|
104
|
+
if negative_carriers.size.positive? && negative_carriers.include?(rate_carrier)
|
109
105
|
next
|
110
106
|
end
|
111
107
|
|
112
108
|
rate_service = k.service.downcase
|
113
|
-
if services.size
|
109
|
+
if services.size.positive? && !services.include?(rate_service)
|
114
110
|
next
|
115
111
|
end
|
116
|
-
if negative_services.size
|
112
|
+
if negative_services.size.positive? && negative_services.include?(rate_service)
|
117
113
|
next
|
118
114
|
end
|
119
115
|
|
120
|
-
if lowest
|
121
|
-
|
116
|
+
if lowest.nil? || k.rate.to_f < lowest.rate.to_f
|
117
|
+
lowest = k
|
122
118
|
end
|
123
119
|
end
|
124
120
|
|
125
|
-
raise EasyPost::Error.new('No rates found.') if lowest
|
121
|
+
raise EasyPost::Error.new('No rates found.') if lowest.nil?
|
126
122
|
|
127
|
-
|
123
|
+
lowest
|
128
124
|
end
|
129
125
|
end
|
data/lib/easypost/tracker.rb
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# A Tracker object contains all of the tracking information for a package.
|
1
4
|
class EasyPost::Tracker < EasyPost::Resource
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
5
|
+
# Create multiple Tracker objects in bulk.
|
6
|
+
def self.create_list(params = {}, api_key = nil)
|
7
|
+
url = "#{self.url}/create_list"
|
8
|
+
new_params = { 'trackers' => params }
|
9
|
+
EasyPost.make_request(:post, url, api_key, new_params)
|
10
|
+
true # This endpoint does not return a response so we return true here instead
|
6
11
|
end
|
7
12
|
end
|
data/lib/easypost/user.rb
CHANGED
@@ -1,15 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The User object can be used to manage your own account and to create child accounts.
|
1
4
|
class EasyPost::User < EasyPost::Resource
|
5
|
+
# Create a child User.
|
2
6
|
def self.create(params = {}, api_key = nil)
|
3
|
-
response = EasyPost.make_request(:post, url, api_key, {class_name.to_sym => params})
|
7
|
+
response = EasyPost.make_request(:post, url, api_key, { class_name.to_sym => params })
|
4
8
|
EasyPost::Util.convert_to_easypost_object(response, api_key)
|
5
9
|
end
|
6
10
|
|
11
|
+
# Save (update) a User.
|
7
12
|
def save
|
8
|
-
if @unsaved_values.length
|
13
|
+
if @unsaved_values.length.positive?
|
9
14
|
values = {}
|
10
15
|
@unsaved_values.each { |k| values[k] = @values[k] }
|
11
16
|
|
12
|
-
wrapped_params = {user: values}
|
17
|
+
wrapped_params = { user: values }
|
13
18
|
|
14
19
|
response = EasyPost.make_request(:put, url, @api_key, wrapped_params)
|
15
20
|
refresh_from(response, api_key)
|
@@ -17,14 +22,17 @@ class EasyPost::User < EasyPost::Resource
|
|
17
22
|
self
|
18
23
|
end
|
19
24
|
|
25
|
+
# Retrieve the authenticated User.
|
20
26
|
def self.retrieve_me
|
21
27
|
all
|
22
28
|
end
|
23
29
|
|
30
|
+
# Retrieve a list of ApiKey objects.
|
24
31
|
def self.all_api_keys
|
25
32
|
EasyPost::ApiKey.all
|
26
33
|
end
|
27
34
|
|
35
|
+
# Retrieve a list of ApiKey objects of a child User.
|
28
36
|
def api_keys
|
29
37
|
api_keys = EasyPost::User.all_api_keys
|
30
38
|
|
@@ -41,10 +49,11 @@ class EasyPost::User < EasyPost::Resource
|
|
41
49
|
|
42
50
|
my_api_keys
|
43
51
|
end
|
44
|
-
|
52
|
+
|
53
|
+
# Update the Brand of a User.
|
45
54
|
def update_brand(**attrs)
|
46
55
|
brand = EasyPost::Brand.new
|
47
|
-
data = {object:
|
56
|
+
data = { object: 'Brand', user_id: id, **attrs }
|
48
57
|
# Add accessors manually because there's no API to retrieve a brand
|
49
58
|
brand.add_accessors(data.keys)
|
50
59
|
# Assigning values with accessors defined above
|
data/lib/easypost/util.rb
CHANGED
@@ -1,28 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Internal utilities helpful for this libraries operation.
|
1
4
|
module EasyPost::Util
|
5
|
+
# Converts an object to an object ID.
|
2
6
|
def self.objects_to_ids(obj)
|
3
7
|
case obj
|
4
8
|
when EasyPost::Resource
|
5
|
-
|
9
|
+
{ id: obj.id }
|
6
10
|
when Hash
|
7
11
|
result = {}
|
8
12
|
obj.each { |k, v| result[k] = objects_to_ids(v) unless v.nil? }
|
9
|
-
|
13
|
+
result
|
10
14
|
when Array
|
11
|
-
|
15
|
+
obj.map { |v| objects_to_ids(v) }
|
12
16
|
else
|
13
|
-
|
17
|
+
obj
|
14
18
|
end
|
15
19
|
end
|
16
20
|
|
21
|
+
# Normalizes a list of strings.
|
17
22
|
def self.normalize_string_list(lst)
|
18
23
|
lst = lst.is_a?(String) ? lst.split(',') : Array(lst)
|
19
24
|
lst.map(&:to_s).map(&:downcase).map(&:strip)
|
20
25
|
end
|
21
26
|
|
22
|
-
|
27
|
+
# Convert data to an EasyPost Object.
|
28
|
+
def self.convert_to_easypost_object(response, api_key, parent = nil, name = nil)
|
23
29
|
types = {
|
24
30
|
'Address' => EasyPost::Address,
|
25
31
|
'Batch' => EasyPost::Batch,
|
32
|
+
'Brand' => EasyPost::Brand,
|
26
33
|
'CarrierAccount' => EasyPost::CarrierAccount,
|
27
34
|
'CustomsInfo' => EasyPost::CustomsInfo,
|
28
35
|
'CustomsItem' => EasyPost::CustomsItem,
|
@@ -34,8 +41,6 @@ module EasyPost::Util
|
|
34
41
|
'Pickup' => EasyPost::Pickup,
|
35
42
|
'PickupRate' => EasyPost::PickupRate,
|
36
43
|
'PostageLabel' => EasyPost::PostageLabel,
|
37
|
-
'Printer' => EasyPost::Printer,
|
38
|
-
'PrintJob' => EasyPost::PrintJob,
|
39
44
|
'Rate' => EasyPost::Rate,
|
40
45
|
'Refund' => EasyPost::Refund,
|
41
46
|
'RefundReport' => EasyPost::Report,
|
@@ -48,12 +53,13 @@ module EasyPost::Util
|
|
48
53
|
'Tracker' => EasyPost::Tracker,
|
49
54
|
'TrackerReport' => EasyPost::Report,
|
50
55
|
'User' => EasyPost::User,
|
51
|
-
'Webhook' => EasyPost::Webhook
|
56
|
+
'Webhook' => EasyPost::Webhook,
|
52
57
|
}
|
53
58
|
|
54
59
|
prefixes = {
|
55
60
|
'adr' => EasyPost::Address,
|
56
61
|
'batch' => EasyPost::Batch,
|
62
|
+
'brd' => EasyPost::Brand,
|
57
63
|
'ca' => EasyPost::CarrierAccount,
|
58
64
|
'cstinfo' => EasyPost::CustomsInfo,
|
59
65
|
'cstitem' => EasyPost::CustomsItem,
|
@@ -66,8 +72,6 @@ module EasyPost::Util
|
|
66
72
|
'pl' => EasyPost::PostageLabel,
|
67
73
|
'plrep' => EasyPost::Report,
|
68
74
|
'prcl' => EasyPost::Parcel,
|
69
|
-
'printer' => EasyPost::Printer,
|
70
|
-
'printjob' => EasyPost::PrintJob,
|
71
75
|
'rate' => EasyPost::Rate,
|
72
76
|
'refrep' => EasyPost::Report,
|
73
77
|
'rfnd' => EasyPost::Refund,
|
@@ -77,33 +81,33 @@ module EasyPost::Util
|
|
77
81
|
'shprep' => EasyPost::Report,
|
78
82
|
'trk' => EasyPost::Tracker,
|
79
83
|
'trkrep' => EasyPost::Report,
|
80
|
-
'user' => EasyPost::User
|
84
|
+
'user' => EasyPost::User,
|
81
85
|
}
|
82
86
|
|
83
87
|
case response
|
84
88
|
when Array
|
85
|
-
|
89
|
+
response.map { |i| convert_to_easypost_object(i, api_key, parent) }
|
86
90
|
when Hash
|
87
|
-
if cls_name = response[:object]
|
91
|
+
if (cls_name = response[:object])
|
88
92
|
cls = types[cls_name]
|
89
93
|
elsif response[:id]
|
90
94
|
if response[:id].index('_').nil?
|
91
95
|
cls = EasyPost::EasyPostObject
|
92
|
-
elsif cls_prefix = response[:id][0..response[:id].index('_')]
|
96
|
+
elsif (cls_prefix = response[:id][0..response[:id].index('_')])
|
93
97
|
cls = prefixes[cls_prefix[0..-2]]
|
94
98
|
end
|
95
99
|
elsif response['id']
|
96
100
|
if response['id'].index('_').nil?
|
97
101
|
cls = EasyPost::EasyPostObject
|
98
|
-
elsif cls_prefix = response['id'][0..response['id'].index('_')]
|
102
|
+
elsif (cls_prefix = response['id'][0..response['id'].index('_')])
|
99
103
|
cls = prefixes[cls_prefix[0..-2]]
|
100
104
|
end
|
101
105
|
end
|
102
106
|
|
103
107
|
cls ||= EasyPost::EasyPostObject
|
104
|
-
|
108
|
+
cls.construct_from(response, api_key, parent, name)
|
105
109
|
else
|
106
|
-
|
110
|
+
response
|
107
111
|
end
|
108
112
|
end
|
109
113
|
end
|
data/lib/easypost/version.rb
CHANGED
data/lib/easypost/webhook.rb
CHANGED
@@ -1,29 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Each Webhook contains the url which EasyPost will notify whenever an object in our system updates. Several types of objects are processed
|
4
|
+
# asynchronously in the EasyPost system, so whenever an object updates, an Event is sent via HTTP POST to each configured webhook URL.
|
1
5
|
class EasyPost::Webhook < EasyPost::Resource
|
2
|
-
|
3
|
-
|
4
|
-
# with the objects field
|
5
|
-
unless
|
6
|
-
raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{
|
6
|
+
# Update a Webhook.
|
7
|
+
def update(params = {})
|
8
|
+
# NOTE: This method is redefined here since the "url" method conflicts with the objects field
|
9
|
+
unless id
|
10
|
+
raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}")
|
7
11
|
end
|
12
|
+
|
8
13
|
instance_url = "#{self.class.url}/#{CGI.escape(id)}"
|
9
14
|
|
10
15
|
response = EasyPost.make_request(:put, instance_url, @api_key, params)
|
11
|
-
|
16
|
+
refresh_from(response, api_key, true)
|
12
17
|
|
13
|
-
|
18
|
+
self
|
14
19
|
end
|
15
20
|
|
21
|
+
# Delete a Webhook.
|
16
22
|
def delete
|
17
|
-
# NOTE: This method is redefined here since the "url" method conflicts
|
18
|
-
|
19
|
-
|
20
|
-
raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{self.id.inspect}")
|
23
|
+
# NOTE: This method is redefined here since the "url" method conflicts with the objects field
|
24
|
+
unless id
|
25
|
+
raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}")
|
21
26
|
end
|
27
|
+
|
22
28
|
instance_url = "#{self.class.url}/#{CGI.escape(id)}"
|
23
29
|
|
24
30
|
response = EasyPost.make_request(:delete, instance_url, @api_key)
|
25
31
|
refresh_from(response, api_key)
|
26
32
|
|
27
|
-
|
33
|
+
self
|
28
34
|
end
|
29
35
|
end
|