Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
inherit_gem:
gc_ruboconfig: rubocop.yml

AllCops:
Exclude:
- 'example/**/*'
- 'main.rb'
- 'nordigen-ruby.gemspec'
11 changes: 6 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

source "https://rubygems.org"
source 'https://rubygems.org'

# Specify your gem's dependencies in nordigen.gemspec
gemspec

gem "faraday", "~> 2.5"
gem 'faraday', '~> 2.5'

group :development do
gem "rake", "~> 13.0"
gem "test-unit", "~> 3.5.3"
gem "dotenv", "~> 2.8.1"
gem 'dotenv', '~> 2.8.1'
gem 'gc_ruboconfig'
gem 'rake', '~> 13.0'
gem 'test-unit', '~> 3.5.3'
end
191 changes: 88 additions & 103 deletions lib/nordigen-ruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "faraday"

require_relative "nordigen_ruby/api/institutions"
Expand All @@ -6,114 +8,97 @@
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 :logger, nil, { headers: true, bodies: true, errors: true }
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
99 changes: 48 additions & 51 deletions lib/nordigen_ruby/api/account.rb
Original file line number Diff line number Diff line change
@@ -1,64 +1,61 @@
module Nordigen
class AccountApi

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

def initialize(client:, account_id:)
@client = client
@account_id = account_id
end
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

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}/"
end

if path
url = "#{url}#{path}/"
"#{ENDPOINT}#{@account_id}/"
end

return client.request.get(url, params).body
end
url = "#{url}#{path}/" if path

def get_metadata
# Access account metadata
return get()
end

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