gocardless_pro 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) 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 +146 -0
  6. data/circle.yml +3 -0
  7. data/demo.rb +9 -0
  8. data/gocardless_pro.gemspec +26 -0
  9. data/lib/gocardless_pro.rb +73 -0
  10. data/lib/gocardless_pro/api_service.rb +58 -0
  11. data/lib/gocardless_pro/client.rb +135 -0
  12. data/lib/gocardless_pro/error.rb +42 -0
  13. data/lib/gocardless_pro/error/gocardless_error.rb +5 -0
  14. data/lib/gocardless_pro/error/invalid_api_usage_error.rb +5 -0
  15. data/lib/gocardless_pro/error/invalid_state_error.rb +5 -0
  16. data/lib/gocardless_pro/error/validation_error.rb +5 -0
  17. data/lib/gocardless_pro/list_response.rb +29 -0
  18. data/lib/gocardless_pro/paginator.rb +43 -0
  19. data/lib/gocardless_pro/request.rb +69 -0
  20. data/lib/gocardless_pro/resources/creditor.rb +84 -0
  21. data/lib/gocardless_pro/resources/creditor_bank_account.rb +78 -0
  22. data/lib/gocardless_pro/resources/customer.rb +75 -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 +87 -0
  28. data/lib/gocardless_pro/resources/payout.rb +66 -0
  29. data/lib/gocardless_pro/resources/redirect_flow.rb +106 -0
  30. data/lib/gocardless_pro/resources/refund.rb +71 -0
  31. data/lib/gocardless_pro/resources/subscription.rb +155 -0
  32. data/lib/gocardless_pro/response.rb +77 -0
  33. data/lib/gocardless_pro/services/base_service.rb +28 -0
  34. data/lib/gocardless_pro/services/creditor_bank_accounts_service.rb +119 -0
  35. data/lib/gocardless_pro/services/creditors_service.rb +113 -0
  36. data/lib/gocardless_pro/services/customer_bank_accounts_service.rb +154 -0
  37. data/lib/gocardless_pro/services/customers_service.rb +113 -0
  38. data/lib/gocardless_pro/services/events_service.rb +80 -0
  39. data/lib/gocardless_pro/services/helpers_service.rb +99 -0
  40. data/lib/gocardless_pro/services/mandates_service.rb +173 -0
  41. data/lib/gocardless_pro/services/payments_service.rb +168 -0
  42. data/lib/gocardless_pro/services/payouts_service.rb +82 -0
  43. data/lib/gocardless_pro/services/redirect_flows_service.rb +98 -0
  44. data/lib/gocardless_pro/services/refunds_service.rb +132 -0
  45. data/lib/gocardless_pro/services/subscriptions_service.rb +134 -0
  46. data/lib/gocardless_pro/version.rb +8 -0
  47. data/spec/api_service_spec.rb +73 -0
  48. data/spec/client_spec.rb +19 -0
  49. data/spec/error_spec.rb +44 -0
  50. data/spec/resources/creditor_bank_account_spec.rb +109 -0
  51. data/spec/resources/creditor_spec.rb +125 -0
  52. data/spec/resources/customer_bank_account_spec.rb +109 -0
  53. data/spec/resources/customer_spec.rb +135 -0
  54. data/spec/resources/event_spec.rb +113 -0
  55. data/spec/resources/helper_spec.rb +23 -0
  56. data/spec/resources/mandate_spec.rb +97 -0
  57. data/spec/resources/payment_spec.rb +129 -0
  58. data/spec/resources/payout_spec.rb +89 -0
  59. data/spec/resources/redirect_flow_spec.rb +97 -0
  60. data/spec/resources/refund_spec.rb +77 -0
  61. data/spec/resources/subscription_spec.rb +165 -0
  62. data/spec/response_spec.rb +89 -0
  63. data/spec/services/creditor_bank_accounts_service_spec.rb +413 -0
  64. data/spec/services/creditors_service_spec.rb +388 -0
  65. data/spec/services/customer_bank_accounts_service_spec.rb +452 -0
  66. data/spec/services/customers_service_spec.rb +429 -0
  67. data/spec/services/events_service_spec.rb +217 -0
  68. data/spec/services/helpers_service_spec.rb +122 -0
  69. data/spec/services/mandates_service_spec.rb +495 -0
  70. data/spec/services/payments_service_spec.rb +546 -0
  71. data/spec/services/payouts_service_spec.rb +217 -0
  72. data/spec/services/redirect_flows_service_spec.rb +254 -0
  73. data/spec/services/refunds_service_spec.rb +323 -0
  74. data/spec/services/subscriptions_service_spec.rb +557 -0
  75. data/spec/spec_helper.rb +91 -0
  76. metadata +224 -0
