Skip to content

Commit

Permalink
Code style changes to conform to ruby code conventions.
Browse files Browse the repository at this point in the history
This includes:

- removing uneccessary return statements
- removing usage of static class variables
- adding .freeze to class constants
- using the faraday authorisation plugin
- identation
- using attr_accessor instead of getter and setter methods.
  • Loading branch information
mnbbrown committed Jun 2, 2023
1 parent 68d49dd commit e3ca5a4
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 318 deletions.
198 changes: 90 additions & 108 deletions lib/nordigen-ruby.rb
Original file line number Diff line number Diff line change
@@ -1,119 +1,101 @@
require "faraday"
require 'faraday'

require_relative "nordigen_ruby/api/institutions"
require_relative "nordigen_ruby/api/agreements"
require_relative "nordigen_ruby/api/requisitions"
require_relative "nordigen_ruby/api/account"
require_relative 'nordigen_ruby/api/institutions'
require_relative 'nordigen_ruby/api/agreements'
require_relative 'nordigen_ruby/api/requisitions'
require_relative 'nordigen_ruby/api/account'

module Nordigen
class NordigenClient

BASE_URL = "https://ob.nordigen.com/api/v2/"
class NordigenClient
BASE_URL = 'https://ob.nordigen.com/api/v2/'

attr_reader :secret_id, :secret_key, :institution, :agreement, :requisition
attr_accessor :access_token

def initialize(secret_id:, secret_key:)
@secret_id = secret_id
@secret_key = secret_key
@institution = InstitutionsApi.new(self)
@agreement = AgreementsApi.new(self)
@requisition = RequisitionsApi.new(self)
@access_token = nil
end

