gocardless-pro 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +2 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +132 -0
  6. data/circle.yml +18 -0
  7. data/demo.rb +10 -0
  8. data/gocardless-pro.gemspec +27 -0
  9. data/lib/gocardless-pro.rb +243 -0
  10. data/lib/gocardless-pro/api_service.rb +57 -0
  11. data/lib/gocardless-pro/error.rb +42 -0
  12. data/lib/gocardless-pro/error/gocardless_error.rb +5 -0
  13. data/lib/gocardless-pro/error/invalid_api_usage_error.rb +5 -0
  14. data/lib/gocardless-pro/error/invalid_state_error.rb +5 -0
  15. data/lib/gocardless-pro/error/validation_error.rb +5 -0
  16. data/lib/gocardless-pro/list_response.rb +34 -0
  17. data/lib/gocardless-pro/paginator.rb +37 -0
  18. data/lib/gocardless-pro/request.rb +69 -0
  19. data/lib/gocardless-pro/resources/api_key.rb +62 -0
  20. data/lib/gocardless-pro/resources/creditor.rb +83 -0
  21. data/lib/gocardless-pro/resources/creditor_bank_account.rb +78 -0
  22. data/lib/gocardless-pro/resources/customer.rb +72 -0
  23. data/lib/gocardless-pro/resources/customer_bank_account.rb +80 -0
  24. data/lib/gocardless-pro/resources/event.rb +75 -0
  25. data/lib/gocardless-pro/resources/helper.rb +29 -0
  26. data/lib/gocardless-pro/resources/mandate.rb +70 -0
  27. data/lib/gocardless-pro/resources/payment.rb +86 -0
  28. data/lib/gocardless-pro/resources/payout.rb +66 -0
  29. data/lib/gocardless-pro/resources/publishable_api_key.rb +51 -0
  30. data/lib/gocardless-pro/resources/redirect_flow.rb +104 -0
  31. data/lib/gocardless-pro/resources/refund.rb +70 -0
  32. data/lib/gocardless-pro/resources/role.rb +101 -0
  33. data/lib/gocardless-pro/resources/subscription.rb +152 -0
  34. data/lib/gocardless-pro/resources/user.rb +60 -0
  35. data/lib/gocardless-pro/response.rb +77 -0
  36. data/lib/gocardless-pro/services/api_key_service.rb +130 -0
  37. data/lib/gocardless-pro/services/base_service.rb +29 -0
  38. data/lib/gocardless-pro/services/creditor_bank_account_service.rb +122 -0
  39. data/lib/gocardless-pro/services/creditor_service.rb +112 -0
  40. data/lib/gocardless-pro/services/customer_bank_account_service.rb +153 -0
  41. data/lib/gocardless-pro/services/customer_service.rb +112 -0
  42. data/lib/gocardless-pro/services/event_service.rb +80 -0
  43. data/lib/gocardless-pro/services/helper_service.rb +97 -0
  44. data/lib/gocardless-pro/services/mandate_service.rb +170 -0
  45. data/lib/gocardless-pro/services/payment_service.rb +164 -0
  46. data/lib/gocardless-pro/services/payout_service.rb +80 -0
  47. data/lib/gocardless-pro/services/publishable_api_key_service.rb +130 -0
  48. data/lib/gocardless-pro/services/redirect_flow_service.rb +96 -0
  49. data/lib/gocardless-pro/services/refund_service.rb +126 -0
  50. data/lib/gocardless-pro/services/role_service.rb +127 -0
  51. data/lib/gocardless-pro/services/subscription_service.rb +133 -0
  52. data/lib/gocardless-pro/services/user_service.rb +148 -0
  53. data/lib/gocardless-pro/version.rb +8 -0
  54. data/spec/api_service_spec.rb +69 -0
  55. data/spec/client_spec.rb +29 -0
  56. data/spec/error_spec.rb +44 -0
  57. data/spec/resources/api_key_spec.rb +85 -0
  58. data/spec/resources/creditor_bank_account_spec.rb +109 -0
  59. data/spec/resources/creditor_spec.rb +125 -0
  60. data/spec/resources/customer_bank_account_spec.rb +109 -0
  61. data/spec/resources/customer_spec.rb +127 -0
  62. data/spec/resources/event_spec.rb +113 -0
  63. data/spec/resources/helper_spec.rb +23 -0
  64. data/spec/resources/mandate_spec.rb +97 -0
  65. data/spec/resources/payment_spec.rb +129 -0
  66. data/spec/resources/payout_spec.rb +89 -0
  67. data/spec/resources/publishable_api_key_spec.rb +63 -0
  68. data/spec/resources/redirect_flow_spec.rb +97 -0
  69. data/spec/resources/refund_spec.rb +77 -0
  70. data/spec/resources/role_spec.rb +63 -0
  71. data/spec/resources/subscription_spec.rb +157 -0
  72. data/spec/resources/user_spec.rb +85 -0
  73. data/spec/response_spec.rb +79 -0
  74. data/spec/services/api_key_service_spec.rb +362 -0
  75. data/spec/services/creditor_bank_account_service_spec.rb +365 -0
  76. data/spec/services/creditor_service_spec.rb +339 -0
  77. data/spec/services/customer_bank_account_service_spec.rb +404 -0
  78. data/spec/services/customer_service_spec.rb +365 -0
  79. data/spec/services/event_service_spec.rb +172 -0
  80. data/spec/services/helper_service_spec.rb +123 -0
  81. data/spec/services/mandate_service_spec.rb +449 -0
  82. data/spec/services/payment_service_spec.rb +497 -0
  83. data/spec/services/payout_service_spec.rb +172 -0
  84. data/spec/services/publishable_api_key_service_spec.rb +336 -0
  85. data/spec/services/redirect_flow_service_spec.rb +208 -0
  86. data/spec/services/refund_service_spec.rb +279 -0
  87. data/spec/services/role_service_spec.rb +336 -0
  88. data/spec/services/subscription_service_spec.rb +488 -0
  89. data/spec/services/user_service_spec.rb +433 -0
  90. data/spec/spec_helper.rb +91 -0
  91. metadata +255 -0
