gocardless-pro 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,433 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardless::Services::UserService 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.users.create(new_resource) }
18
+ context "with a valid request" do
19
+ let(:new_resource) do
20
+ {
21
+
22
+ "created_at" => "created_at-input",
23
+ "email" => "email-input",
24
+ "enabled" => "enabled-input",
25
+ "family_name" => "family_name-input",
26
+ "given_name" => "given_name-input",
27
+ "id" => "id-input",
28
+ "links" => "links-input",
29
+ }
30
+ end
31
+
32
+ before do
33
+ stub_request(:post, /.*api.gocardless.com\/users/).
34
+ with(
35
+ body: {
36
+ users: {
37
+
38
+ "created_at" => "created_at-input",
39
+ "email" => "email-input",
40
+ "enabled" => "enabled-input",
41
+ "family_name" => "family_name-input",
42
+ "given_name" => "given_name-input",
43
+ "id" => "id-input",
44
+ "links" => "links-input",
45
+ }
46
+ }
47
+ ).
48
+ to_return(
49
+ body: {
50
+ users: {
51
+
52
+ "created_at" => "created_at-input",
53
+ "email" => "email-input",
54
+ "enabled" => "enabled-input",
55
+ "family_name" => "family_name-input",
56
+ "given_name" => "given_name-input",
57
+ "id" => "id-input",
58
+ "links" => "links-input",
59
+ }
60
+ }.to_json,
61
+ :headers => {'Content-Type' => 'application/json'}
62
+ )
63
+ end
64
+
65
+ it "creates and returns the resource" do
66
+ expect(post_create_response).to be_a(GoCardless::Resources::User)
67
+ end
68
+ end
69
+
70
+ context "with a request that returns a validation error" do
71
+ let(:new_resource) { {} }
72
+
73
+ before do
74
+ stub_request(:post, /.*api.gocardless.com\/users/).to_return(
75
+ body: {
76
+ error: {
77
+ type: 'validation_failed',
78
+ code: 422,
79
+ errors: [
80
+ { message: 'test error message', field: 'test_field' }
81
+ ]
82
+ }
83
+ }.to_json,
84
+ headers: {'Content-Type' => 'application/json'},
85
+ status: 422
86
+ )
87
+ end
88
+
89
+ it "throws the correct error" do
90
+ expect { post_create_response }.to raise_error(GoCardless::ValidationError)
91
+ end
92
+ end
93
+ end
94
+
95
+
96
+
97
+
98
+
99
+ describe "#list" do
100
+ describe "with no filters" do
101
+ subject(:get_list_response) { client.users.list }
102
+
103
+ before do
104
+ stub_request(:get, /.*api.gocardless.com\/users/).to_return(
105
+ body: {
106
+ users: [{
107
+
108
+ "created_at" => "created_at-input",
109
+ "email" => "email-input",
110
+ "enabled" => "enabled-input",
111
+ "family_name" => "family_name-input",
112
+ "given_name" => "given_name-input",
113
+ "id" => "id-input",
114
+ "links" => "links-input",
115
+ }],
116
+ meta: {
117
+ cursors: {
118
+ before: nil,
119
+ after: "ABC123"
120
+ }
121
+ }
122
+ }.to_json,
123
+ :headers => {'Content-Type' => 'application/json'}
124
+ )
125
+ end
126
+
127
+ it "wraps each item in the resource class" do
128
+ expect(get_list_response.map { |x| x.class }.uniq.first).to eq(GoCardless::Resources::User)
129
+
130
+
131
+
132
+ expect(get_list_response.first.created_at).to eq("created_at-input")
133
+
134
+
135
+
136
+ expect(get_list_response.first.email).to eq("email-input")
137
+
138
+
139
+
140
+ expect(get_list_response.first.enabled).to eq("enabled-input")
141
+
142
+
143
+
144
+ expect(get_list_response.first.family_name).to eq("family_name-input")
145
+
146
+
147
+
148
+ expect(get_list_response.first.given_name).to eq("given_name-input")
149
+
150
+
151
+
152
+ expect(get_list_response.first.id).to eq("id-input")
153
+
154
+
155
+
156
+
157
+ end
158
+
159
+ it "exposes the cursors for before and after" do
160
+ expect(get_list_response.before).to eq(nil)
161
+ expect(get_list_response.after).to eq("ABC123")
162
+ end
163
+ end
164
+ end
165
+
166
+ describe "#all" do
167
+ let!(:first_response_stub) do
168
+ stub_request(:get, /.*api.gocardless.com\/users$/).to_return(
169
+ body: {
170
+ users: [{
171
+
172
+ "created_at" => "created_at-input",
173
+ "email" => "email-input",
174
+ "enabled" => "enabled-input",
175
+ "family_name" => "family_name-input",
176
+ "given_name" => "given_name-input",
177
+ "id" => "id-input",
178
+ "links" => "links-input",
179
+ }],
180
+ meta: {
181
+ cursors: { after: 'AB345' },
182
+ limit: 1
183
+ }
184
+ }.to_json,
185
+ :headers => {'Content-Type' => 'application/json'}
186
+ )
187
+ end
188
+
189
+ let!(:second_response_stub) do
190
+ stub_request(:get, /.*api.gocardless.com\/users\?after=AB345/).to_return(
191
+ body: {
192
+ users: [{
193
+
194
+ "created_at" => "created_at-input",
195
+ "email" => "email-input",
196
+ "enabled" => "enabled-input",
197
+ "family_name" => "family_name-input",
198
+ "given_name" => "given_name-input",
199
+ "id" => "id-input",
200
+ "links" => "links-input",
201
+ }],
202
+ meta: {
203
+ limit: 2,
204
+ cursors: {}
205
+ }
206
+ }.to_json,
207
+ :headers => {'Content-Type' => 'application/json'}
208
+ )
209
+ end
210
+
211
+ it "automatically makes the extra requests" do
212
+ expect(client.users.all.to_a.length).to eq(2)
213
+ expect(first_response_stub).to have_been_requested
214
+ expect(second_response_stub).to have_been_requested
215
+ end
216
+ end
217
+
218
+
219
+
220
+
221
+
222
+
223
+ describe "#get" do
224
+ let(:id) { "ID123" }
225
+
226
+ subject(:get_response) { client.users.get(id) }
227
+
228
+ context "when there is a user to return" do
229
+ before do
230
+ stub_request(:get, /.*api.gocardless.com\/users\/ID123/).to_return(
231
+ body: {
232
+ users: {
233
+
234
+ "created_at" => "created_at-input",
235
+ "email" => "email-input",
236
+ "enabled" => "enabled-input",
237
+ "family_name" => "family_name-input",
238
+ "given_name" => "given_name-input",
239
+ "id" => "id-input",
240
+ "links" => "links-input",
241
+ }
242
+ }.to_json,
243
+ :headers => {'Content-Type' => 'application/json'}
244
+ )
245
+ end
246
+
247
+ it "wraps the response in a resource" do
248
+ expect(get_response).to be_a(GoCardless::Resources::User)
249
+ end
250
+ end
251
+ end
252
+
253
+
254
+
255
+
256
+
257
+
258
+ describe "#update" do
259
+ subject(:put_update_response) { client.users.update(id, update_params) }
260
+ let(:id) { "ABC123" }
261
+
262
+ context "with a valid request" do
263
+ let(:update_params) { { "hello" => "world" } }
264
+
265
+ let!(:stub) do
266
+ stub_request(:put, /.*api.gocardless.com\/users\/ABC123/).to_return(
267
+ body: {
268
+ users: {
269
+
270
+ "created_at" => "created_at-input",
271
+ "email" => "email-input",
272
+ "enabled" => "enabled-input",
273
+ "family_name" => "family_name-input",
274
+ "given_name" => "given_name-input",
275
+ "id" => "id-input",
276
+ "links" => "links-input",
277
+ }
278
+ }.to_json,
279
+ :headers => {'Content-Type' => 'application/json'}
280
+ )
281
+ end
282
+
283
+ it "updates and returns the resource" do
284
+ expect(put_update_response).to be_a(GoCardless::Resources::User)
285
+ expect(stub).to have_been_requested
286
+ end
287
+ end
288
+ end
289
+
290
+
291
+
292
+
293
+
294
+ describe "#enable" do
295
+
296
+
297
+ subject(:post_response) { client.users.enable(resource_id) }
298
+
299
+ let(:resource_id) { "ABC123" }
300
+
301
+ let!(:stub) do
302
+ # /users/%v/actions/enable
303
+ stub_url = "/users/:identity/actions/enable".gsub(':identity', resource_id)
304
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
305
+ body: {
306
+ users: {
307
+
308
+ "created_at" => "created_at-input",
309
+ "email" => "email-input",
310
+ "enabled" => "enabled-input",
311
+ "family_name" => "family_name-input",
312
+ "given_name" => "given_name-input",
313
+ "id" => "id-input",
314
+ "links" => "links-input",
315
+ }
316
+ }.to_json,
317
+ headers: {'Content-Type' => 'application/json'},
318
+ )
319
+ end
320
+
321
+ it "wraps the response and calls the right endpoint" do
322
+ expect(post_response).to be_a(GoCardless::Resources::User)
323
+
324
+ expect(stub).to have_been_requested
325
+ end
326
+
327
+ context "when the request needs a body and custom header" do
328
+
329
+ let(:body) { { foo: 'bar' } }
330
+ let(:headers) { { 'Foo' => 'Bar' } }
331
+ subject(:post_response) { client.users.enable(resource_id, body, headers) }
332
+
333
+ let(:resource_id) { "ABC123" }
334
+
335
+ let!(:stub) do
336
+ # /users/%v/actions/enable
337
+ stub_url = "/users/:identity/actions/enable".gsub(':identity', resource_id)
338
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
339
+ with(
340
+ body: { foo: 'bar' },
341
+ headers: { 'Foo' => 'Bar' }
342
+ ).to_return(
343
+ body: {
344
+ users: {
345
+
346
+ "created_at" => "created_at-input",
347
+ "email" => "email-input",
348
+ "enabled" => "enabled-input",
349
+ "family_name" => "family_name-input",
350
+ "given_name" => "given_name-input",
351
+ "id" => "id-input",
352
+ "links" => "links-input",
353
+ }
354
+ }.to_json,
355
+ headers: {'Content-Type' => 'application/json'},
356
+ )
357
+ end
358
+ end
359
+ end
360
+
361
+
362
+
363
+
364
+
365
+ describe "#disable" do
366
+
367
+
368
+ subject(:post_response) { client.users.disable(resource_id) }
369
+
370
+ let(:resource_id) { "ABC123" }
371
+
372
+ let!(:stub) do
373
+ # /users/%v/actions/disable
374
+ stub_url = "/users/:identity/actions/disable".gsub(':identity', resource_id)
375
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
376
+ body: {
377
+ users: {
378
+
379
+ "created_at" => "created_at-input",
380
+ "email" => "email-input",
381
+ "enabled" => "enabled-input",
382
+ "family_name" => "family_name-input",
383
+ "given_name" => "given_name-input",
384
+ "id" => "id-input",
385
+ "links" => "links-input",
386
+ }
387
+ }.to_json,
388
+ headers: {'Content-Type' => 'application/json'},
389
+ )
390
+ end
391
+
392
+ it "wraps the response and calls the right endpoint" do
393
+ expect(post_response).to be_a(GoCardless::Resources::User)
394
+
395
+ expect(stub).to have_been_requested
396
+ end
397
+
398
+ context "when the request needs a body and custom header" do
399
+
400
+ let(:body) { { foo: 'bar' } }
401
+ let(:headers) { { 'Foo' => 'Bar' } }
402
+ subject(:post_response) { client.users.disable(resource_id, body, headers) }
403
+
404
+ let(:resource_id) { "ABC123" }
405
+
406
+ let!(:stub) do
407
+ # /users/%v/actions/disable
408
+ stub_url = "/users/:identity/actions/disable".gsub(':identity', resource_id)
409
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
410
+ with(
411
+ body: { foo: 'bar' },
412
+ headers: { 'Foo' => 'Bar' }
413
+ ).to_return(
414
+ body: {
415
+ users: {
416
+
417
+ "created_at" => "created_at-input",
418
+ "email" => "email-input",
419
+ "enabled" => "enabled-input",
420
+ "family_name" => "family_name-input",
421
+ "given_name" => "given_name-input",
422
+ "id" => "id-input",
423
+ "links" => "links-input",
424
+ }
425
+ }.to_json,
426
+ headers: {'Content-Type' => 'application/json'},
427
+ )
428
+ end
429
+ end
430
+ end
431
+
432
+
433
+ end
@@ -0,0 +1,91 @@
1
+ require 'webmock/rspec'
2
+ require_relative '../lib/gocardless-pro'
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
54
+ # For more details, see:
55
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
+ config.disable_monkey_patching!
59
+
60
+ # This setting enables warnings. It's recommended, but in some cases may
61
+ # be too noisy due to issues in dependencies.
62
+ config.warnings = true
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ Kernel.srand config.seed
90
+ =end
91
+ end