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,57 @@
1
+ # encoding: utf-8
2
+ #
3
+ # WARNING: Do not edit by hand, this file was generated by Crank:
4
+ #
5
+ # https://github.com/gocardless/crank
6
+ #
7
+ require 'uri'
8
+ require 'base64'
9
+
10
+ module GoCardless
11
+ # GoCardless Enterprise API
12
+ class ApiService
13
+ # Initialize an APIService
14
+ #
15
+ # @param url [String] the URL to make requests to
16
+ # @param key [String] the API Key ID to use
17
+ # @param secret [String] the API key secret to use
18
+ # @param options [Hash] additional options to use when creating the service
19
+ def initialize(url, key, secret, options = {})
20
+ @url = url
21
+ root_url, @path_prefix = unpack_url(url)
22
+ http_adapter = options[:http_adapter] || [:net_http]
23
+ @connection = Faraday.new(url: root_url) do |faraday|
24
+ faraday.adapter(*http_adapter)
25
+ end
26
+
27
+ @headers = options[:default_headers] || {}
28
+ @headers['Authorization'] = 'Basic ' + Base64.strict_encode64("#{key}:#{secret}")
29
+ end
30
+
31
+ # Make a request to the API
32
+ #
33
+ # @param method [Symbol] the method to use to make the request
34
+ # @param path [String] the URL (without the base domain) to make the request to
35
+ # @param options [Hash] the options hash - either the query parameters for a GET, or the body if POST/PUT
36
+ # @param custom_headers [Hash] a hash of custom headers to use in the request
37
+ def make_request(method, path, options = {}, custom_headers = {})
38
+ fail ArgumentError, 'options must be a hash' unless options.is_a?(Hash)
39
+ Request.new(@connection, method, @path_prefix + path, options, @headers.merge(custom_headers)).request
40
+ end
41
+
42
+ # inspect the API Service
43
+ def inspect
44
+ url = URI.parse(@url)
45
+ url.password = 'REDACTED' unless url.password.nil?
46
+ "#<GoCardless::Client url=\"#{url}\">"
47
+ end
48
+ alias_method :to_s, :inspect
49
+
50
+ private
51
+
52
+ def unpack_url(url)
53
+ path = URI.parse(url).path
54
+ [URI.join(url).to_s, path == '/' ? '' : path]
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,42 @@
1
+ module GoCardless
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 GoCardless
2
+ # Thrown when the API returns a GoCardlessError
3
+ class GoCardlessError < Error
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module GoCardless
2
+ # Thrown when the API returns an invalid usage error
3
+ class InvalidApiUsageError < Error
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module GoCardless
2
+ # Thrown when the API returns an invalid state error
3
+ class InvalidStateError < Error
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module GoCardless
2
+ # Thrown when the API returns a validation error
3
+ class ValidationError < Error
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ module GoCardless
2
+ # Wraps a response from an API LIST endpoint
3
+ class ListResponse
4
+ include Enumerable
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
+ @items = @unenveloped_body.map { |item| @resource_class.new(item) }
17
+ end
18
+
19
+ # iterate over all the response items
20
+ def each(&block)
21
+ @items.each(&block)
22
+ end
23
+
24
+ # return the before cursor for paginating
25
+ def before
26
+ @raw_response.body[:meta][:cursors][:before]
27
+ end
28
+
29
+ # return the after cursor for paginating
30
+ def after
31
+ @raw_response.body[:meta][:cursors][:after]
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,37 @@
1
+ module GoCardless
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
+ @path = options.fetch(:path)
12
+ @options = options.fetch(:options)
13
+ end
14
+
15
+ # Get a lazy enumerable for listing data from the API
16
+ def enumerator
17
+ response = get_initial_response
18
+ Enumerator.new do |yielder|
19
+ loop do
20
+ items = @service.unenvelope_body(response.body)
21
+ items.each { |item| yielder << item }
22
+
23
+ after_cursor = response.meta['cursors']['after']
24
+ break if after_cursor.nil?
25
+
26
+ response = @service.make_request(:get, @path, @options.merge(after: after_cursor))
27
+ end
28
+ end.lazy
29
+ end
30
+
31
+ private
32
+
33
+ def get_initial_response
34
+ @initial_response ||= @service.make_request(:get, @path, @options)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,69 @@
1
+ module GoCardless
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, headers)
11
+ @connection = connection
12
+ @method = method
13
+ @path = path
14
+ @headers = 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
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
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,62 @@
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 GoCardless
12
+ # A module containing classes for each of the resources in the GC Api
13
+ module Resources
14
+ # <a name="api_key_not_active"></a>API keys are designed to be used by any
15
+ # integrations you build. You should generate a key and then use it to make
16
+ # requests to the API and set the webhook URL for that integration. They do
17
+ # not expire, but can be disabled.
18
+ # Represents an instance of a api_key resource returned from the API
19
+ class ApiKey
20
+ attr_reader :created_at
21
+
22
+ attr_reader :enabled
23
+
24
+ attr_reader :id
25
+
26
+ attr_reader :key
27
+
28
+ attr_reader :name
29
+
30
+ attr_reader :webhook_url
31
+ # initialize a resource instance
32
+ # @param object [Hash] an object returned from the API
33
+ def initialize(object)
34
+ @object = object
35
+
36
+ @created_at = object['created_at']
37
+ @enabled = object['enabled']
38
+ @id = object['id']
39
+ @key = object['key']
40
+ @links = object['links']
41
+ @name = object['name']
42
+ @webhook_url = object['webhook_url']
43
+ end
44
+
45
+ # return the links that the resource has
46
+ def links
47
+ Struct.new(
48
+ *{
49
+
50
+ role: ''
51
+
52
+ }.keys.sort
53
+ ).new(*@links.sort.map(&:last))
54
+ end
55
+
56
+ # Provides the resource as a hash of all it's readable attributes
57
+ def to_h
58
+ @object
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,83 @@
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 GoCardless
12
+ # A module containing classes for each of the resources in the GC Api
13
+ module Resources
14
+ # Each [payment](https://developer.gocardless.com/pro/#api-endpoints-payments)
15
+ # taken through the API is linked to a "creditor", to whom the payment is then
16
+ # paid out. In most cases your organisation will have a single "creditor", but
17
+ # the API also supports collecting payments on behalf of others.
18
+ #
19
+ #
20
+ # Please get in touch if you wish to use this endpoint. Currently, for Anti
21
+ # Money Laundering reasons, any creditors you add must be directly related to
22
+ # your organisation.
23
+ # Represents an instance of a creditor resource returned from the API
24
+ class Creditor
25
+ attr_reader :address_line1
26
+
27
+ attr_reader :address_line2
28
+
29
+ attr_reader :address_line3
30
+
31
+ attr_reader :city
32
+
33
+ attr_reader :country_code
34
+
35
+ attr_reader :created_at
36
+
37
+ attr_reader :id
38
+
39
+ attr_reader :name
40
+
41
+ attr_reader :postal_code
42
+
43
+ attr_reader :region
44
+ # initialize a resource instance
45
+ # @param object [Hash] an object returned from the API
46
+ def initialize(object)
47
+ @object = object
48
+
49
+ @address_line1 = object['address_line1']
50
+ @address_line2 = object['address_line2']
51
+ @address_line3 = object['address_line3']
52
+ @city = object['city']
53
+ @country_code = object['country_code']
54
+ @created_at = object['created_at']
55
+ @id = object['id']
56
+ @links = object['links']
57
+ @name = object['name']
58
+ @postal_code = object['postal_code']
59
+ @region = object['region']
60
+ end
61
+
62
+ # return the links that the resource has
63
+ def links
64
+ Struct.new(
65
+ *{
66
+
67
+ default_eur_payout_account: '',
68
+
69
+ default_gbp_payout_account: '',
70
+
71
+ logo: ''
72
+
73
+ }.keys.sort
74
+ ).new(*@links.sort.map(&:last))
75
+ end
76
+
77
+ # Provides the resource as a hash of all it's readable attributes
78
+ def to_h
79
+ @object
80
+ end
81
+ end
82
+ end
83
+ 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 GoCardless
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/#api-endpoints-creditor).
16
+ # These are the bank accounts which your
17
+ # [payouts](https://developer.gocardless.com/pro/#api-endpoints-payouts) will
18
+ # be sent to.
19
+ #
20
+ # Note that creditor bank accounts must be unique, and so
21
+ # you will encounter a `bank_account_exists` error if you try to create a
22
+ # duplicate bank account. You may wish to handle this by updating the existing
23
+ # 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