Skip to content
Merged
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
28 changes: 28 additions & 0 deletions lib/fintoc/v2/managers/account_statements_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'fintoc/v2/resources/account_statement'

module Fintoc
module V2
module Managers
class AccountStatementsManager
def initialize(client, account_id)
@client = client
@account_id = account_id
end

def list(**params)
_list_account_statements(**params).map { |data| build_account_statement(data) }
end

private

def _list_account_statements(**params)
@client.get(version: :v2).call("accounts/#{@account_id}/account_statements", **params)
end

def build_account_statement(data)
Fintoc::V2::AccountStatement.new(**data)
end
end
end
end
end
38 changes: 38 additions & 0 deletions lib/fintoc/v2/managers/movements_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'fintoc/v2/resources/movement'

module Fintoc
module V2
module Managers
class MovementsManager
def initialize(client, account_id)
@client = client
@account_id = account_id
end

def list(**params)
_list_movements(**params).map { |data| build_movement(data) }
end

def get(movement_id, **params)
data = _get_movement(movement_id, **params)
build_movement(data)
end

private

def _list_movements(**params)
@client.get(version: :v2).call("accounts/#{@account_id}/movements", **params)
end

def _get_movement(movement_id, **params)
path = "accounts/#{@account_id}/movements/#{movement_id}"
@client.get(version: :v2).call(path, **params)
end

def build_movement(data)
Fintoc::V2::Movement.new(**data)
end
end
end
end
end
10 changes: 10 additions & 0 deletions lib/fintoc/v2/resources/account.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'money'
require 'fintoc/v2/managers/account_statements_manager'
require 'fintoc/v2/managers/movements_manager'

module Fintoc
module V2
Expand Down Expand Up @@ -81,6 +83,14 @@ def simulate_receive_transfer(amount:, idempotency_key: nil)
)
end

def account_statements
@account_statements ||= Managers::AccountStatementsManager.new(@client, @id)
end

def movements
@movements ||= Managers::MovementsManager.new(@client, @id)
end

private

def refresh_from_account(account)
Expand Down
48 changes: 48 additions & 0 deletions lib/fintoc/v2/resources/account_statement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Fintoc
module V2
class AccountStatement
attr_reader :id, :object, :start_date, :end_date, :total_debited_cents,
:total_credited_cents, :initial_balance_cents, :final_balance_cents,
:download_url, :created_at

def initialize(
id:,
object:,
start_date:,
end_date:,
total_debited_cents:,
total_credited_cents:,
initial_balance_cents:,
final_balance_cents:,
download_url:,
created_at:,
**
)
@id = id
@object = object
@start_date = start_date
@end_date = end_date
@total_debited_cents = total_debited_cents
@total_credited_cents = total_credited_cents
@initial_balance_cents = initial_balance_cents
@final_balance_cents = final_balance_cents
@download_url = download_url
@created_at = created_at
end

def ==(other)
@id == other.id
end

alias eql? ==

def hash
@id.hash
end

def to_s
"AccountStatement(#{@id}) #{@start_date} - #{@end_date}"
end
end
end
end
52 changes: 52 additions & 0 deletions lib/fintoc/v2/resources/movement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Fintoc
module V2
class Movement
attr_reader :id, :object, :type, :direction, :resource_id, :mode,
:amount, :currency, :transaction_date, :return_pair_id,
:balance, :account_id

def initialize(
id:,
object:,
type:,
direction:,
mode:,
amount:,
currency:,
transaction_date:,
balance:,
account_id:,
resource_id: nil,
return_pair_id: nil,
**
)
@id = id
@object = object
@type = type
@direction = direction
@resource_id = resource_id
@mode = mode
@amount = amount
@currency = currency
@transaction_date = transaction_date
@return_pair_id = return_pair_id
@balance = balance
@account_id = account_id
end

def ==(other)
@id == other.id
end

alias eql? ==

def hash
@id.hash
end

def to_s
"#{@direction} #{@amount} #{@currency} (#{@type})"
end
end
end
end
24 changes: 24 additions & 0 deletions spec/lib/fintoc/v2/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,30 @@
end
end

describe '#account_statements' do
it 'returns an AccountStatementsManager instance' do
expect(account.account_statements).to be_an_instance_of(Fintoc::V2::Managers::AccountStatementsManager)
end

it 'memoizes the manager instance' do
first_call = account.account_statements
second_call = account.account_statements
expect(first_call).to be(second_call)
end
end

