bridge_bankin 0.1.0 → 0.1.4

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.
@@ -8,6 +8,9 @@ require "bridge_bankin/api/error"
8
8
 
9
9
  module BridgeBankin
10
10
  module API
11
+ #
12
+ # Allows to request the Bridge API using Ruby native net/http library
13
+ #
11
14
  class Client
12
15
  HTTP_VERBS_MAP = {
13
16
  get: Net::HTTP::Get,
@@ -18,18 +21,56 @@ module BridgeBankin
18
21
 
19
22
  attr_accessor :access_token
20
23
 
24
+ #
25
+ # Handles a GET request
26
+ #
27
+ # @param [String] path the API endpoint PATH to query
28
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
29
+ #
30
+ # @return [Hash] the parsed API response
31
+ #
32
+ # @raise [API::Error] expectation if API responding with any error
33
+ #
21
34
  def get(path, **params)
22
35
  request :get, path, params
23
36
  end
24
37
 
38
+ #
39
+ # Handles a POST request
40
+ #
41
+ # @param (see #get)
42
+ #
43
+ # @return (see #get)
44
+ #
45
+ # @raise (see #get)
46
+ #
47
+
25
48
  def post(path, **params)
26
49
  request :post, path, params
27
50
  end
28
51
 
52
+ #
53
+ # Handles a PUT request
54
+ #
55
+ # @param (see #get)
56
+ #
57
+ # @return (see #get)
58
+ #
59
+ # @raise (see #get)
60
+ #
29
61
  def put(path, **params)
30
62
  request :put, path, params
31
63
  end
32
64
 
65
+ #
66
+ # Handles a DELETE request
67
+ #
68
+ # @param (see #get)
69
+ #
70
+ # @return (see #get)
71
+ #
72
+ # @raise (see #get)
73
+ #
33
74
  def delete(path, **params)
34
75
  request :delete, path, params
35
76
  end
@@ -48,9 +89,14 @@ module BridgeBankin
48
89
  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
49
90
  api_response = http.request(yield)
50
91
 
51
- return parse_response_body(api_response.body) if %w[200 201].include?(api_response.code)
52
-
53
- handle_error(api_response)
92
+ case api_response.code
93
+ when "200", "201"
94
+ parse_response_body(api_response.body)
95
+ when "204", "202"
96
+ {}
97
+ else
98
+ handle_error(api_response)
99
+ end
54
100
  end
55
101
  end
56
102
 
@@ -2,7 +2,19 @@
2
2
 
3
3
  module BridgeBankin
4
4
  module API
5
+ #
6
+ # Error is the base error from which all other more specific BridgeBankin errors derive.
7
+ #
5
8
  class Error < StandardError
9
+ #
10
+ # Initializes Error
11
+ #
12
+ # @param [String] code the HTTP code returned by the API
13
+ # @param [Hash] response_body the parsed API response
14
+ # @option response_body [String] :type the machine readable error message
15
+ # @option response_body [String] :message the human readable error message
16
+ # @option response_body [String] :documentation_url the optional link to documentation
17
+ #
6
18
  def initialize(code, response_body = {})
7
19
  @payload = response_body
8
20
  @code = code
@@ -2,14 +2,17 @@
2
2
 
3
3
  module BridgeBankin
4
4
  module API
5
+ #
6
+ # Extend any resource class with API specific methods
7
+ #
5
8
  module Resource
9
+ private
10
+
6
11
  def protected_resource(access_token)
7
12
  api_client.access_token = access_token
8
13
  yield
9
14
  end
10
15
 
11
- private
12
-
13
16
  def api_client
14
17
  @api_client ||= API::Client.new
15
18
  end
@@ -3,9 +3,18 @@
3
3
  require "time"
4
4
 
5
5
  module BridgeBankin
6
+ #
7
+ # User authentication & authorization
8
+ #
6
9
  class Authorization
7
10
  attr_reader :access_token, :expires_at
8
11
 
12
+ #
13
+ # Initializes Authorization
14
+ #
15
+ # @param [String] access_token access token the access token provided by the API
16
+ # @param [Time] expires_at the expiration time for the provided access token
17
+ #
9
18
  def initialize(access_token, expires_at)
10
19
  @access_token = access_token
11
20
  @expires_at = Time.parse(expires_at)
@@ -14,6 +23,14 @@ module BridgeBankin
14
23
  class << self
15
24
  include API::Resource
16
25
 
26
+ #
27
+ # Authenticate user with the corresponding credentials
28
+ #
29
+ # @param [String] email the registered user email
30
+ # @param [String] password the registered user password
31
+ #
32
+ # @return [Authorization] the authorization informations provided by the API
33
+ #
17
34
  def generate_token(email:, password:)
18
35
  response = api_client.post("/v2/authenticate", email: email, password: password)
19
36
  new(response[:access_token], response[:expires_at])
@@ -1,20 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BridgeBankin
4
+ #
5
+ # Bank resource
6
+ #
4
7
  class Bank < BridgeObject
5
8
  RESOURCE_TYPE = "bank"
6
9
 
7
10
  class << self
8
11
  include API::Resource
9
12
 
13
+ #
14
+ # List all banks supported by the Bridge API
15
+ #
16
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
17
+ #
18
+ # @return [Array<Bank>] the supported banks list
19
+ #
10
20
  def list(**params)
11
- data = api_client.get("/v2/banks", params)
12
- convert_to_bridge_object(data)
21
+ data = api_client.get("/v2/banks", **params)
22
+ convert_to_bridge_object(**data)
13
23
  end
14
24
 
25
+ #
26
+ # Retrieve a single bank
27
+ #
28
+ # @param [Integer] id the id of the requested resource
29
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
30
+ #
31
+ # @return [Bank] the requested bank
32
+ #
15
33
  def find(id:, **params)
16
- data = api_client.get("/v2/banks/#{id}", params)
17
- convert_to_bridge_object(data)
34
+ data = api_client.get("/v2/banks/#{id}", **params)
35
+ convert_to_bridge_object(**data)
18
36
  end
19
37
  end
20
38
  end
@@ -1,15 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BridgeBankin
4
+ #
5
+ # BridgeObject is the base class from which all other more specific BridgeBankin resources derive.
6
+ #
4
7
  class BridgeObject
5
8
  HIDDEN_ATTRIBUTES = %i[resource_type resource_uri].freeze
6
9
 
10
+ #
11
+ # Initializes BridgeObject
12
+ #
13
+ # @param [Hash] attrs any informations returned by the API as a valid response
14
+ #
7
15
  def initialize(**attrs)
8
16
  define_instance_variables(attrs)
9
17
  end
10
18
 
11
19
  class << self
12
- def convert_to_bridge_object(**data)
20
+ #
21
+ # Convert any API response body with its corresponding resource object if exists
22
+ #
23
+ # @param [Hash] data parsed API response body
24
+ #
25
+ # @return [Account, Bank, Category, Item, Stock, Transaction, Transfer, User, BridgeObject] a resource object
26
+ #
27
+ def convert_to_bridge_object(data)
13
28
  if data[:resources]
14
29
  data[:resources].map { |resource| convert_to_bridge_object(resource) }
15
30
  elsif data.is_a?(Array)
@@ -26,7 +41,7 @@ module BridgeBankin
26
41
  end
27
42
 
28
43
  def object_from_resource_type(data)
29
- object_classes.fetch(data[:resource_type], BridgeObject).new(data)
44
+ object_classes.fetch(data[:resource_type], BridgeObject).new(**data)
30
45
  end
31
46
  end
32
47
 
@@ -1,20 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BridgeBankin
4
+ #
5
+ # Category resource
6
+ #
4
7
  class Category < BridgeObject
5
8
  RESOURCE_TYPE = "category"
6
9
 
7
10
  class << self
8
11
  include API::Resource
9
12
 
13
+ #
14
+ # List all categories supported by the Bridge API
15
+ #
16
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
17
+ #
18
+ # @return [Array<Category>] the supported categories list
19
+ #
10
20
  def list(**params)
11
- data = api_client.get("/v2/categories", params)
12
- convert_to_bridge_object(data)
21
+ data = api_client.get("/v2/categories", **params)
22
+ convert_to_bridge_object(**data)
13
23
  end
14
24
 
25
+ #
26
+ # Retrieve a single category
27
+ #
28
+ # @param [Integer] id the id of the requested resource
29
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
30
+ #
31
+ # @return [Category] the requested category
32
+ #
15
33
  def find(id:, **params)
16
- data = api_client.get("/v2/categories/#{id}", params)
17
- convert_to_bridge_object(data)
34
+ data = api_client.get("/v2/categories/#{id}", **params)
35
+ convert_to_bridge_object(**data)
18
36
  end
19
37
  end
20
38
  end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #
4
+ # BridgeBankin module
5
+ #
3
6
  module BridgeBankin
4
7
  def self.configuration
5
8
  @configuration ||= Configuration.new
@@ -13,10 +16,16 @@ module BridgeBankin
13
16
  yield configuration
14
17
  end
15
18
 
19
+ #
20
+ # Configurations setup
21
+ #
16
22
  class Configuration
17
23
  attr_reader :api_base_url, :api_version
18
24
  attr_accessor :api_client_id, :api_client_secret
19
25
 
26
+ #
27
+ # Initializes Configuration
28
+ #
20
29
  def initialize
21
30
  @api_base_url = "https://sync.bankin.com"
22
31
  @api_version = "2019-02-18"
@@ -1,49 +1,101 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BridgeBankin
4
+ #
5
+ # Connect resource
6
+ #
4
7
  class Connect < BridgeObject
5
8
  class << self
6
9
  include API::Resource
7
10
 
11
+ #
12
+ # Request URL to Bridge's Connect funnel for adding an item
13
+ #
14
+ # @param [String] access_token the access token provided during the user authentication
15
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
16
+ #
17
+ # @return [BridgeObject] URL to Bridge's Connect funnel for adding an item
18
+ #
8
19
  def connect_item(access_token:, **params)
9
20
  protected_resource(access_token) do
10
- data = api_client.get("/v2/connect/items/add/url", params)
11
- convert_to_bridge_object(data)
21
+ data = api_client.get("/v2/connect/items/add/url", **params)
22
+ convert_to_bridge_object(**data)
12
23
  end
13
24
  end
14
25
 
26
+ #
27
+ # Returns the URL to Bridge's Connect funnel for adding an item using IBAN
28
+ #
29
+ # @param [String] access_token the access token provided during the user authentication
30
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
31
+ #
32
+ # @return [BridgeObject] the URL to Bridge's Connect funnel for adding an item using IBAN
33
+ #
15
34
  def connect_item_with_iban(access_token:, **params)
16
35
  protected_resource(access_token) do
17
- data = api_client.post("/v2/connect/items/add/url", params)
18
- convert_to_bridge_object(data)
36
+ data = api_client.post("/v2/connect/items/add/url", **params)
37
+ convert_to_bridge_object(**data)
19
38
  end
20
39
  end
21
40
 
41
+ #
42
+ # Request the URL to Bridge's Connect funnel for editing an item
43
+ #
44
+ # @param [String] access_token the access token provided during the user authentication
45
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
46
+ #
47
+ # @return [BridgeObject] the URL to Bridge's Connect funnel for editing an item
48
+ #
22
49
  def edit_item(access_token:, **params)
23
50
  protected_resource(access_token) do
24
- data = api_client.get("/v2/connect/items/edit/url", params)
25
- convert_to_bridge_object(data)
51
+ data = api_client.get("/v2/connect/items/edit/url", **params)
52
+ convert_to_bridge_object(**data)
26
53
  end
27
54
  end
28
55
 
56
+ #
57
+ # Request the URL to Bridge's Connect funnel for managing
58
+ # a Strong Customer Authentication and synchronizing an item
59
+ #
60
+ # @param [String] access_token the access token provided during the user authentication
61
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
62
+ #
63
+ # @return [BridgeObject] the URL to Bridge's Connect funnel for managing
64
+ #
29
65
  def item_sync(access_token:, **params)
30
66
  protected_resource(access_token) do
31
- data = api_client.get("/v2/connect/items/sync", params)
32
- convert_to_bridge_object(data)
67
+ data = api_client.get("/v2/connect/items/sync", **params)
68
+ convert_to_bridge_object(**data)
33
69
  end
34
70
  end
35
71
 
72
+ #
73
+ # Request the URL to Bridge's Connect funnel for validating a user's email
74
+ #
75
+ # @param [String] access_token the access token provided during the user authentication
76
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
77
+ #
78
+ # @return [BridgeObject] the URL to Bridge's Connect funnel for validating a user's email
79
+ #
36
80
  def validate_email(access_token:, **params)
37
81
  protected_resource(access_token) do
38
- data = api_client.get("/v2/connect/users/email/confirmation/url", params)
39
- convert_to_bridge_object(data)
82
+ data = api_client.get("/v2/connect/users/email/confirmation/url", **params)
83
+ convert_to_bridge_object(**data)
40
84
  end
41
85
  end
42
86
 
87
+ #
88
+ # Request the URL to Bridge's Connect funnel for validating pro items
89
+ #
90
+ # @param [String] access_token the access token provided during the user authentication
91
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
92
+ #
93
+ # @return [BridgeObject] the URL to Bridge's Connect funnel for validating pro items
94
+ #
43
95
  def validate_pro_items(access_token:, **params)
44
96
  protected_resource(access_token) do
45
- data = api_client.get("/v2/connect/items/pro/confirmation/url", params)
46
- convert_to_bridge_object(data)
97
+ data = api_client.get("/v2/connect/items/pro/confirmation/url", **params)
98
+ convert_to_bridge_object(**data)
47
99
  end
48
100
  end
49
101
  end
@@ -1,14 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BridgeBankin
4
+ #
5
+ # Insight resource
6
+ #
4
7
  class Insight < BridgeObject
5
8
  class << self
6
9
  include API::Resource
7
10
 
11
+ #
12
+ # Categories statistics provided by Bridge
13
+ #
14
+ # @param [String] access_token the access token provided during the user authentication
15
+ # @param [Hash] params any params that might be required (or optional) to communicate with the API
16
+ #
17
+ # @return [Insight] the statistics generated by Bridge API
18
+ #
8
19
  def categories_insights(access_token:, **params)
9
20
  protected_resource(access_token) do
10
- data = api_client.get("/v2/insights/category", params)
11
- convert_to_bridge_object(data)
21
+ data = api_client.get("/v2/insights/category", **params)
22
+ convert_to_bridge_object(**data)
12
23
  end
13
24
  end
14
25
  end