sendle-api 0.0.2 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +5 -0
  3. data/README.md +99 -5
  4. data/lib/sendle/api.rb +46 -3
  5. data/lib/sendle/api/actions/base.rb +51 -0
  6. data/lib/sendle/api/actions/create.rb +31 -0
  7. data/lib/sendle/api/actions/destroy.rb +23 -0
  8. data/lib/sendle/api/actions/index.rb +16 -14
  9. data/lib/sendle/api/actions/show.rb +23 -0
  10. data/lib/sendle/api/constants.rb +4 -0
  11. data/lib/sendle/api/errors/invalid_plan.rb +16 -0
  12. data/lib/sendle/api/errors/missing_params.rb +16 -0
  13. data/lib/sendle/api/errors/payment_required.rb +9 -0
  14. data/lib/sendle/api/errors/precondition_failed.rb +9 -0
  15. data/lib/sendle/api/errors/unprocessable_entity.rb +19 -0
  16. data/lib/sendle/api/factories/errors.rb +26 -0
  17. data/lib/sendle/api/order.rb +41 -0
  18. data/lib/sendle/api/ping.rb +8 -3
  19. data/lib/sendle/api/quote.rb +28 -0
  20. data/lib/sendle/api/resource.rb +64 -0
  21. data/lib/sendle/api/responses/json.rb +15 -0
  22. data/lib/sendle/api/sugars/create.rb +13 -0
  23. data/lib/sendle/api/sugars/destroy.rb +13 -0
  24. data/lib/sendle/api/sugars/index.rb +13 -0
  25. data/lib/sendle/api/sugars/show.rb +13 -0
  26. data/lib/sendle/api/utils.rb +24 -0
  27. data/lib/sendle/api/version.rb +1 -1
  28. data/spec/sendle/api/order_spec.rb +249 -0
  29. data/spec/sendle/api/ping_spec.rb +9 -1
  30. data/spec/sendle/api/quote_spec.rb +103 -0
  31. data/spec/sendle/api_spec.rb +22 -0
  32. data/spec/sendle/errors/missing_params_spec.rb +13 -0
  33. data/spec/support/helpers.rb +15 -1
  34. data/spec/support/shared_examples/with_credentials_spec.rb +1 -1
  35. metadata +29 -3
  36. data/lib/sendle/utils/actions.rb +0 -19
@@ -33,13 +33,21 @@ describe Sendle::Api::Ping do
33
33
  expect(pong).to be_a Sendle::Api::Responses::Pong
34
34
  end
35
35
 
36
- it "handles unauthorized" do
36
+ it "handles unauthorized error" do
37
37
  expect(RestClient::Request).to receive(:execute).and_raise(UNAUTHORIZED_ERROR)
38
38
 
39
39
  expect {
40
40
  Sendle::Api::Ping.execute
41
41
  }.to raise_error(Sendle::Api::Errors::Unauthorized)
42
42
  end
43
+
44
+ it "handles payment required error" do
45
+ expect(RestClient::Request).to receive(:execute).and_raise(PAYMENT_REQUIRED_ERROR)
46
+
47
+ expect {
48
+ Sendle::Api::Ping.execute
49
+ }.to raise_error(Sendle::Api::Errors::PaymentRequired)
50
+ end
43
51
  end
44
52
 
45
53
  end
