Skip to content

Commit

Permalink
Merge pull request #9 from mikamai/transactions
Browse files Browse the repository at this point in the history
modif tests transactions
  • Loading branch information
Lauris Z authored Sep 3, 2019
2 parents a07e38a + bf308b7 commit fdf8b49
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 79 deletions.
6 changes: 6 additions & 0 deletions lib/figo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,11 @@ def query_api_object(type, path, data = nil, method = 'GET', array_name = nil) #

response[array_name].map { |entry| type.new(self, entry) }
end

def encodeOptions(options)
options.delete_if { |k, v| v.nil? }.to_query
optionsEncoded = URI.encode_www_form(options)
return optionsEncoded
end
end
end
101 changes: 65 additions & 36 deletions lib/transaction/api_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,32 @@

require_relative 'model.rb'
module Figo
# Retrieve list of transactions (on all or a specific account)
# Retrieve list of transactions (on all accounts)
#
# @param options [Object]: further optional options
# accounts: comma separated list of account IDs.
# filter (obj) - Can take 4 possible keys:
# - date (ISO date) - Transaction date
# - person (str) - Payer or payee name
# - purpose (str)
# - amount (num)
# sync_id (str): Show only those items that have been created within this synchronization.
# count (int): Limit the number of returned transactions.
# offset (int): Which offset into the result set should be used to determine the
# first transaction to return (useful in combination with count)
# sort (enum): ASC or DESC
# since (ISO date): Return only transactions after this date based on since_type
# until (ISO date): This parameter can either be a transaction ID or a date. Return only transactions which were booked on or before
# since_type (enum): This parameter defines how the parameter since will be interpreted.
# Possible values: "booked" "created" "modified"
# types (enum): Comma separated list of transaction types used for filtering.
# Possible values:"Transfer", "Standing order", "Direct debit", "Salary or rent", "GeldKarte", "Charges or interest"
# cents (bool): If true amounts will be shown in cents, Optional, default: False
# include_pending (bool): This flag indicates whether pending transactions should
# be included in the response. Pending transactions are always
# included as a complete set, regardless of the `since` parameter.
# include_statistics (bool): Includes statistics on the returned transactionsif true, Default: false.
=======
# @param account_id [String] ID of the account for which to list the transactions
# @param since [String, Date] this parameter can either be a transaction ID or a date
# @param count [Integer] limit the number of returned transactions
Expand All @@ -13,48 +37,53 @@ module Figo
# in the response; pending transactions are always included as a complete set, regardless of
# the `since` parameter
# @return [Array] an array of `Transaction` objects, one for each transaction of the user
def transactions(account_id = nil, since = nil, count = 1000, offset = 0, include_pending = false)
data = { 'count' => count.to_s, 'offset' => offset.to_s, 'include_pending' => include_pending ? '1' : '0' }
data['since'] = ((since.is_a?(Date) ? since.to_s : since) unless since.nil?)

query_api_object Transaction, (account_id.nil? ? '/rest/transactions?' : "/rest/accounts/#{account_id}/transactions?") + URI.encode_www_form(data), nil, 'GET', 'transactions'
def list_transactions(options)
path = "/rest/transactions?#{encodeOptions options}"
query_api_object Transaction, path, nil, "GET", "transactions"
end

# Retrieve a specific transaction
# Retrieve list of transactions (on a specific account)
#
# @param account_id [String] ID of the account on which the transaction occured
# @param transaction_id [String] ID of the transaction to be retrieved
# @return [Transaction] transaction object
def get_transaction(account_id, transaction_id)
query_api_object Transaction, "/rest/accounts/#{account_id}/transactions/#{transaction_id}"
end

