Module: AfterbanksPSD2

Defined in:
lib/afterbanks_psd2/base.rb,
lib/afterbanks_psd2/error.rb,
lib/afterbanks_psd2/version.rb,
lib/afterbanks_psd2/resource.rb,
lib/afterbanks_psd2/response.rb,
lib/afterbanks_psd2/collection.rb,
lib/afterbanks_psd2/configuration.rb,
lib/afterbanks_psd2/resources/bank.rb,
lib/afterbanks_psd2/resources/user.rb,
lib/afterbanks_psd2/resources/account.rb,
lib/afterbanks_psd2/resources/transaction.rb

Defined Under Namespace

Classes: Account, AccountNotFoundError, Bank, Collection, Configuration, ConsentWithUnfinalizedProcessError, Error, ExpiredConsentError, GenericConsentError, GenericError, GenericTransactionError, IncorrectParametersError, InvalidConsentError, InvalidConsentForProductError, MaximumNumberOfCallsReachedConsentError, ProductMismatchConsentError, Resource, Response, Transaction, User

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.api_call(method:, path:, params: {}, options: {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/afterbanks_psd2/base.rb', line 14

def api_call(method:, path:, params: {}, options: {})
  options = {} if options.nil?

  api_url = ENV['AFTERBANKS_API_URL'] || 'https://apipsd2.afterbanks.com'
  url = api_url + path

  request_params = { method: method, url: url }

  if options[:timeout]
    request_params.merge!(timeout: options[:timeout])
  end

  if method == :post
    request_params.merge!(payload: params)
  else
    request_params.merge!(headers: { params: params })
  end

  response = begin
    RestClient::Request.execute(**request_params)
  rescue RestClient::BadRequest, RestClient::ExpectationFailed => bad_request
    # Check
    bad_request.response
  rescue RestClient::Exceptions::ReadTimeout
    log_request(
      method: method,
      url:    url,
      params: params
    )

    raise
  end

  debug_id = response.headers[:debug_id]

  log_request(
    method:   method,
    url:      url,
    params:   params,
    debug_id: debug_id
  )

  response_body = JSON.parse(response.body)

  treat_errors_if_any(
    response_body: response_body,
    debug_id:      debug_id
  )

  [response_body, debug_id]
end

.configurationObject



6
7
8
# File 'lib/afterbanks_psd2/base.rb', line 6

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



10
11
12
# File 'lib/afterbanks_psd2/base.rb', line 10

def configure
  yield(configuration)
end

.log_request(method:, url:, params: {}, debug_id: nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/afterbanks_psd2/base.rb', line 66

def log_request(method:, url:, params: {}, debug_id: nil)
  logger = AfterbanksPSD2.configuration.logger
  return if logger.nil?

  now = Time.now

  safe_params = {}
  params.each do |key, value|
    safe_value = if %w{servicekey}.include?(key.to_s)
                   "<masked>"
                 else
                   value
                 end

    safe_value = safe_value.to_s if safe_value.is_a?(Symbol)

    safe_params[key] = safe_value
  end

  logger.info(
    message:   'Afterbanks PSD2 request',
    method:    method.upcase.to_s,
    url:       url,
    time:      now.to_s,
    timestamp: now.to_i,
    debug_id:  debug_id || 'none',
    params:    safe_params
  )
end

.treat_errors_if_any(response_body:, debug_id:) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/afterbanks_psd2/base.rb', line 96

def treat_errors_if_any(response_body:, debug_id:)
  return unless response_body.is_a?(Hash)

  code = response_body['code']
  integer_code = code.to_i
  if integer_code.to_s == code
    code = integer_code
  end

  message = response_body['message']

  error_info = { message: message, debug_id: debug_id }

  case code
  when 1
    raise GenericError.new(**error_info)
  when 50
    raise IncorrectParametersError.new(**error_info)
  when 'C000'
    raise GenericConsentError.new(**error_info)
  when 'C001'
    raise InvalidConsentError.new(**error_info)
  when 'C002'
    raise ConsentWithUnfinalizedProcessError.new(**error_info)
  when 'C003'
    raise ProductMismatchConsentError.new(**error_info)
  when 'C004'
    raise ExpiredConsentError.new(**error_info)
  when 'C005'
    raise MaximumNumberOfCallsReachedConsentError.new(**error_info)
  when 'T000'
    raise GenericTransactionError.new(**error_info)
  when 'T001'
    raise InvalidConsentForProductError.new(**error_info)
  when 'P000'
    raise AccountNotFoundError.new(**error_info)
  end

  nil
end