@@ -0,0 +1,103 @@
1
+ require "spec_helper"
2
+
3
+ describe Sendle::Api::Quote do
4
+
5
+ describe "#execute" do
6
+ let(:params) {
7
+ {
8
+ pickup_suburb: 'woolloomooloo',
9
+ pickup_postcode: '2011',
10
+ delivery_suburb: 'eastgardens',
11
+ delivery_postcode: '2036',
12
+ kilogram_weight: '1'
13
+ }
14
+ }
15
+
16
+ it "makes the correct request" do
17
+ expected_params = {
18
+ method: :get,
19
+ url: Sendle::Api::Quote.url,
20
+ headers: {
21
+ accept: :json,
22
+ content_type: :json,
23
+ params: params
24
+ }
25
+ }
26
+ expect(RestClient::Request).to receive(:execute).with(hash_including(expected_params)).and_return(QUOTE_NO_PLAN_RESPONSE)
27
+
28
+ Sendle::Api::Quote.execute(params)
29
+ end
30
+
31
+ it "throws an error if params are missing" do
32
+ expect {
33
+ Sendle::Api::Quote.execute
34
+ }.to raise_error(Sendle::Api::Errors::MissingParams, "The following params are required: pickup_suburb, pickup_postcode, delivery_suburb, delivery_postcode, kilogram_weight. Please check your request and try again.")
35
+ end
36
+
37
+ it "returns correct response" do
38
+ expect(RestClient::Request).to receive(:execute).and_return(QUOTE_NO_PLAN_RESPONSE)
39
+
40
+ response = Sendle::Api::Quote.execute(params)
41
+
42
+ expect(response.json).to eq JSON.parse(QUOTE_NO_PLAN_RESPONSE)
43
+ end
44
+
45
+ context "passing in a plan_name" do
46
+ it "checks a valid plan name is passed in" do
47
+ PLANS.each do |plan|
48
+ params[:plan_name] = plan
49
+ expect(RestClient::Request).to receive(:execute).and_return(QUOTE_NO_PLAN_RESPONSE)
50
+ Sendle::Api::Quote.execute(params)
51
+ end
52
+
53
+ params[:plan_name] = 'invalid-plan'
54
+
55
+ expect {
56
+ Sendle::Api::Quote.execute(params)
57
+ }.to raise_error(Sendle::Api::Errors::InvalidPlan, "invalid-plan is not in the list of valid plans: easy, premium, pro")
58
+ end
59
+
60
+ it "respects the plan_name param" do
61
+ expected_params = {
62
+ method: :get,
63
+ url: Sendle::Api::Quote.url,
64
+ headers: {
65
+ accept: :json,
66
+ content_type: :json,
67
+ params: params.merge(plan_name: PLAN_EASY)
68
+ }
69
+ }
70
+ expect(RestClient::Request).to receive(:execute).with(hash_including(expected_params)).and_return(QUOTE_NO_PLAN_RESPONSE)
71
+
72
+ Sendle::Api::Quote.execute(params.merge(plan_name: PLAN_EASY))
73
+ end
74
+ end
75
+
76
+ it "passes optional params" do
77
+ expected_params = {
78
+ method: :get,
79
+ url: Sendle::Api::Quote.url,
80
+ headers: {
81
+ accept: :json,
82
+ content_type: :json,
83
+ params: params.merge(cubic_metre_volume: 0.5)
84
+ }
85
+ }
86
+ expect(RestClient::Request).to receive(:execute).with(hash_including(expected_params)).and_return(QUOTE_NO_PLAN_RESPONSE)
87
+
88
+ Sendle::Api::Quote.execute(params.merge(cubic_metre_volume: 0.5))
89
+ end
90
+
91
+ it "handles 422" do
92
+ expect(RestClient::Request).to receive(:execute).and_raise(UNPROCESSABLE_ENTITY_ERROR)
93
+
94
+ params[:kilogram_weight] = "sdfsdf"
95
+
96
+ expect {
97
+ Sendle::Api::Quote.execute(params)
98
+ }.to raise_error(Sendle::Api::Errors::UnprocessableEntity, "Please fix the following errors and try again - kilogram_weight: is not a number")
99
+ end
100
+
101
+ end
102
+
103
+ end
@@ -17,4 +17,26 @@ describe Sendle::Api do
17
17
 
18
18
  expect(Sendle::Api.sendle_id).to eq sendle_id
19
19
  end