@@ -0,0 +1,42 @@
1
+ module GoCardlessPro
2
+ # A class to represent an API Error
3
+ class Error < StandardError
4
+ attr_reader :error
5
+
6
+ # intialize a new error
7
+ #  @param error the error from the API
8
+ def initialize(error)
9
+ @error = error
10
+ end
11
+
12
+ # access the documentation_url from the response
13
+ def documentation_url
14
+ @error['documentation_url']
15
+ end
16
+
17
+ # access the message from the response
18
+ def message
19
+ @error['message']
20
+ end
21
+
22
+ # access the type from the response
23
+ def type
24
+ @error['type']
25
+ end
26
+
27
+ # access the code from the response
28
+ def code
29
+ @error['code']
30
+ end
31
+
32
+ # access the request_id from the response
33
+ def request_id
34
+ @error['request_id']
35
+ end
36
+
37
+ # access the errors from the response
38
+ def errors
39
+ @error['errors']
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module GoCardlessPro
2
+ # Thrown when the API returns a GoCardlessError
3
+ class GoCardlessError < Error
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module GoCardlessPro
2
+ # Thrown when the API returns an invalid usage error
3
+ class InvalidApiUsageError < Error
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module GoCardlessPro
2
+ # Thrown when the API returns an invalid state error
3
+ class InvalidStateError < Error
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module GoCardlessPro
2
+ # Thrown when the API returns a validation error
3
+ class ValidationError < Error
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ module GoCardlessPro
2
+ # Wraps a response from an API LIST endpoint
3
+ class ListResponse
4
+ attr_reader :records
5
+
6
+ # Initialize a list response
7
+ # @param options [Hash]
8
+ # @option option :raw_response the raw API response
9
+ # @option option :resource_class the class for the resource returned by the API
10
+ # @option option :unenveloped_body the parsed response from the API
11
+ def initialize(options = {})
12
+ @raw_response = options.fetch(:raw_response)
13
+ @resource_class = options.fetch(:resource_class)
14
+ @unenveloped_body = options.fetch(:unenveloped_body)
15
+
16
+ @records = @unenveloped_body.map { |item| @resource_class.new(item) }
17
+ end
18
+
19
+ # return the before cursor for paginating
20
+ def before
21
+ @raw_response.body['meta']['cursors']['before']
22
+ end
23
+
24
+ # return the after cursor for paginating
25
+ def after
26
+ @raw_response.body['meta']['cursors']['after']
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,43 @@
1
+ module GoCardlessPro
2
+ # A class that can take an API LIST query and auto paginate through results
3
+ class Paginator
4
+ # initialize a paginator
5
+ # @param options [Hash]
6
+ # @option options :service the service class to use to make requests to
7
+ # @option options :path the path to make the request to
8
+ # @option options :options additional options to send with the requests
9
+ def initialize(options = {})
10
+ @service = options.fetch(:service)
11
+ @resource_class = options.fetch(:resource_class)
12
+ @path = options.fetch(:path)
13
+ @options = options.fetch(:options)
14
+ end
15
+
16
+ # Get a lazy enumerable for listing data from the API
17
+ def enumerator
18
+ response = get_initial_response
19
+ Enumerator.new do |yielder|
20
+ loop do
21
+ items = @service.unenvelope_body(response.body).map do |item|
22
+ @resource_class.new(item)
23
+ end
24
+
25
+ items.each { |item| yielder << item }
26
+
27
+ after_cursor = response.meta['cursors']['after']
28
+ break if after_cursor.nil?
29
+
30
+ @options[:params] ||= {}
31
+ @options[:params] = @options[:params].merge(after: after_cursor)
32
+ response = @service.make_request(:get, @path, @options.merge(after: after_cursor))
33
+ end
34
+ end.lazy
35
+ end
36
+
37
+ private
38
+
39
+ def get_initial_response
40
+ @initial_response ||= @service.make_request(:get, @path, @options)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,69 @@
1
+ module GoCardlessPro
2
+ # A class that wraps an API request
3
+ class Request
4
+ # Initialize a request class, which makes calls to the API
5
+ # @param connection
6
+ # @param method [Symbol] the method to make the request with
7
+ # @param path [String] the path to make the request to
8
+ # @param options [hash] options for the request
9
+ # @param headers [hash] headers to send with the request
10
+ def initialize(connection, method, path, options)
11
+ @connection = connection
12
+ @method = method
13
+ @path = path
14
+ @headers = options.delete(:headers) || {}
15
+ @envelope_name = options.delete(:envelope_key)
16
+ @given_options = options
17
+
18
+ @request_body = request_body
19
+
20
+ if @request_body.is_a?(Hash)
21
+ @request_body = @request_body.to_json
22
+ @headers['Content-Type'] ||= 'application/json'
23
+ end
24
+ end
25
+
26
+ # Make the request and wrap it in a Response object
27
+ def request
28
+ Response.new(make_request)
29
+ end
30
+
31
+ # Make the API request
32
+ def make_request
33
+ @connection.send(@method) do |request|
34
+ request.url @path
35
+ request.body = @request_body
36
+ request.params = request_query
37
+ request.headers.merge!(@headers)
38
+ end
39
+ end
40
+
41
+ # Fetch the body to send with the request
42
+ def request_body
43
+ if @method == :get
44
+ nil
45
+ elsif @method == :post || @method == :put
46
+ @given_options.fetch(:params, {})
47
+ else
48
+ fail "unknown method #{@method}"
49
+ end
50
+ end
51
+
52
+ # Get the query params to send with the request
53
+ def request_query
54
+ if @method == :get
55
+ @given_options.fetch(:params, {})
56
+ elsif @method == :post || @method == :put
57
+ {}
58
+ else
59
+ fail "unknown method #{@method}"
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def options
66
+ { headers: @headers, body: @body, query: @query }
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,84 @@
1
+
2
+
3
+ # encoding: utf-8
4
+ #
5
+ # WARNING: Do not edit by hand, this file was generated by Crank:
6
+ #
7
+ # https://github.com/gocardless/crank
8
+ #
9
+ require 'uri'
10
+
11
+ module GoCardlessPro
12
+ # A module containing classes for each of the resources in the GC Api
13
+ module Resources
14
+ # Each
15
+ # [payment](https://developer.gocardless.com/pro/2015-04-29/#api-endpoints-payments)
16
+ # taken through the API is linked to a "creditor", to whom the payment is then
17
+ # paid out. In most cases your organisation will have a single "creditor", but
18
+ # the API also supports collecting payments on behalf of others.
19
+ #
20
+ #
21
+ # Please get in touch if you wish to use this endpoint. Currently, for Anti
22
+ # Money Laundering reasons, any creditors you add must be directly related to
23
+ # your organisation.
24
+ # Represents an instance of a creditor resource returned from the API
25
+ class Creditor
26
+ attr_reader :address_line1
27
+
28
+ attr_reader :address_line2
29
+
30
+ attr_reader :address_line3
31
+
32
+ attr_reader :city
33
+
34
+ attr_reader :country_code
35
+
36
+ attr_reader :created_at
37
+
38
+ attr_reader :id
39
+
40
+ attr_reader :name
41
+
42
+ attr_reader :postal_code
43
+
44
+ attr_reader :region
45
+ # initialize a resource instance
46
+ # @param object [Hash] an object returned from the API
47
+ def initialize(object)
48
+ @object = object
49
+
50
+ @address_line1 = object['address_line1']
51
+ @address_line2 = object['address_line2']
52
+ @address_line3 = object['address_line3']
53
+ @city = object['city']
54
+ @country_code = object['country_code']
55
+ @created_at = object['created_at']
56
+ @id = object['id']
57
+ @links = object['links']
58
+ @name = object['name']
59
+ @postal_code = object['postal_code']
60
+ @region = object['region']
61
+ end
62
+
63
+ # return the links that the resource has
64
+ def links
65
+ Struct.new(
66
+ *{
67
+
68
+ default_eur_payout_account: '',
69
+
70
+ default_gbp_payout_account: '',
71
+
72
+ logo: ''
73
+
74
+ }.keys.sort
75
+ ).new(*@links.sort.map(&:last))
76
+ end
77
+
78
+ # Provides the resource as a hash of all it's readable attributes
79
+ def to_h
80
+ @object
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,78 @@
1
+
2
+
3
+ # encoding: utf-8
4
+ #
5
+ # WARNING: Do not edit by hand, this file was generated by Crank:
6
+ #
7
+ # https://github.com/gocardless/crank
8
+ #
9
+ require 'uri'
10
+
11
+ module GoCardlessPro
12
+ # A module containing classes for each of the resources in the GC Api
13
+ module Resources
14
+ # Creditor Bank Accounts hold the bank details of a
15
+ # [creditor](https://developer.gocardless.com/pro/2015-04-29/#api-endpoints-creditor).
16
+ # These are the bank accounts which your
17
+ # [payouts](https://developer.gocardless.com/pro/2015-04-29/#api-endpoints-payouts)
18
+ # will be sent to.
19
+ #
20
+ # Note that creditor bank accounts must be unique,
21
+ # and so you will encounter a `bank_account_exists` error if you try to create
22
+ # a duplicate bank account. You may wish to handle this by updating the
23
+ # existing record instead, the ID of which will be provided as
24
+ # `links[creditor_bank_account]` in the error response.
25
+ # Represents an instance of a creditor_bank_account resource returned from the API
26
+ class CreditorBankAccount
27
+ attr_reader :account_holder_name
28
+
29
+ attr_reader :account_number_ending
30
+
31
+ attr_reader :bank_name
32
+
33
+ attr_reader :country_code
34
+
35
+ attr_reader :created_at
36
+
37
+ attr_reader :currency
38
+
39
+ attr_reader :enabled
40
+
41
+ attr_reader :id
42
+
43
+ attr_reader :metadata
44
+ # initialize a resource instance
45
+ # @param object [Hash] an object returned from the API
46
+ def initialize(object)
47
+ @object = object
48
+
49
+ @account_holder_name = object['account_holder_name']
50
+ @account_number_ending = object['account_number_ending']
51
+ @bank_name = object['bank_name']
52
+ @country_code = object['country_code']
53
+ @created_at = object['created_at']
54
+ @currency = object['currency']
55
+ @enabled = object['enabled']
56
+ @id = object['id']
57
+ @links = object['links']
58
+ @metadata = object['metadata']
59
+ end
60
+
61
+ # return the links that the resource has
62
+ def links
63
+ Struct.new(
64
+ *{
65
+
66
+ creditor: ''
67
+
68
+ }.keys.sort
69
+ ).new(*@links.sort.map(&:last))
70
+ end
71
+
72
+ # Provides the resource as a hash of all it's readable attributes
73
+ def to_h
74
+ @object
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,75 @@
1
+
2
+
3
+ # encoding: utf-8
4
+ #
5
+ # WARNING: Do not edit by hand, this file was generated by Crank:
6
+ #
7
+ # https://github.com/gocardless/crank
8
+ #
9
+ require 'uri'
10
+
11
+ module GoCardlessPro
12
+ # A module containing classes for each of the resources in the GC Api
13
+ module Resources
14
+ # Customer objects hold the contact details for a customer. A customer can
15
+ # have several [customer bank
16
+ # accounts](https://developer.gocardless.com/pro/2015-04-29/#api-endpoints-customer-bank-accounts),
17
+ # which in turn can have several Direct Debit
18
+ # [mandates](https://developer.gocardless.com/pro/2015-04-29/#api-endpoints-mandates).
19
+ # Represents an instance of a customer resource returned from the API
20
+ class Customer
21
+ attr_reader :address_line1
22
+
23
+ attr_reader :address_line2
24
+
25
+ attr_reader :address_line3
26
+
27
+ attr_reader :city
28
+
29
+ attr_reader :company_name
30
+
31
+ attr_reader :country_code
32
+
33
+ attr_reader :created_at
34
+
35
+ attr_reader :email
36
+
37
+ attr_reader :family_name
38
+
39
+ attr_reader :given_name
40
+
41
+ attr_reader :id
42
+
43
+ attr_reader :metadata
44
+
45
+ attr_reader :postal_code
46
+
47
+ attr_reader :region
48
+ # initialize a resource instance
49
+ # @param object [Hash] an object returned from the API
50
+ def initialize(object)
51
+ @object = object
52
+
53
+ @address_line1 = object['address_line1']
54
+ @address_line2 = object['address_line2']
55
+ @address_line3 = object['address_line3']
56
+ @city = object['city']
57
+ @company_name = object['company_name']
58
+ @country_code = object['country_code']
59
+ @created_at = object['created_at']
60
+ @email = object['email']
61
+ @family_name = object['family_name']
62
+ @given_name = object['given_name']
63
+ @id = object['id']
64
+ @metadata = object['metadata']
65
+ @postal_code = object['postal_code']
66
+ @region = object['region']
67
+ end
68
+
69
+ # Provides the resource as a hash of all it's readable attributes
70
+ def to_h
71
+ @object
72
+ end
73
+ end
74
+ end
75
+ end