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,26 +1,28 @@
1
- module EasyPost
2
- class Order < Resource
1
+ class EasyPost::Order < EasyPost::Resource
3
2
 
4
- def get_rates(params={})
5
- response, api_key = EasyPost.request(:get, url + '/rates', @api_key, params)
6
- self.refresh_from(response, @api_key, true)
3
+ def get_rates(params={})
4
+ response = EasyPost.make_request(:get, url + '/rates', @api_key, params)
5
+ self.refresh_from(response, @api_key, true)
7
6
 
8
- return self
9
- end
7
+ return self
8
+ end
10
9
 
11
- def buy(params={})
12
- if params.instance_of?(EasyPost::Rate)
13
- temp = params.clone
14
- params = {}
15
- params[:carrier] = temp.carrier
16
- params[:service] = temp.service
17
- end
10
+ def buy(params={})
11
+ if params.instance_of?(EasyPost::Rate)
12
+ temp = params.clone
13
+ params = {}
14
+ params[:carrier] = temp.carrier
15
+ params[:service] = temp.service
16
+ end
18
17
 
19
- response, api_key = EasyPost.request(:post, url + '/buy', @api_key, params)
20
- self.refresh_from(response, @api_key, true)
18
+ response = EasyPost.make_request(:post, url + '/buy', @api_key, params)
19
+ self.refresh_from(response, @api_key, true)
21
20
 
22
- return self
23
- end
21
+ return self
22
+ end
24
23
 
24
+ def self.all(filters={}, api_key=nil)
25
+ raise NotImplementedError.new('Order.all not implemented.')
25
26
  end
27
+
26
28
  end
@@ -1,4 +1,2 @@
1
- module EasyPost
2
- class Parcel < Resource
3
- end
1
+ class EasyPost::Parcel < EasyPost::Resource
4
2
  end
@@ -1,25 +1,26 @@
1
- module EasyPost
2
- class Pickup < Resource
1
+ class EasyPost::Pickup < EasyPost::Resource
2
+ def buy(params={})
3
+ if params.instance_of?(EasyPost::PickupRate)
4
+ temp = params.clone
5
+ params = {}
6
+ params[:carrier] = temp.carrier
7
+ params[:service] = temp.service
8
+ end
3
9
 
4
- def buy(params={})
5
- if params.instance_of?(EasyPost::PickupRate)
6
- temp = params.clone
7
- params = {}
8
- params[:carrier] = temp.carrier
9
- params[:service] = temp.service
10
- end
10
+ response = EasyPost.make_request(:post, url + '/buy', @api_key, params)
11
+ self.refresh_from(response, @api_key, true)
11
12
 
12
- response, api_key = EasyPost.request(:post, url + '/buy', @api_key, params)
13
- self.refresh_from(response, @api_key, true)
13
+ return self
14
+ end
14
15
 
15
- return self
16
- end
16
+ def cancel(params={})
17
+ response = EasyPost.make_request(:post, url + '/cancel', @api_key, params)
18
+ self.refresh_from(response, @api_key, true)
17
19
 
18
- def cancel(params={})
19
- response, api_key = EasyPost.request(:post, url + '/cancel', @api_key, params)
20
- self.refresh_from(response, @api_key, true)
20
+ return self
21
+ end
21
22
 
22
- return self
23
- end
23
+ def self.all(filters={}, api_key=nil)
24
+ raise NotImplementedError.new('Pickup.all not implemented.')
24
25
  end
25
26
  end
@@ -1,5 +1,3 @@
1
- module EasyPost
2
- class PickupRate < Resource
1
+ class EasyPost::PickupRate < EasyPost::Resource
3
2
 
4
- end
5
3
  end
@@ -1,4 +1,2 @@
1
- module EasyPost
2
- class PostageLabel < Resource
3
- end
1
+ class EasyPost::PostageLabel < EasyPost::Resource
4
2
  end
@@ -1,6 +1,2 @@
1
- module EasyPost
2
- class PrintJob < Resource
3
- end
1
+ class EasyPost::PrintJob < EasyPost::Resource
4
2
  end
5
-
6
-
@@ -1,28 +1,24 @@
1
- module EasyPost
2
- class Printer < Resource
1
+ class EasyPost::Printer < EasyPost::Resource
3
2
 