20
+
21
+ it "turns off sandbox mode by default" do
22
+ expect(Sendle::Api.sandbox).to eq false
23
+ end
24
+
25
+ it "allows turning on sandbox mode" do
26
+ Sendle::Api.sandbox = true
27
+
28
+ expect(Sendle::Api.sandbox).to eq true
29
+ end
30
+
31
+ it "uses sandbox api uri when in sandbox mode" do
32
+ Sendle::Api.sandbox = true
33
+
34
+ expect(Sendle::Api.base_url).to eq 'https://sandbox.sendle.com/api/'
35
+ end
36
+
37
+ it "uses live api uri when not in sandbox mode" do
38
+ Sendle::Api.sandbox = false
39
+
40
+ expect(Sendle::Api.base_url).to eq 'https://www.sendle.com/api/'
41
+ end
20
42
  end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe Sendle::Api::Errors::MissingParams do
4
+
5
+ describe "#to_s" do
6
+ it "formats the message" do
7
+ error = Sendle::Api::Errors::MissingParams.new(%w( pickup_address deliver_address ))
8
+
9
+ expect(error.to_s).to eq "The following params are required: pickup_address, deliver_address. Please check your request and try again."
10
+ end
11
+ end
12
+
13
+ end
@@ -4,4 +4,18 @@ def json_headers
4
4
  { headers: { accept: :json, content_type: :json } }
5
5
  end
6
6
 
