easypost 3.5.1 → 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 -48
- 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 -40
- data/lib/easypost/version.rb +3 -1
- data/lib/easypost/webhook.rb +18 -12
- data/lib/easypost.rb +59 -55
- 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,51 +1,35 @@
|
|
1
|
-
|
2
|
-
attr_accessor :os_name, :os_version, :os_arch
|
3
|
-
|
4
|
-
def self.os_name
|
5
|
-
case RUBY_PLATFORM
|
6
|
-
when /linux/i
|
7
|
-
'Linux'
|
8
|
-
when /darwin/i
|
9
|
-
'Darwin'
|
10
|
-
when /cygwin|mswin|mingw|bccwin|wince|emx/i
|
11
|
-
'Windows'
|
12
|
-
else
|
13
|
-
'Unknown'
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.os_version
|
18
|
-
Gem::Platform.local.version
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.os_arch
|
22
|
-
Gem::Platform.local.cpu
|
23
|
-
end
|
1
|
+
# frozen_string_literal: true
|
24
2
|
|
3
|
+
# Internal utilities helpful for this libraries operation.
|
4
|
+
module EasyPost::Util
|
5
|
+
# Converts an object to an object ID.
|
25
6
|
def self.objects_to_ids(obj)
|
26
7
|
case obj
|
27
8
|
when EasyPost::Resource
|
28
|
-
|
9
|
+
{ id: obj.id }
|
29
10
|
when Hash
|
30
11
|
result = {}
|
31
12
|
obj.each { |k, v| result[k] = objects_to_ids(v) unless v.nil? }
|
32
|
-
|
13
|
+
result
|
33
14
|
when Array
|
34
|
-
|
15
|
+
obj.map { |v| objects_to_ids(v) }
|
35
16
|
else
|
36
|
-
|
17
|
+
obj
|
37
18
|
end
|
38
19
|
end
|
39
20
|
|
21
|
+
# Normalizes a list of strings.
|
40
22
|
def self.normalize_string_list(lst)
|
41
23
|
lst = lst.is_a?(String) ? lst.split(',') : Array(lst)
|
42
24
|
lst.map(&:to_s).map(&:downcase).map(&:strip)
|
43
25
|
end
|
44
26
|
|
45
|
-
|
27
|
+
# Convert data to an EasyPost Object.
|
28
|
+
def self.convert_to_easypost_object(response, api_key, parent = nil, name = nil)
|
46
29
|
types = {
|
47
30
|
'Address' => EasyPost::Address,
|
48
31
|
'Batch' => EasyPost::Batch,
|
32
|
+
'Brand' => EasyPost::Brand,
|
49
33
|
'CarrierAccount' => EasyPost::CarrierAccount,
|
50
34
|
'CustomsInfo' => EasyPost::CustomsInfo,
|
51
35
|
'CustomsItem' => EasyPost::CustomsItem,
|
@@ -57,8 +41,6 @@ module EasyPost::Util
|
|
57
41
|
'Pickup' => EasyPost::Pickup,
|
58
42
|
'PickupRate' => EasyPost::PickupRate,
|
59
43
|
'PostageLabel' => EasyPost::PostageLabel,
|
60
|
-
'Printer' => EasyPost::Printer,
|
61
|
-
'PrintJob' => EasyPost::PrintJob,
|
62
44
|
'Rate' => EasyPost::Rate,
|
63
45
|
'Refund' => EasyPost::Refund,
|
64
46
|
'RefundReport' => EasyPost::Report,
|
@@ -71,12 +53,13 @@ module EasyPost::Util
|
|
71
53
|
'Tracker' => EasyPost::Tracker,
|
72
54
|
'TrackerReport' => EasyPost::Report,
|
73
55
|
'User' => EasyPost::User,
|
74
|
-
'Webhook' => EasyPost::Webhook
|
56
|
+
'Webhook' => EasyPost::Webhook,
|
75
57
|
}
|
76
58
|
|
77
59
|
prefixes = {
|
78
60
|
'adr' => EasyPost::Address,
|
79
61
|
'batch' => EasyPost::Batch,
|
62
|
+
'brd' => EasyPost::Brand,
|
80
63
|
'ca' => EasyPost::CarrierAccount,
|
81
64
|
'cstinfo' => EasyPost::CustomsInfo,
|
82
65
|
'cstitem' => EasyPost::CustomsItem,
|
@@ -89,8 +72,6 @@ module EasyPost::Util
|
|
89
72
|
'pl' => EasyPost::PostageLabel,
|
90
73
|
'plrep' => EasyPost::Report,
|
91
74
|
'prcl' => EasyPost::Parcel,
|
92
|
-
'printer' => EasyPost::Printer,
|
93
|
-
'printjob' => EasyPost::PrintJob,
|
94
75
|
'rate' => EasyPost::Rate,
|
95
76
|
'refrep' => EasyPost::Report,
|
96
77
|
'rfnd' => EasyPost::Refund,
|
@@ -100,33 +81,33 @@ module EasyPost::Util
|
|
100
81
|
'shprep' => EasyPost::Report,
|
101
82
|
'trk' => EasyPost::Tracker,
|
102
83
|
'trkrep' => EasyPost::Report,
|
103
|
-
'user' => EasyPost::User
|
84
|
+
'user' => EasyPost::User,
|
104
85
|
}
|
105
86
|
|
106
87
|
case response
|
107
88
|
when Array
|
108
|
-
|
89
|
+
response.map { |i| convert_to_easypost_object(i, api_key, parent) }
|
109
90
|
when Hash
|
110
|
-
if cls_name = response[:object]
|
91
|
+
if (cls_name = response[:object])
|
111
92
|
cls = types[cls_name]
|
112
93
|
elsif response[:id]
|
113
94
|
if response[:id].index('_').nil?
|
114
95
|
cls = EasyPost::EasyPostObject
|
115
|
-
elsif cls_prefix = response[:id][0..response[:id].index('_')]
|
96
|
+
elsif (cls_prefix = response[:id][0..response[:id].index('_')])
|
116
97
|
cls = prefixes[cls_prefix[0..-2]]
|
117
98
|
end
|
118
99
|
elsif response['id']
|
119
100
|
if response['id'].index('_').nil?
|
120
101
|
cls = EasyPost::EasyPostObject
|
121
|
-
elsif cls_prefix = response['id'][0..response['id'].index('_')]
|
102
|
+
elsif (cls_prefix = response['id'][0..response['id'].index('_')])
|
122
103
|
cls = prefixes[cls_prefix[0..-2]]
|
123
104
|
end
|
124
105
|
end
|
125
106
|
|
126
107
|
cls ||= EasyPost::EasyPostObject
|
127
|
-
|
108
|
+
cls.construct_from(response, api_key, parent, name)
|
128
109
|
else
|
129
|
-
|
110
|
+
response
|
130
111
|
end
|
131
112
|
end
|
132
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
|