4
- def job
5
- response, api_key = EasyPost.request(
6
- :get, url + '/jobs', @api_key
7
- )
8
- return EasyPost::Util::convert_to_easypost_object(response, api_key)
9
- end
10
-
11
- def print(params={})
12
- if params.instance_of?(EasyPost::PostageLabel)
13
- temp = params.clone
14
- params = {}
15
- params[:postage_label] = temp
16
- end
3
+ def job
4
+ response = EasyPost.make_request(
5
+ :get, url + '/jobs', @api_key
6
+ )
7
+ return EasyPost::Util::convert_to_easypost_object(response, api_key)
8
+ end
17
9
 
18
- response, api_key = EasyPost.request(
19
- :post, url + '/print_postage_label', @api_key, params
20
- )
21
- return true
22
- rescue
23
- return false
10
+ def print(params={})
11
+ if params.instance_of?(EasyPost::PostageLabel)
12
+ temp = params.clone
13
+ params = {}
14
+ params[:postage_label] = temp
24
15
  end
25
16
 
17
+ response = EasyPost.make_request(
18
+ :post, url + '/print_postage_label', @api_key, params
19
+ )
20
+ true
21
+ rescue
22
+ false
26
23
  end
27
24
  end
28
-
data/lib/easypost/rate.rb CHANGED
@@ -1,4 +1,2 @@
1
- module EasyPost
2
- class Rate < Resource
3
- end
1
+ class EasyPost::Rate < EasyPost::Resource
4
2
  end
@@ -1,4 +1,2 @@
1
- module EasyPost
2
- class Refund < Resource
3
- end
1
+ class EasyPost::Refund < EasyPost::Resource
4
2
  end
@@ -1,31 +1,29 @@
1
- module EasyPost
2
- class Report < Resource
3
- def self.create(params={}, api_key=nil)
4
- url = "#{self.url}/#{params[:type]}"
5
- wrapped_params = {}
6
- wrapped_params[class_name.to_sym] = params
1
+ class EasyPost::Report < EasyPost::Resource
2
+ def self.create(params={}, api_key=nil)
3
+ url = "#{self.url}/#{params[:type]}"
4
+ wrapped_params = {}
5
+ wrapped_params[class_name.to_sym] = params
7
6
 
8
- response, api_key = EasyPost.request(:post, url, api_key, params)
9
- return Util.convert_to_easypost_object(response, api_key)
10
- end
11
-
12
- def self.retrieve(params, api_key=nil)
13
- id = if params.is_a?(String)
14
- params
15
- else
16
- params[:id]
17
- end
7
+ response = EasyPost.make_request(:post, url, api_key, params)
8
+ return EasyPost::Util.convert_to_easypost_object(response, api_key)
9
+ end
18
10
 
19
- instance = self.new(id, api_key)
20
- instance.refresh
21
- return instance
11
+ def self.retrieve(params, api_key=nil)
12
+ id = if params.is_a?(String)
13
+ params
14
+ else
15
+ params[:id]
22
16
  end
23
17
 
24
- def self.all(filters={}, api_key=nil)
25
- url = "#{self.url}/#{filters[:type]}"
18
+ instance = self.new(id, api_key)
19
+ instance.refresh
20
+ return instance
21
+ end
26
22
 
27
- response, api_key = EasyPost.request(:get, url, api_key, filters)
28
- return EasyPost::Util::convert_to_easypost_object(response, api_key) if response
29
- end
23
+ def self.all(filters={}, api_key=nil)
24
+ url = "#{self.url}/#{filters[:type]}"
25
+
26
+ response = EasyPost.make_request(:get, url, api_key, filters)
27
+ return EasyPost::Util::convert_to_easypost_object(response, api_key) if response
30
28
  end
31
29
  end
@@ -1,77 +1,75 @@
1
- module EasyPost
2
- class Resource < EasyPostObject
3
- def self.class_name
4
- camel = self.name.split('::')[-1]
5
- snake = camel[0..0] + camel[1..-1].gsub(/([A-Z])/, '_\1')
6
- return snake.downcase
7
- end
1
+ class EasyPost::Resource < EasyPost::EasyPostObject
2
+ def self.class_name
3
+ camel = self.name.split('::')[-1]
4
+ snake = camel[0..0] + camel[1..-1].gsub(/([A-Z])/, '_\1')
5
+ return snake.downcase
6
+ end
8
7
 