7
- UNAUTHORIZED_ERROR = RestClient::Unauthorized.new("{\"error\":\"unauthorised\",\"error_description\":\"The authorisation details are not valid. Either the Sendle ID or API key are incorrect.\"}")
7
+ UNAUTHORIZED_ERROR = RestClient::Unauthorized.new("{\"error\":\"unauthorised\",\"error_description\":\"The authorisation details are not valid. Either the Sendle ID or API key are incorrect.\"}")
8
+
9
+ PAYMENT_REQUIRED_ERROR = RestClient::PaymentRequired.new("{\"error\":\"payment_required\",\"error_description\":\"The account associated with this API key has no method of payment. Please go to your Account Settings in your Sendle Dashboard and add a payment method.\"}")
10
+
11
+ UNPROCESSABLE_ENTITY_ERROR = RestClient::UnprocessableEntity.new("{\"messages\":{\"kilogram_weight\":[\"is not a number\"]},\"error\":\"unprocessable_entity\",\"error_description\":\"The data you supplied is invalid. Error messages are in the messages section. Please fix those fields and try again.\"}")
12
+
13
+ PRECONDITION_FAILED_ERROR = RestClient::PreconditionFailed.new("{\"error\":\"precondition_failed\",\"error_description\":\"The account associated with this API key has not accepted the dangerous goods terms. Please visit your Account Settings in https://www.sendle.com/dashboard/ to view and accept these terms.\"}")
14
+
15
+ QUOTE_NO_PLAN_RESPONSE = "[{\"quote\":{\"gross\":{\"amount\":9.75,\"currency\":\"AUD\"},\"net\":{\"amount\":8.86,\"currency\":\"AUD\"},\"tax\":{\"amount\":0.89,\"currency\":\"AUD\"}},\"plan_name\":\"Easy\"},{\"quote\":{\"gross\":{\"amount\":8.75,\"currency\":\"AUD\"},\"net\":{\"amount\":7.95,\"currency\":\"AUD\"},\"tax\":{\"amount\":0.8,\"currency\":\"AUD\"}},\"plan_name\":\"Premium\"},{\"quote\":{\"gross\":{\"amount\":5.98,\"currency\":\"AUD\"},\"net\":{\"amount\":5.44,\"currency\":\"AUD\"},\"tax\":{\"amount\":0.54,\"currency\":\"AUD\"}},\"plan_name\":\"Pro\"}]"
16
+
17
+ ORDER_CREATED_RESPONSE = "{\"order_id\":\"9dbadcd6-ee57-488e-a175-49e378ad29ff\",\"state\":\"Pickup\",\"order_url\":\"https://sendle-sandbox.herokuapp.com/api/orders/9dbadcd6-ee57-488e-a175-49e378ad29ff\",\"labels\":null,\"scheduling\":{\"is_cancellable\":true,\"pickup_date\":\"2016-05-05\"},\"description\":\"Kryptonite\",\"confirmed_not_dangerous\":false,\"kilogram_weight\":\"1.0\",\"cubic_metre_volume\":\"0.01\",\"customer_reference\":\"SupBdayPressie\",\"sender\":{\"contact\":{\"name\":\"Lex Luthor\",\"phone\":\"0412 345 678\",\"email\":\"bobl53@gmail.com\",\"sendle_id\":\"bobl53_gmail_com\"},\"address\":{\"address_line1\":\"123 Gotham Ln\",\"address_line2\":null,\"suburb\":\"Sydney\",\"state_name\":\"NSW\",\"postcode\":\"2000\",\"country\":\"Australia\"},\"instructions\":\"Knock loudly\"},\"receiver\":{\"contact\":{\"name\":\"Clark Kent\",\"phone\":null,\"email\":\"clarkissuper@dailyplanet.xyz\"},\"address\":{\"address_line1\":\"80 Wentworth Park Road\",\"address_line2\":null,\"suburb\":\"Glebe\",\"state_name\":\"NSW\",\"postcode\":\"2037\",\"country\":\"Australia\"},\"instructions\":\"Give directly to Clark\"}}"
18
+
19
+ ORDER_GET_RESPONSE = "{\"order_id\":\"9dbadcd6-ee57-488e-a175-49e378ad29ff\",\"state\":\"Pickup\",\"order_url\":\"https://sendle-sandbox.herokuapp.com/api/orders/9dbadcd6-ee57-488e-a175-49e378ad29ff\",\"labels\":[{\"format\":\"pdf\",\"size\":\"a4\",\"url\":\"https://sendle-sandbox.herokuapp.com/api/orders/9dbadcd6-ee57-488e-a175-49e378ad29ff/labels/a4.pdf\"},{\"format\":\"pdf\",\"size\":\"cropped\",\"url\":\"https://sendle-sandbox.herokuapp.com/api/orders/9dbadcd6-ee57-488e-a175-49e378ad29ff/labels/cropped.pdf\"}],\"scheduling\":{\"is_cancellable\":true,\"pickup_date\":\"2016-05-05\",\"estimated_delivery_date_minimum\":\"2016-05-05\",\"estimated_delivery_date_maximum\":\"2016-05-06\"},\"description\":\"Kryptonite\",\"confirmed_not_dangerous\":false,\"kilogram_weight\":\"1.0\",\"cubic_metre_volume\":\"0.01\",\"customer_reference\":\"SupBdayPressie\",\"sender\":{\"contact\":{\"name\":\"Lex Luthor\",\"phone\":\"0412 345 678\",\"email\":\"bobl53@gmail.com\",\"sendle_id\":\"bobl53_gmail_com\"},\"address\":{\"address_line1\":\"123 Gotham Ln\",\"address_line2\":null,\"suburb\":\"Sydney\",\"state_name\":\"NSW\",\"postcode\":\"2000\",\"country\":\"Australia\"},\"instructions\":\"Knock loudly\"},\"receiver\":{\"contact\":{\"name\":\"Clark Kent\",\"phone\":null,\"email\":\"clarkissuper@dailyplanet.xyz\"},\"address\":{\"address_line1\":\"80 Wentworth Park Road\",\"address_line2\":null,\"suburb\":\"Glebe\",\"state_name\":\"NSW\",\"postcode\":\"2037\",\"country\":\"Australia\"},\"instructions\":\"Give directly to Clark\"}}"
20
+
21
+ ORDER_DELETE_RESPONSE = "{\"order_id\":\"9dbadcd6-ee57-488e-a175-49e378ad29ff\",\"state\":\"Cancelled\",\"order_url\":\"https://sendle-sandbox.herokuapp.com/api/orders/9dbadcd6-ee57-488e-a175-49e378ad29ff\",\"customer_reference\":\"SupBdayPressie\",\"cancelled_at\":\"2016-05-02 23:53:51 UTC\",\"cancellation_message\":\"Cancelled by 4GZwvmnr6Gr8Nd2XJrhKYhxF during picking up\"}"
@@ -32,7 +32,7 @@ shared_examples_for "a resource action with credentials" do
32
32
 