# Modify trasnaction
#
# @param account_id [String] ID of an account the transaction belongs to
# @param transaction_id [String] ID of the transaction
# @param visited [Boolean] a bit showing whether the user has already seen this transaction or not
def modify_transaction(account_id, transaction_id, visited)
query_api_object Transaction, '/rest/accounts/' + account_id + '/transactions/' + transaction_id, { 'visited' => visited }, 'PUT'
# @param account_id [String] ID of the account for which to list the transactions
# @param options [Object]: further optional options
# accounts: comma separated list of account IDs.
# filter (obj) - Can take 4 possible keys:
# - date (ISO date) - Transaction date
# - person (str) - Payer or payee name
# - purpose (str)
# - amount (num)
# count (int): Limit the number of returned transactions.
# offset (int): Which offset into the result set should be used to determine the
# first transaction to return (useful in combination with count)
# sort (enum): ASC or DESC
# since (ISO date): Return only transactions after this date based on since_type
# until (ISO date): This parameter can either be a transaction ID or a date. Return only transactions which were booked on or before
# since_type (enum): This parameter defines how the parameter since will be interpreted.
# Possible values: "booked" "created" "modified"
# types (enum): Comma separated list of transaction types used for filtering.
# Possible values:"Transfer", "Standing order", "Direct debit", "Salary or rent", "GeldKarte", "Charges or interest"
# cents (bool): If true amounts will be shown in cents, Optional, default: False
# include_pending (bool): This flag indicates whether pending transactions should
# be included in the response. Pending transactions are always
# included as a complete set, regardless of the `since` parameter.
# include_statistics (bool): Includes statistics on the returned transactionsif true, Default: false.
# @return [Array] an array of `Transaction` objects, one for each transaction of the user
def list_transactions_of_account(account_id, options)
path = "/rest/accounts/#{account_id}/transactions?#{encodeOptions options}"
query_api_object Transaction, path, nil, "GET", "transactions"
end

# Modify transactions
# Retrieve a specific transaction
#
# @param visited [Boolean] a bit showing whether the user has already seen this transaction or not
# @param account_id [String] ID of the account transactions belongs to (optional)
def modify_transactions(visited, account_id = nil)
if account_id
query_api '/rest/accounts/' + account_id + '/transactions', { 'visited' => visited }, 'PUT'
# @param account_id [String] ID of the account on which the transaction occured (required)
# @param transaction_id [String] ID of the transaction to be retrieved (optional)
# @param cents [Boolean] If true amounts will be shown in cents (optional, default: false)
# @return [Transaction] transaction object
def get_transaction(account_id, transaction_id, cents = false)
if transaction_id.nil?
query_api_object Transaction, "/rest/accounts/#{account_id}/transactions?cents=#{cents}"
else
query_api '/rest/transactions', { 'visited' => visited }, 'PUT'
query_api_object Transaction, "/rest/accounts/#{account_id}/transactions/#{transaction_id}?cents=#{cents}"
end
end

# Delete transaction
#
# @param account_id [String] ID of an account the transaction belongs to
# @param transaction_id [String] ID of transaction to be deleted
def delete_transaction(account_id, transaction_id)
query_api '/rest/accounts/' + account_id + '/transactions/' + transaction_id, nil, 'DELETE'
end
end
46 changes: 3 additions & 43 deletions test/test_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,10 @@
class FigoTest < MiniTest::Spec
include Setup

## Transactions
# Retrieve a Transaction
def test_retreive_a_transaction
transactions = figo_session.get_account('A1.2').transactions
transaction = figo_session.get_transaction('A1.2', transactions[0].transaction_id)

assert transaction
end

# Retrieve Transactions of one Account
def test_retreive_transactions_of_one_account
transactions = figo_session.get_account('A1.2').transactions
assert !transactions.empty?
end

# Retrieve Transactions of all Accounts
def test_retreive_transactions_of_all_accounts
transactions = figo_session.transactions
assert !transactions.empty?
end

# Modify a Transaction*
def test_modify_a_transaction
tID = @sut.get_account('A1.2').transactions[-1].transaction_id
exception = assert_raises(Figo::Error) { figo_session.modify_transaction('A1.2', tID, true) }
assert 'Missing, invalid or expired access token.', exception.message
end

# Modify all Transactions of one Account*
def test_modify_all_transactions_of_one_account
exception = assert_raises(Figo::Error) { figo_session.modify_transactions(false, 'A1.2') }
assert 'Missing, invalid or expired access token.', exception.message
end

# Modify all Transactions of all Accounts*
def test_modify_all_transactions_of_all_accounts
exception = assert_raises(Figo::Error) { figo_session.modify_transactions(false) }
assert 'Missing, invalid or expired access token.', exception.message
end

# Delete a Transaction
def test_delete_transaction
tID = figo_session.get_account('A1.2').transactions[-1].transaction_id
exception = assert_raises(Figo::Error) { figo_session.modify_transactions('A1.2', tID) }
assert 'Missing, invalid or expired access token.', exception.message
options = {cents: true, accounts: nil}
transactions = figo_session.list_transactions(options)
assert transactions.instance_of?(Array)
end
end

0 comments on commit fdf8b49

Please sign in to comment.