9
- def self.url
10
- if self.class_name == 'resource'
11
- raise NotImplementedError.new('Resource is an abstract class. You should perform actions on its subclasses (Address, Shipment, etc.)')
12
- end
13
- if(self.class_name[-1..-1] == 's' || self.class_name[-1..-1] == 'h')
14
- return "/#{CGI.escape(self.class_name.downcase)}es"
15
- else
16
- return "/#{CGI.escape(class_name.downcase)}s"
17
- end
8
+ def self.url
9
+ if self.class_name == 'resource'
10
+ raise NotImplementedError.new('Resource is an abstract class. You should perform actions on its subclasses (Address, Shipment, etc.)')
18
11
  end
19
-
20
- def url
21
- unless self.id
22
- raise Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{self.id.inspect}")
23
- end
24
- return "#{self.class.url}/#{CGI.escape(id)}"
12
+ if(self.class_name[-1..-1] == 's' || self.class_name[-1..-1] == 'h')
13
+ return "/v2/#{CGI.escape(self.class_name.downcase)}es"
14
+ else
15
+ return "/v2/#{CGI.escape(class_name.downcase)}s"
25
16
  end
17
+ end
26
18
 
27
- def refresh
28
- response, api_key = EasyPost.request(:get, url, @api_key, @retrieve_options)
29
- refresh_from(response, api_key)
30
- return self
19
+ def url
20
+ unless self.id
21
+ raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{self.id.inspect}")
31
22
  end
23
+ return "#{self.class.url}/#{CGI.escape(id)}"
24
+ end
32
25
 
33
- def self.all(filters={}, api_key=nil)
34
- response, api_key = EasyPost.request(:get, url, api_key, filters)
35
- return Util.convert_to_easypost_object(response, api_key)
36
- end
26
+ def refresh
27
+ response = EasyPost.make_request(:get, url, @api_key, @retrieve_options)
28
+ refresh_from(response, api_key)
29
+ return self
30
+ end
37
31
 
38
- def self.retrieve(id, api_key=nil)
39
- instance = self.new(id, api_key)
40
- instance.refresh
41
- return instance
42
- end
32
+ def self.all(filters={}, api_key=nil)
33
+ response = EasyPost.make_request(:get, url, api_key, filters)
34
+ return EasyPost::Util.convert_to_easypost_object(response, api_key)
35
+ end
43
36
 
44
- def self.create(params={}, api_key=nil)
45
- wrapped_params = {}
46
- wrapped_params[self.class_name().to_sym] = params
47
- response, api_key = EasyPost.request(:post, self.url, api_key, wrapped_params)
48
- return Util.convert_to_easypost_object(response, api_key)
49
- end
37
+ def self.retrieve(id, api_key=nil)
38
+ instance = self.new(id, api_key)
39
+ instance.refresh
40
+ return instance
41
+ end
50
42
 
51
- def delete
52
- response, api_key = EasyPost.request(:delete, url, @api_key)
53
- refresh_from(response, api_key)
54
- return self
55
- end
43
+ def self.create(params={}, api_key=nil)
44
+ wrapped_params = {}
45
+ wrapped_params[self.class_name().to_sym] = params
46
+ response = EasyPost.make_request(:post, self.url, api_key, wrapped_params)
47
+ return EasyPost::Util.convert_to_easypost_object(response, api_key)
48
+ end
56
49
 
57
- def save
58
- if @unsaved_values.length > 0
59
- values = {}
60
- @unsaved_values.each { |k| values[k] = @values[k] }
50
+ def delete
51
+ response = EasyPost.make_request(:delete, url, @api_key)
52
+ refresh_from(response, api_key)
53
+ return self
54
+ end
55
+
56
+ def save
57
+ if @unsaved_values.length > 0
58
+ values = {}
59
+ @unsaved_values.each { |k| values[k] = @values[k] }
61
60
 
62
- for key in @unsaved_values
63
- value = values[key]
64
- if value.is_a?(EasyPost::EasyPostObject)
65
- values[key] = value.flatten_unsaved
66
- end
61
+ for key in @unsaved_values
62
+ value = values[key]
63
+ if value.is_a?(EasyPost::EasyPostObject)
64
+ values[key] = value.flatten_unsaved
67
65
  end