33
33
  expect {
34
34
  described_resource.send(described_action)
35
- }.to_not raise_error
35
+ }.to_not raise_error Sendle::Api::Errors::MissingApiKey
36
36
  end
37
37
  end
38
38
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sendle-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bobby Lei
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-29 00:00:00.000000000 Z
11
+ date: 2016-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -103,22 +103,45 @@ extensions: []
103
103
  extra_rdoc_files: []
104
104
  files:
105
105
  - ".gitignore"
106
+ - Changelog.md
106
107
  - Gemfile
107
108
  - LICENSE.txt
108
109
  - README.md
109
110
  - Rakefile
110
111
  - lib/sendle/api.rb
112
+ - lib/sendle/api/actions/base.rb
113
+ - lib/sendle/api/actions/create.rb
114
+ - lib/sendle/api/actions/destroy.rb
111
115
  - lib/sendle/api/actions/index.rb
116
+ - lib/sendle/api/actions/show.rb
117
+ - lib/sendle/api/constants.rb
118
+ - lib/sendle/api/errors/invalid_plan.rb
112
119
  - lib/sendle/api/errors/missing_api_key.rb
120
+ - lib/sendle/api/errors/missing_params.rb
113
121
  - lib/sendle/api/errors/missing_sendle_id.rb
122
+ - lib/sendle/api/errors/payment_required.rb
123
+ - lib/sendle/api/errors/precondition_failed.rb
114
124
  - lib/sendle/api/errors/unauthorized.rb
125
+ - lib/sendle/api/errors/unprocessable_entity.rb
126
+ - lib/sendle/api/factories/errors.rb
127
+ - lib/sendle/api/order.rb
115
128
  - lib/sendle/api/ping.rb
129
+ - lib/sendle/api/quote.rb
130
+ - lib/sendle/api/resource.rb
131
+ - lib/sendle/api/responses/json.rb
116
132
  - lib/sendle/api/responses/pong.rb
133
+ - lib/sendle/api/sugars/create.rb
134
+ - lib/sendle/api/sugars/destroy.rb
135
+ - lib/sendle/api/sugars/index.rb
136
+ - lib/sendle/api/sugars/show.rb
137
+ - lib/sendle/api/utils.rb
117
138
  - lib/sendle/api/version.rb
118
- - lib/sendle/utils/actions.rb
119
139
  - sendle-api.gemspec
140
+ - spec/sendle/api/order_spec.rb
120
141
  - spec/sendle/api/ping_spec.rb
142
+ - spec/sendle/api/quote_spec.rb
121
143
  - spec/sendle/api_spec.rb
144
+ - spec/sendle/errors/missing_params_spec.rb
122
145
  - spec/spec_helper.rb
123
146
  - spec/support/helpers.rb
124
147
  - spec/support/shared_examples/with_credentials_spec.rb
@@ -148,8 +171,11 @@ signing_key:
148
171
  specification_version: 4
149
172
  summary: Ruby bindings for the Sendle API
150
173
  test_files:
174
+ - spec/sendle/api/order_spec.rb
151
175
  - spec/sendle/api/ping_spec.rb
176
+ - spec/sendle/api/quote_spec.rb
152
177
  - spec/sendle/api_spec.rb
178
+ - spec/sendle/errors/missing_params_spec.rb
153
179
  - spec/spec_helper.rb
154
180
  - spec/support/helpers.rb
155
181
  - spec/support/shared_examples/with_credentials_spec.rb
@@ -1,19 +0,0 @@
1
- module Sendle
2
- module Api
3
- module Utils
4
- class Actions
5
-
6
- def self.check_for_missing_credentials
7
- raise Sendle::Api::Errors::MissingSendleId if Sendle::Api.sendle_id.nil?
8
- raise Sendle::Api::Errors::MissingApiKey if Sendle::Api.api_key.nil?
9
- end
10
-
11
- def self.json_headers
12
- { accept: :json, content_type: :json }
13
- end
14
-
15
- end
16
- end
17
- end
18
-
19
- end