@@ -0,0 +1,336 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardless::Services::RoleService do
4
+ let(:client) do
5
+ GoCardless::Client.new(
6
+ api_key: "AK123",
7
+ api_secret: "ABC"
8
+ )
9
+ end
10
+
11
+
12
+
13
+
14
+
15
+
16
+ describe "#create" do
17
+ subject(:post_create_response) { client.roles.create(new_resource) }
18
+ context "with a valid request" do
19
+ let(:new_resource) do
20
+ {
21
+
22
+ "created_at" => "created_at-input",
23
+ "enabled" => "enabled-input",
24
+ "id" => "id-input",
25
+ "name" => "name-input",
26
+ "permissions" => "permissions-input",
27
+ }
28
+ end
29
+
30
+ before do
31
+ stub_request(:post, /.*api.gocardless.com\/roles/).
32
+ with(
33
+ body: {
34
+ roles: {
35
+
36
+ "created_at" => "created_at-input",
37
+ "enabled" => "enabled-input",
38
+ "id" => "id-input",
39
+ "name" => "name-input",
40
+ "permissions" => "permissions-input",
41
+ }
42
+ }
43
+ ).
44
+ to_return(
45
+ body: {
46
+ roles: {
47
+
48
+ "created_at" => "created_at-input",
49
+ "enabled" => "enabled-input",
50
+ "id" => "id-input",
51
+ "name" => "name-input",
52
+ "permissions" => "permissions-input",
53
+ }
54
+ }.to_json,
55
+ :headers => {'Content-Type' => 'application/json'}
56
+ )
57
+ end
58
+
59
+ it "creates and returns the resource" do
60
+ expect(post_create_response).to be_a(GoCardless::Resources::Role)
61
+ end
62
+ end
63
+
64
+ context "with a request that returns a validation error" do
65
+ let(:new_resource) { {} }
66
+
67
+ before do
68
+ stub_request(:post, /.*api.gocardless.com\/roles/).to_return(
69
+ body: {
70
+ error: {
71
+ type: 'validation_failed',
72
+ code: 422,
73
+ errors: [
74
+ { message: 'test error message', field: 'test_field' }
75
+ ]
76
+ }
77
+ }.to_json,
78
+ headers: {'Content-Type' => 'application/json'},
79
+ status: 422
80
+ )
81
+ end
82
+
83
+ it "throws the correct error" do
84
+ expect { post_create_response }.to raise_error(GoCardless::ValidationError)
85
+ end
86
+ end
87
+ end
88
+
89
+
90
+
91
+
92
+
93
+ describe "#list" do
94
+ describe "with no filters" do
95
+ subject(:get_list_response) { client.roles.list }
96
+
97
+ before do
98
+ stub_request(:get, /.*api.gocardless.com\/roles/).to_return(
99
+ body: {
100
+ roles: [{
101
+
102
+ "created_at" => "created_at-input",
103
+ "enabled" => "enabled-input",
104
+ "id" => "id-input",
105
+ "name" => "name-input",
106
+ "permissions" => "permissions-input",
107
+ }],
108
+ meta: {
109
+ cursors: {
110
+ before: nil,
111
+ after: "ABC123"
112
+ }
113
+ }
114
+ }.to_json,
115
+ :headers => {'Content-Type' => 'application/json'}
116
+ )
117
+ end
118
+
119
+ it "wraps each item in the resource class" do
120
+ expect(get_list_response.map { |x| x.class }.uniq.first).to eq(GoCardless::Resources::Role)
121
+
122
+
123
+
124
+ expect(get_list_response.first.created_at).to eq("created_at-input")
125
+
126
+
127
+
128
+ expect(get_list_response.first.enabled).to eq("enabled-input")
129
+
130
+
131
+
132
+ expect(get_list_response.first.id).to eq("id-input")
133
+
134
+
135
+
136
+ expect(get_list_response.first.name).to eq("name-input")
137
+
138
+
139
+
140
+ expect(get_list_response.first.permissions).to eq("permissions-input")
141
+
142
+
143
+ end
144
+
145
+ it "exposes the cursors for before and after" do
146
+ expect(get_list_response.before).to eq(nil)
147
+ expect(get_list_response.after).to eq("ABC123")
148
+ end
149
+ end
150
+ end
151
+
152
+ describe "#all" do
153
+ let!(:first_response_stub) do
154
+ stub_request(:get, /.*api.gocardless.com\/roles$/).to_return(
155
+ body: {
156
+ roles: [{
157
+
158
+ "created_at" => "created_at-input",
159
+ "enabled" => "enabled-input",
160
+ "id" => "id-input",
161
+ "name" => "name-input",
162
+ "permissions" => "permissions-input",
163
+ }],
164
+ meta: {
165
+ cursors: { after: 'AB345' },
166
+ limit: 1
167
+ }
168
+ }.to_json,
169
+ :headers => {'Content-Type' => 'application/json'}
170
+ )
171
+ end
172
+
173
+ let!(:second_response_stub) do
174
+ stub_request(:get, /.*api.gocardless.com\/roles\?after=AB345/).to_return(
175
+ body: {
176
+ roles: [{
177
+
178
+ "created_at" => "created_at-input",
179
+ "enabled" => "enabled-input",
180
+ "id" => "id-input",
181
+ "name" => "name-input",
182
+ "permissions" => "permissions-input",
183
+ }],
184
+ meta: {
185
+ limit: 2,
186
+ cursors: {}
187
+ }
188
+ }.to_json,
189
+ :headers => {'Content-Type' => 'application/json'}
190
+ )
191
+ end
192
+
193
+ it "automatically makes the extra requests" do
194
+ expect(client.roles.all.to_a.length).to eq(2)
195
+ expect(first_response_stub).to have_been_requested
196
+ expect(second_response_stub).to have_been_requested
197
+ end
198
+ end
199
+
200
+
201
+
202
+
203
+
204
+
205
+ describe "#get" do
206
+ let(:id) { "ID123" }
207
+
208
+ subject(:get_response) { client.roles.get(id) }
209
+
210
+ context "when there is a role to return" do
211
+ before do
212
+ stub_request(:get, /.*api.gocardless.com\/roles\/ID123/).to_return(
213
+ body: {
214
+ roles: {
215
+
216
+ "created_at" => "created_at-input",
217
+ "enabled" => "enabled-input",
218
+ "id" => "id-input",
219
+ "name" => "name-input",
220
+ "permissions" => "permissions-input",
221
+ }
222
+ }.to_json,
223
+ :headers => {'Content-Type' => 'application/json'}
224
+ )
225
+ end
226
+
227
+ it "wraps the response in a resource" do
228
+ expect(get_response).to be_a(GoCardless::Resources::Role)
229
+ end
230
+ end
231
+ end
232
+
233
+
234
+
235
+
236
+
237
+
238
+ describe "#update" do
239
+ subject(:put_update_response) { client.roles.update(id, update_params) }
240
+ let(:id) { "ABC123" }
241
+
242
+ context "with a valid request" do
243
+ let(:update_params) { { "hello" => "world" } }
244
+
245
+ let!(:stub) do
246
+ stub_request(:put, /.*api.gocardless.com\/roles\/ABC123/).to_return(
247
+ body: {
248
+ roles: {
249
+
250
+ "created_at" => "created_at-input",
251
+ "enabled" => "enabled-input",
252
+ "id" => "id-input",
253
+ "name" => "name-input",
254
+ "permissions" => "permissions-input",
255
+ }
256
+ }.to_json,
257
+ :headers => {'Content-Type' => 'application/json'}
258
+ )
259
+ end
260
+
261
+ it "updates and returns the resource" do
262
+ expect(put_update_response).to be_a(GoCardless::Resources::Role)
263
+ expect(stub).to have_been_requested
264
+ end
265
+ end
266
+ end
267
+
268
+
269
+
270
+
271
+
272
+ describe "#disable" do
273
+
274
+
275
+ subject(:post_response) { client.roles.disable(resource_id) }
276
+
277
+ let(:resource_id) { "ABC123" }
278
+
279
+ let!(:stub) do
280
+ # /roles/%v/actions/disable
281
+ stub_url = "/roles/:identity/actions/disable".gsub(':identity', resource_id)
282
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
283
+ body: {
284
+ roles: {
285
+
286
+ "created_at" => "created_at-input",
287
+ "enabled" => "enabled-input",
288
+ "id" => "id-input",
289
+ "name" => "name-input",
290
+ "permissions" => "permissions-input",
291
+ }
292
+ }.to_json,
293
+ headers: {'Content-Type' => 'application/json'},
294
+ )
295
+ end
296
+
297
+ it "wraps the response and calls the right endpoint" do
298
+ expect(post_response).to be_a(GoCardless::Resources::Role)
299
+
300
+ expect(stub).to have_been_requested
301
+ end
302
+
303
+ context "when the request needs a body and custom header" do
304
+
305
+ let(:body) { { foo: 'bar' } }
306
+ let(:headers) { { 'Foo' => 'Bar' } }
307
+ subject(:post_response) { client.roles.disable(resource_id, body, headers) }
308
+
309
+ let(:resource_id) { "ABC123" }
310
+
311
+ let!(:stub) do
312
+ # /roles/%v/actions/disable
313
+ stub_url = "/roles/:identity/actions/disable".gsub(':identity', resource_id)
314
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
315
+ with(
316
+ body: { foo: 'bar' },
317
+ headers: { 'Foo' => 'Bar' }
318
+ ).to_return(
319
+ body: {
320
+ roles: {
321
+
322
+ "created_at" => "created_at-input",
323
+ "enabled" => "enabled-input",
324
+ "id" => "id-input",
325
+ "name" => "name-input",
326
+ "permissions" => "permissions-input",
327
+ }
328
+ }.to_json,
329
+ headers: {'Content-Type' => 'application/json'},
330
+ )
331
+ end
332
+ end
333
+ end
334
+
335
+
336
+ end
@@ -0,0 +1,488 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardless::Services::SubscriptionService do
4
+ let(:client) do
5
+ GoCardless::Client.new(
6
+ api_key: "AK123",
7
+ api_secret: "ABC"
8
+ )
9
+ end
10
+
11
+
12
+
13
+
14
+
15
+
16
+ describe "#create" do
17
+ subject(:post_create_response) { client.subscriptions.create(new_resource) }
18
+ context "with a valid request" do
19
+ let(:new_resource) do
20
+ {
21
+
22
+ "amount" => "amount-input",
23
+ "count" => "count-input",
24
+ "created_at" => "created_at-input",
25
+ "currency" => "currency-input",
26
+ "day_of_month" => "day_of_month-input",
27
+ "end_at" => "end_at-input",
28
+ "id" => "id-input",
29
+ "interval" => "interval-input",
30
+ "interval_unit" => "interval_unit-input",
31
+ "links" => "links-input",
32
+ "metadata" => "metadata-input",
33
+ "month" => "month-input",
34
+ "name" => "name-input",
35
+ "start_at" => "start_at-input",
36
+ "status" => "status-input",
37
+ "upcoming_payments" => "upcoming_payments-input",
38
+ }
39
+ end
40
+
41
+ before do
42
+ stub_request(:post, /.*api.gocardless.com\/subscriptions/).
43
+ with(
44
+ body: {
45
+ subscriptions: {
46
+
47
+ "amount" => "amount-input",
48
+ "count" => "count-input",
49
+ "created_at" => "created_at-input",
50
+ "currency" => "currency-input",
51
+ "day_of_month" => "day_of_month-input",
52
+ "end_at" => "end_at-input",
53
+ "id" => "id-input",
54
+ "interval" => "interval-input",
55
+ "interval_unit" => "interval_unit-input",
56
+ "links" => "links-input",
57
+ "metadata" => "metadata-input",
58
+ "month" => "month-input",
59
+ "name" => "name-input",
60
+ "start_at" => "start_at-input",
61
+ "status" => "status-input",
62
+ "upcoming_payments" => "upcoming_payments-input",
63
+ }
64
+ }
65
+ ).
66
+ to_return(
67
+ body: {
68
+ subscriptions: {
69
+
70
+ "amount" => "amount-input",
71
+ "count" => "count-input",
72
+ "created_at" => "created_at-input",
73
+ "currency" => "currency-input",
74
+ "day_of_month" => "day_of_month-input",
75
+ "end_at" => "end_at-input",
76
+ "id" => "id-input",
77
+ "interval" => "interval-input",
78
+ "interval_unit" => "interval_unit-input",
79
+ "links" => "links-input",
80
+ "metadata" => "metadata-input",
81
+ "month" => "month-input",
82
+ "name" => "name-input",
83
+ "start_at" => "start_at-input",
84
+ "status" => "status-input",
85
+ "upcoming_payments" => "upcoming_payments-input",
86
+ }
87
+ }.to_json,
88
+ :headers => {'Content-Type' => 'application/json'}
89
+ )
90
+ end
91
+
92
+ it "creates and returns the resource" do
93
+ expect(post_create_response).to be_a(GoCardless::Resources::Subscription)
94
+ end
95
+ end
96
+
97
+ context "with a request that returns a validation error" do
98
+ let(:new_resource) { {} }
99
+
100
+ before do
101
+ stub_request(:post, /.*api.gocardless.com\/subscriptions/).to_return(
102
+ body: {
103
+ error: {
104
+ type: 'validation_failed',
105
+ code: 422,
106
+ errors: [
107
+ { message: 'test error message', field: 'test_field' }
108
+ ]
109
+ }
110
+ }.to_json,
111
+ headers: {'Content-Type' => 'application/json'},
112
+ status: 422
113
+ )
114
+ end
115
+
116
+ it "throws the correct error" do
117
+ expect { post_create_response }.to raise_error(GoCardless::ValidationError)
118
+ end
119
+ end
120
+ end
121
+
122
+
123
+
124
+
125
+
126
+ describe "#list" do
127
+ describe "with no filters" do
128
+ subject(:get_list_response) { client.subscriptions.list }
129
+
130
+ before do
131
+ stub_request(:get, /.*api.gocardless.com\/subscriptions/).to_return(
132
+ body: {
133
+ subscriptions: [{
134
+
135
+ "amount" => "amount-input",
136
+ "count" => "count-input",
137
+ "created_at" => "created_at-input",
138
+ "currency" => "currency-input",
139
+ "day_of_month" => "day_of_month-input",
140
+ "end_at" => "end_at-input",
141
+ "id" => "id-input",
142
+ "interval" => "interval-input",
143
+ "interval_unit" => "interval_unit-input",
144
+ "links" => "links-input",
145
+ "metadata" => "metadata-input",
146
+ "month" => "month-input",
147
+ "name" => "name-input",
148
+ "start_at" => "start_at-input",
149
+ "status" => "status-input",
150
+ "upcoming_payments" => "upcoming_payments-input",
151
+ }],
152
+ meta: {
153
+ cursors: {
154
+ before: nil,
155
+ after: "ABC123"
156
+ }
157
+ }
158
+ }.to_json,
159
+ :headers => {'Content-Type' => 'application/json'}
160
+ )
161
+ end
162
+
163
+ it "wraps each item in the resource class" do
164
+ expect(get_list_response.map { |x| x.class }.uniq.first).to eq(GoCardless::Resources::Subscription)
165
+
166
+
167
+
168
+ expect(get_list_response.first.amount).to eq("amount-input")
169
+
170
+
171
+
172
+ expect(get_list_response.first.count).to eq("count-input")
173
+
174
+
175
+
176
+ expect(get_list_response.first.created_at).to eq("created_at-input")
177
+
178
+
179
+
180
+ expect(get_list_response.first.currency).to eq("currency-input")
181
+
182
+
183
+
184
+ expect(get_list_response.first.day_of_month).to eq("day_of_month-input")
185
+
186
+
187
+
188
+ expect(get_list_response.first.end_at).to eq("end_at-input")
189
+
190
+
191
+
192
+ expect(get_list_response.first.id).to eq("id-input")
193
+
194
+
195
+
196
+ expect(get_list_response.first.interval).to eq("interval-input")
197
+
198
+
199
+
200
+ expect(get_list_response.first.interval_unit).to eq("interval_unit-input")
201
+
202
+
203
+
204
+
205
+
206
+ expect(get_list_response.first.metadata).to eq("metadata-input")
207
+
208
+
209
+
210
+ expect(get_list_response.first.month).to eq("month-input")
211
+
212
+
213
+
214
+ expect(get_list_response.first.name).to eq("name-input")
215
+
216
+
217
+
218
+ expect(get_list_response.first.start_at).to eq("start_at-input")
219
+
220
+
221
+
222
+ expect(get_list_response.first.status).to eq("status-input")
223
+
224
+
225
+
226
+ expect(get_list_response.first.upcoming_payments).to eq("upcoming_payments-input")
227
+
228
+
229
+ end
230
+
231
+ it "exposes the cursors for before and after" do
232
+ expect(get_list_response.before).to eq(nil)
233
+ expect(get_list_response.after).to eq("ABC123")
234
+ end
235
+ end
236
+ end
237
+
238
+ describe "#all" do
239
+ let!(:first_response_stub) do
240
+ stub_request(:get, /.*api.gocardless.com\/subscriptions$/).to_return(
241
+ body: {
242
+ subscriptions: [{
243
+
244
+ "amount" => "amount-input",
245
+ "count" => "count-input",
246
+ "created_at" => "created_at-input",
247
+ "currency" => "currency-input",
248
+ "day_of_month" => "day_of_month-input",
249
+ "end_at" => "end_at-input",
250
+ "id" => "id-input",
251
+ "interval" => "interval-input",
252
+ "interval_unit" => "interval_unit-input",
253
+ "links" => "links-input",
254
+ "metadata" => "metadata-input",
255
+ "month" => "month-input",
256
+ "name" => "name-input",
257
+ "start_at" => "start_at-input",
258
+ "status" => "status-input",
259
+ "upcoming_payments" => "upcoming_payments-input",
260
+ }],
261
+ meta: {
262
+ cursors: { after: 'AB345' },
263
+ limit: 1
264
+ }
265
+ }.to_json,
266
+ :headers => {'Content-Type' => 'application/json'}
267
+ )
268
+ end
269
+
270
+ let!(:second_response_stub) do
271
+ stub_request(:get, /.*api.gocardless.com\/subscriptions\?after=AB345/).to_return(
272
+ body: {
273
+ subscriptions: [{
274
+
275
+ "amount" => "amount-input",
276
+ "count" => "count-input",
277
+ "created_at" => "created_at-input",
278
+ "currency" => "currency-input",
279
+ "day_of_month" => "day_of_month-input",
280
+ "end_at" => "end_at-input",
281
+ "id" => "id-input",
282
+ "interval" => "interval-input",
283
+ "interval_unit" => "interval_unit-input",
284
+ "links" => "links-input",
285
+ "metadata" => "metadata-input",
286
+ "month" => "month-input",
287
+ "name" => "name-input",
288
+ "start_at" => "start_at-input",
289
+ "status" => "status-input",
290
+ "upcoming_payments" => "upcoming_payments-input",
291
+ }],
292
+ meta: {
293
+ limit: 2,
294
+ cursors: {}
295
+ }
296
+ }.to_json,
297
+ :headers => {'Content-Type' => 'application/json'}
298
+ )
299
+ end
300
+
301
+ it "automatically makes the extra requests" do
302
+ expect(client.subscriptions.all.to_a.length).to eq(2)
303
+ expect(first_response_stub).to have_been_requested
304
+ expect(second_response_stub).to have_been_requested
305
+ end
306
+ end
307
+
308
+
309
+
310
+
311
+
312
+
313
+ describe "#get" do
314
+ let(:id) { "ID123" }
315
+
316
+ subject(:get_response) { client.subscriptions.get(id) }
317
+
318
+ context "when there is a subscription to return" do
319
+ before do
320
+ stub_request(:get, /.*api.gocardless.com\/subscriptions\/ID123/).to_return(
321
+ body: {
322
+ subscriptions: {
323
+
324
+ "amount" => "amount-input",
325
+ "count" => "count-input",
326
+ "created_at" => "created_at-input",
327
+ "currency" => "currency-input",
328
+ "day_of_month" => "day_of_month-input",
329
+ "end_at" => "end_at-input",
330
+ "id" => "id-input",
331
+ "interval" => "interval-input",
332
+ "interval_unit" => "interval_unit-input",
333
+ "links" => "links-input",
334
+ "metadata" => "metadata-input",
335
+ "month" => "month-input",
336
+ "name" => "name-input",
337
+ "start_at" => "start_at-input",
338
+ "status" => "status-input",
339
+ "upcoming_payments" => "upcoming_payments-input",
340
+ }
341
+ }.to_json,
342
+ :headers => {'Content-Type' => 'application/json'}
343
+ )
344
+ end
345
+
346
+ it "wraps the response in a resource" do
347
+ expect(get_response).to be_a(GoCardless::Resources::Subscription)
348
+ end
349
+ end
350
+ end
351
+
352
+
353
+
354
+
355
+
356
+
357
+ describe "#update" do
358
+ subject(:put_update_response) { client.subscriptions.update(id, update_params) }
359
+ let(:id) { "ABC123" }
360
+
361
+ context "with a valid request" do
362
+ let(:update_params) { { "hello" => "world" } }
363
+
364
+ let!(:stub) do
365
+ stub_request(:put, /.*api.gocardless.com\/subscriptions\/ABC123/).to_return(
366
+ body: {
367
+ subscriptions: {
368
+
369
+ "amount" => "amount-input",
370
+ "count" => "count-input",
371
+ "created_at" => "created_at-input",
372
+ "currency" => "currency-input",
373
+ "day_of_month" => "day_of_month-input",
374
+ "end_at" => "end_at-input",
375
+ "id" => "id-input",
376
+ "interval" => "interval-input",
377
+ "interval_unit" => "interval_unit-input",
378
+ "links" => "links-input",
379
+ "metadata" => "metadata-input",
380
+ "month" => "month-input",
381
+ "name" => "name-input",
382
+ "start_at" => "start_at-input",
383
+ "status" => "status-input",
384
+ "upcoming_payments" => "upcoming_payments-input",
385
+ }
386
+ }.to_json,
387
+ :headers => {'Content-Type' => 'application/json'}
388
+ )
389
+ end
390
+
391
+ it "updates and returns the resource" do
392
+ expect(put_update_response).to be_a(GoCardless::Resources::Subscription)
393
+ expect(stub).to have_been_requested
394
+ end
395
+ end
396
+ end
397
+
398
+
399
+
400
+
401
+
402
+ describe "#cancel" do
403
+
404
+
405
+ subject(:post_response) { client.subscriptions.cancel(resource_id) }
406
+
407
+ let(:resource_id) { "ABC123" }
408
+
409
+ let!(:stub) do
410
+ # /subscriptions/%v/actions/cancel
411
+ stub_url = "/subscriptions/:identity/actions/cancel".gsub(':identity', resource_id)
412
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
413
+ body: {
414
+ subscriptions: {
415
+
416
+ "amount" => "amount-input",
417
+ "count" => "count-input",
418
+ "created_at" => "created_at-input",
419
+ "currency" => "currency-input",
420
+ "day_of_month" => "day_of_month-input",
421
+ "end_at" => "end_at-input",
422
+ "id" => "id-input",
423
+ "interval" => "interval-input",
424
+ "interval_unit" => "interval_unit-input",
425
+ "links" => "links-input",
426
+ "metadata" => "metadata-input",
427
+ "month" => "month-input",
428
+ "name" => "name-input",
429
+ "start_at" => "start_at-input",
430
+ "status" => "status-input",
431
+ "upcoming_payments" => "upcoming_payments-input",
432
+ }
433
+ }.to_json,
434
+ headers: {'Content-Type' => 'application/json'},
435
+ )
436
+ end
437
+
438
+ it "wraps the response and calls the right endpoint" do
439
+ expect(post_response).to be_a(GoCardless::Resources::Subscription)
440
+
441
+ expect(stub).to have_been_requested
442
+ end
443
+
444
+ context "when the request needs a body and custom header" do
445
+
446
+ let(:body) { { foo: 'bar' } }
447
+ let(:headers) { { 'Foo' => 'Bar' } }
448
+ subject(:post_response) { client.subscriptions.cancel(resource_id, body, headers) }
449
+
450
+ let(:resource_id) { "ABC123" }
451
+
452
+ let!(:stub) do
453
+ # /subscriptions/%v/actions/cancel
454
+ stub_url = "/subscriptions/:identity/actions/cancel".gsub(':identity', resource_id)
455
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
456
+ with(
457
+ body: { foo: 'bar' },
458
+ headers: { 'Foo' => 'Bar' }
459
+ ).to_return(
460
+ body: {
461
+ subscriptions: {
462
+
463
+ "amount" => "amount-input",
464
+ "count" => "count-input",
465
+ "created_at" => "created_at-input",
466
+ "currency" => "currency-input",
467
+ "day_of_month" => "day_of_month-input",
468
+ "end_at" => "end_at-input",
469
+ "id" => "id-input",
470
+ "interval" => "interval-input",
471
+ "interval_unit" => "interval_unit-input",
472
+ "links" => "links-input",
473
+ "metadata" => "metadata-input",
474
+ "month" => "month-input",
475
+ "name" => "name-input",
476
+ "start_at" => "start_at-input",
477
+ "status" => "status-input",
478
+ "upcoming_payments" => "upcoming_payments-input",
479
+ }
480
+ }.to_json,
481
+ headers: {'Content-Type' => 'application/json'},
482
+ )
483
+ end
484
+ end
485
+ end
486
+
487
+
488
+ end