describe '#movements' do
it 'returns a MovementsManager instance' do
expect(account.movements).to be_an_instance_of(Fintoc::V2::Managers::MovementsManager)
end

it 'memoizes the manager instance' do
first_call = account.movements
second_call = account.movements
expect(first_call).to be(second_call)
end
end

describe '#simulate_receive_transfer' do
let(:expected_transfer) { instance_double(Fintoc::V2::Transfer) }

Expand Down
66 changes: 66 additions & 0 deletions spec/lib/fintoc/v2/account_statement_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'fintoc/v2/resources/account_statement'

RSpec.describe Fintoc::V2::AccountStatement do
let(:data) do
{
id: 'acst_12345',
object: 'account_statement',
start_date: '2026-01-01',
end_date: '2026-01-31',
total_debited_cents: 500000,
total_credited_cents: 750000,
initial_balance_cents: 1000000,
final_balance_cents: 1250000,
download_url: 'https://api.fintoc.com/v2/account_statements/acst_12345/download',
created_at: '2026-02-01T00:00:00Z'
}
end

let(:account_statement) { described_class.new(**data) }

describe '#new' do
it 'creates an instance of AccountStatement' do
expect(account_statement).to be_an_instance_of(described_class)
end

it 'sets all attributes correctly' do # rubocop:disable RSpec/ExampleLength
expect(account_statement).to have_attributes(
id: 'acst_12345',
object: 'account_statement',
start_date: '2026-01-01',
end_date: '2026-01-31',
total_debited_cents: 500000,
total_credited_cents: 750000,
initial_balance_cents: 1000000,
final_balance_cents: 1250000,
download_url: 'https://api.fintoc.com/v2/account_statements/acst_12345/download',
created_at: '2026-02-01T00:00:00Z'
)
end
end

describe '#to_s' do
it 'returns a formatted string representation' do
expect(account_statement.to_s).to eq('AccountStatement(acst_12345) 2026-01-01 - 2026-01-31')
end
end

describe '#==' do
it 'returns true when comparing statements with the same id' do
other = described_class.new(**data)
expect(account_statement).to eq(other)
end

it 'returns false when comparing statements with different ids' do
other = described_class.new(**data, id: 'acst_67890')
expect(account_statement).not_to eq(other)
end
end

describe '#hash' do
it 'returns the same hash for statements with the same id' do
other = described_class.new(**data)
expect(account_statement.hash).to eq(other.hash)
end
end
end
57 changes: 57 additions & 0 deletions spec/lib/fintoc/v2/managers/account_statements_manager_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'fintoc/v2/managers/account_statements_manager'

RSpec.describe Fintoc::V2::Managers::AccountStatementsManager do
let(:client) { instance_double(Fintoc::BaseClient) }
let(:get_proc) { instance_double(Proc) }
let(:account_id) { 'acc_12345' }
let(:manager) { described_class.new(client, account_id) }
let(:first_statement_data) do
{
id: 'acst_12345',
object: 'account_statement',
start_date: '2026-01-01',
end_date: '2026-01-31',
total_debited_cents: 500000,
total_credited_cents: 750000,
initial_balance_cents: 1000000,
final_balance_cents: 1250000,
download_url: 'https://api.fintoc.com/v2/account_statements/acst_12345/download',
created_at: '2026-02-01T00:00:00Z'
}
end
let(:second_statement_data) do
{
id: 'acst_67890',
object: 'account_statement',
start_date: '2026-02-01',
end_date: '2026-02-28',
total_debited_cents: 300000,
total_credited_cents: 400000,
initial_balance_cents: 1250000,
final_balance_cents: 1350000,
download_url: 'https://api.fintoc.com/v2/account_statements/acst_67890/download',
created_at: '2026-03-01T00:00:00Z'
}
end

before do
allow(client).to receive(:get).with(version: :v2).and_return(get_proc)

allow(get_proc)
.to receive(:call)
.with("accounts/#{account_id}/account_statements")
.and_return([first_statement_data, second_statement_data])

allow(Fintoc::V2::AccountStatement).to receive(:new)
end

describe '#list' do
it 'calls build_account_statement for each response' do
manager.list
expect(Fintoc::V2::AccountStatement)
.to have_received(:new).with(**first_statement_data)
expect(Fintoc::V2::AccountStatement)
.to have_received(:new).with(**second_statement_data)
end
end
end
Loading
Loading