66
+ end
68
67
 
69
- wrapped_params = {self.class.class_name => values}
68
+ wrapped_params = {self.class.class_name => values}
70
69
 
71
- response, api_key = EasyPost.request(:put, url, @api_key, wrapped_params)
72
- refresh_from(response, api_key)
73
- end
74
- return self
70
+ response = EasyPost.make_request(:put, url, @api_key, wrapped_params)
71
+ refresh_from(response, api_key)
75
72
  end
73
+ return self
76
74
  end
77
75
  end
@@ -1,8 +1,6 @@
1
- module EasyPost
2
- class ScanForm < Resource
3
- def self.create(params={}, api_key=nil)
4
- response, api_key = EasyPost.request(:post, self.url, api_key, params)
5
- return Util.convert_to_easypost_object(response, api_key)
6
- end
1
+ class EasyPost::ScanForm < EasyPost::Resource
2
+ def self.create(params={}, api_key=nil)
3
+ response = EasyPost.make_request(:post, self.url, api_key, params)
4
+ return EasyPost::Util.convert_to_easypost_object(response, api_key)
7
5
  end
8
6
  end
@@ -1,124 +1,122 @@
1
- module EasyPost
2
- class Shipment < Resource
1
+ class EasyPost::Shipment < EasyPost::Resource
2
+ def get_rates(params={})
3
+ response = EasyPost.make_request(:get, url + '/rates', @api_key, params)
4
+ self.refresh_from(response, @api_key, true)
3
5
 
4
- def get_rates(params={})
5
- response, api_key = EasyPost.request(:get, url + '/rates', @api_key, params)
6
- self.refresh_from(response, @api_key, true)
6
+ return self
7
+ end
8
+
9
+ def get_smartrates
10
+ response = EasyPost.make_request(:get, url + '/smartrate', @api_key)
11
+
12
+ return response.fetch('result', [])
13
+ end
7
14
 
8
- return self
15
+ def buy(params={})
16
+ if params.instance_of?(EasyPost::Rate)
17
+ temp = params.clone
18
+ params = {}
19
+ params[:rate] = temp
9
20
  end
10
21
 
11
- def buy(params={})
12
- if params.instance_of?(EasyPost::Rate)
13
- temp = params.clone
14
- params = {}
15
- params[:rate] = temp
16
- end
22
+ response = EasyPost.make_request(:post, url + '/buy', @api_key, params)
23
+ self.refresh_from(response, @api_key, true)
17
24
 
18
- response, api_key = EasyPost.request(:post, url + '/buy', @api_key, params)
19
- self.refresh_from(response, @api_key, true)
25
+ return self
26
+ end
20
27
 
21
- return self
28
+ def insure(params={})
29
+ if params.is_a?(Integer) || params.is_a?(Float)
30
+ temp = params.clone
31
+ params = {}
32
+ params[:amount] = temp
22
33
  end
23
34
 
24
- def insure(params={})
25
- if params.is_a?(Integer) || params.is_a?(Float)
26
- temp = params.clone
27
- params = {}
28
- params[:amount] = temp
29
- end
35
+ response = EasyPost.make_request(:post, url + '/insure', @api_key, params)
36
+ self.refresh_from(response, @api_key, true)
30
37
 
31
- response, api_key = EasyPost.request(:post, url + '/insure', @api_key, params)
32
- self.refresh_from(response, @api_key, true)
38
+ return self
39
+ end
33
40
 
34
- return self
35
- end
41
+ def refund(params={})
42
+ response = EasyPost.make_request(:get, url + '/refund', @api_key, params)
43
+ self.refresh_from(response, @api_key, true)
36
44
 
37
- def refund(params={})
38
- response, api_key = EasyPost.request(:get, url + '/refund', @api_key, params)
39
- self.refresh_from(response, @api_key, true)
45
+ return self
46
+ end
40
47
 
41
- return self
48
+ def print(params={})
49
+ if params.instance_of?(EasyPost::Printer)
50
+ return params.print(self.postage_label)
42
51
  end
52
+ return false
53
+ end
43
54
 