@@headers = {
"accept" => "application/json",
"Content-Type" => "application/json",
"User-Agent" => "Nordigen-Ruby-v2"
def request
# HTTP client request
@request ||= Faraday.new do |conn|
conn.url_prefix = BASE_URL
conn.headers = {
'accept' => 'application/json',
'Content-Type' => 'application/json',
'User-Agent' => 'Nordigen-Ruby-v2'
}
conn.request :authorization, 'Bearer', access_token
conn.request :json
conn.response :json
end
end

attr_reader :secret_id, :secret_key, :institution, :agreement, :requisition

def initialize(secret_id:, secret_key:)
@secret_id = secret_id
@secret_key = secret_key
@institution = InstitutionsApi.new(client=self)
@agreement = AgreementsApi.new(client=self)
@requisition = RequisitionsApi.new(client=self)
end

def request
# HTTP client request
@request ||= Faraday.new do |conn|
conn.url_prefix = BASE_URL
conn.headers = @@headers
conn.request :json
conn.response :json
end
end

def set_token(access_token)
# Use existing token
@@headers["Authorization"] = "Bearer #{access_token}"
end

def get_token
# Get token
return request.headers["Authorization"]
end

def generate_token
# Generate new access & refresh token
payload = {
"secret_key": @secret_key,
"secret_id": @secret_id
}
response = self.request.post("token/new/", payload)
if !response.success?
raise Exception.new response.body
end

@@headers["Authorization"] = "Bearer #{response.body['access']}"
request.headers = @@headers
return response.body
end

def exchange_token(refresh_token)
# Exchange refresh token for access token
payload = {"refresh": refresh_token}
response = self.request.post("token/refresh/", payload).body
@@headers["Authorization"] = "Bearer #{response['access']}"
request.headers = @@headers
return response
end


def account(account_id)
# Create Account instance
return AccountApi.new(client: self, account_id: account_id)
end

def init_session(
redirect_url:,
institution_id:,
reference_id:,
max_historical_days: 90,
access_valid_for_days: 90,
user_language: "en",
account_selection: false,
redirect_immediate: false,
ssn: nil
)
# Factory method that creates authorization in a specific institution
# and are responsible for the following steps:
# * Creates agreement
# * Creates requisiton

# Create agreement
new_agreement = @agreement.create_agreement(
institution_id: institution_id,
max_historical_days: max_historical_days,
access_valid_for_days: access_valid_for_days
)
def generate_token
# Generate new access & refresh token
payload = {
"secret_key": @secret_key,
"secret_id": @secret_id
}
response = request.post('token/new/', payload)
raise StandardError, response.body unless response.success?

@access_token = response.body['access']
response.body
end

# Create requisition
new_requsition = @requisition.create_requisition(
redirect_url: redirect_url,
reference: reference_id,
institution_id: institution_id,
user_language: user_language,
account_selection: account_selection,
redirect_immediate: redirect_immediate,
agreement: new_agreement["id"],
ssn: ssn
)
def exchange_token(refresh_token)
# Exchange refresh token for access token
payload = { "refresh": refresh_token }
response = request.post('token/refresh/', payload).body
@access_token = response['access']
response
end

return new_requsition
end
def account(account_id)
# Create Account instance
AccountApi.new(client: self, account_id: account_id)
end

def init_session(
redirect_url:,
institution_id:,
reference_id:,
max_historical_days: 90,
access_valid_for_days: 90,
user_language: 'en',
account_selection: false,
redirect_immediate: false,
ssn: nil
)
# Factory method that creates authorization in a specific institution
# and are responsible for the following steps:
# * Creates agreement
# * Creates requisiton

# Create agreement
new_agreement = @agreement.create_agreement(
institution_id: institution_id,
max_historical_days: max_historical_days,
access_valid_for_days: access_valid_for_days
)

# Create requisition
@requisition.create_requisition(
redirect_url: redirect_url,
reference: reference_id,
institution_id: institution_id,
user_language: user_language,
account_selection: account_selection,
redirect_immediate: redirect_immediate,
agreement: new_agreement['id'],
ssn: ssn
)
end
end
end
97 changes: 46 additions & 51 deletions lib/nordigen_ruby/api/account.rb
Original file line number Diff line number Diff line change
@@ -1,64 +1,59 @@
module Nordigen
class AccountApi

ENDPOINT = "accounts/"
PREMIUM_ENDPOINT = "accounts/premium/"
attr_reader :client, :account_id

def initialize(client:, account_id:)
@client = client
@account_id = account_id
end
class AccountApi
ENDPOINT = 'accounts/'
PREMIUM_ENDPOINT = 'accounts/premium/'
attr_reader :client, :account_id

def initialize(client:, account_id:)
@client = client
@account_id = account_id
end

def get(path = nil, params = nil, premium: nil)
# Create Get request
if premium
url = "#{PREMIUM_ENDPOINT}#{@account_id}/"
def get(path = nil, params = nil, premium: nil)
# Create Get request
url = if premium
"#{PREMIUM_ENDPOINT}#{@account_id}/"
else
url = "#{ENDPOINT}#{@account_id}/"
"#{ENDPOINT}#{@account_id}/"
end

if path
url = "#{url}#{path}/"
end

return client.request.get(url, params).body
end

def get_metadata
# Access account metadata
return get()
end
url = "#{url}#{path}/" if path

def get_details
# Access account details
return get("details")
end
client.request.get(url, params).body
end

def get_metadata
# Access account metadata
get
end

def get_balances
# Access account balances
return get("balances")
end
def get_details
# Access account details
get('details')
end

def get_transactions(date_from: nil, date_to: nil)
# Access account transactions
date_range = {
"date_from" => date_from,
"date_to" => date_to
}
return get("transactions", date_range)
end
def get_balances
# Access account balances
get('balances')
end

def get_premium_transactions(date_from: nil, date_to: nil, country: nil)
# Access account transactions
params = {
"date_from" => date_from,
"date_to" => date_to,
"country" => country
}
return get("transactions", params, premium: true )
end
def get_transactions(date_from: nil, date_to: nil)
# Access account transactions
date_range = {
'date_from' => date_from,
'date_to' => date_to
}
get('transactions', date_range)
end

def get_premium_transactions(date_from: nil, date_to: nil, country: nil)
# Access account transactions
params = {
'date_from' => date_from,
'date_to' => date_to,
'country' => country
}
get('transactions', params, premium: true)
end
end
end
Loading

0 comments on commit e3ca5a4

Please sign in to comment.