44
- def print(params={})
45
- if params.instance_of?(EasyPost::Printer)
46
- return params.print(self.postage_label)
47
- end
48
- return false
55
+ def label(params={})
56
+ if params.is_a?(String)
57
+ temp = params.clone
58
+ params = {}
59
+ params[:file_format] = temp
49
60
  end
50
61
 
51
- def label(params={})
52
- if params.is_a?(String)
53
- temp = params.clone
54
- params = {}
55
- params[:file_format] = temp
56
- end
62
+ response = EasyPost.make_request(:get, url + '/label', @api_key, params)
63
+ self.refresh_from(response, @api_key, true)
57
64
 
58
- response, api_key = EasyPost.request(:get, url + '/label', @api_key, params)
59
- self.refresh_from(response, @api_key, true)
60
-
61
- return self
62
- end
65
+ return self
66
+ end
63
67
 
64
- def lowest_rate(carriers=[], services=[])
65
- lowest = nil
68
+ def lowest_rate(carriers=[], services=[])
69
+ lowest = nil
66
70
 
67
- self.get_rates unless self.rates
71
+ self.get_rates unless self.rates
68
72
 
69
- carriers = carriers.is_a?(String) ? carriers.split(',') : Array(carriers)
70
- carriers.map!(&:downcase)
71
- carriers.map!(&:strip)
73
+ carriers = EasyPost::Util.normalize_string_list(carriers)
72
74
 
73
- negative_carriers = []
74
- carriers_copy = carriers.clone
75
- carriers_copy.each do |carrier|
76
- if carrier[0,1] == '!'
77
- negative_carriers << carrier[1..-1]
78
- carriers.delete(carrier)
79
- end
75
+ negative_carriers = []
76
+ carriers_copy = carriers.clone
77
+ carriers_copy.each do |carrier|
78
+ if carrier[0,1] == '!'
79
+ negative_carriers << carrier[1..-1]
80
+ carriers.delete(carrier)
80
81
  end
82
+ end
83
+
84
+ services = EasyPost::Util.normalize_string_list(services)
81
85
 
82
- services = services.is_a?(String) ? services.split(',') : Array(services)
83
- services.map!(&:downcase)
84
- services.map!(&:strip)
85
-
86
- negative_services = []
87
- services_copy = services.clone
88
- services_copy.each do |service|
89
- if service[0,1] == '!'
90
- negative_services << service[1..-1]
91
- services.delete(service)
92
- end
86
+ negative_services = []
87
+ services_copy = services.clone
88
+ services_copy.each do |service|
89
+ if service[0,1] == '!'
90
+ negative_services << service[1..-1]
91
+ services.delete(service)
93
92
  end
93
+ end
94
94
 
95
- self.rates.each do |k|
96
-
97
- rate_carrier = k.carrier.downcase
98
- if carriers.size() > 0 && !carriers.include?(rate_carrier)
99
- next
100
- end
101
- if negative_carriers.size() > 0 && negative_carriers.include?(rate_carrier)
102
- next
103
- end
104
-
105
- rate_service = k.service.downcase
106
- if services.size() > 0 && !services.include?(rate_service)
107
- next
108
- end
109
- if negative_services.size() > 0 && negative_services.include?(rate_service)
110
- next
111
- end
112
-
113
- if lowest == nil || k.rate.to_f < lowest.rate.to_f
114
- lowest = k
115
- end
95
+ self.rates.each do |k|
96
+
97
+ rate_carrier = k.carrier.downcase
98
+ if carriers.size() > 0 && !carriers.include?(rate_carrier)
99
+ next
100
+ end
101
+ if negative_carriers.size() > 0 && negative_carriers.include?(rate_carrier)
102
+ next
116
103
  end
117
104
 
118
- raise Error.new('No rates found.') if lowest == nil
105
+ rate_service = k.service.downcase
106
+ if services.size() > 0 && !services.include?(rate_service)
107
+ next
108
+ end
109
+ if negative_services.size() > 0 && negative_services.include?(rate_service)
110
+ next
111
+ end
119
112
 
120
- return lowest
113
+ if lowest == nil || k.rate.to_f < lowest.rate.to_f
114
+ lowest = k
115
+ end
121
116
  end
122
117
 
118
+ raise EasyPost::Error.new('No rates found.') if lowest == nil
119
+
120
+ return lowest
123
121
  end
124
122
  end