diff --git a/lib/blnk/balance.rb b/lib/blnk/balance.rb index 4ca7f44..e35c1cb 100644 --- a/lib/blnk/balance.rb +++ b/lib/blnk/balance.rb @@ -4,8 +4,8 @@ module Blnk # Balance representation class Balance < Resourceable def self.resource_name = :balances + def self.id_field = :balance_id - def persisted? = !balance_id.nil? def body_data = { ledger_id:, currency: } end end diff --git a/lib/blnk/ledger.rb b/lib/blnk/ledger.rb index 815a78c..e7b5baf 100644 --- a/lib/blnk/ledger.rb +++ b/lib/blnk/ledger.rb @@ -4,8 +4,8 @@ module Blnk # Ledger representation class Ledger < Resourceable def self.resource_name = :ledgers + def self.id_field = :ledger_id - def persisted? = !ledger_id.nil? def body_data = { name:, meta_data: } end end diff --git a/lib/blnk/resourceable.rb b/lib/blnk/resourceable.rb index 214a755..637c0b9 100644 --- a/lib/blnk/resourceable.rb +++ b/lib/blnk/resourceable.rb @@ -7,47 +7,50 @@ class SearchResult < OpenStruct; end include Client - def self.resource_name = raise NotImplementedError + class << self + def resource_name = raise NotImplementedError + def id_field = :id - def self.find(id) - response = new.get_request(path: "/#{resource_name}/#{id}") - return response unless response.status.success? + def find(id) + response = new.get_request(path: "/#{resource_name}/#{id}") + return response unless response.status.success? - new response.parse - end + new response.parse + end - def self.all - response = new.get_request(path: "/#{resource_name}") - return response unless response.status.success? + def all + response = new.get_request(path: "/#{resource_name}") + return response unless response.status.success? - response.parse.map do |r| - new r + response.parse.map do |r| + new r + end end - end - def self.create(*) - response = new.post_request( - path: "/#{resource_name}", - body: new(*).body_data - ) - return response unless response.status.success? + def create(**args) + response = new.post_request( + path: "/#{resource_name}", + body: args + ) + return response unless response.status.success? - new(response.parse) - end + new(response.parse) + end - def self.search(**args) - response = new.post_request( - path: "/search/#{resource_name}", - body: args - ) - return response unless response.status.success? + def search(**args) + response = new.post_request( + path: "/search/#{resource_name}", + body: args + ) + return response unless response.status.success? - sr = SearchResult.new(response.parse) - sr.resource_name = resource_name - sr + sr = SearchResult.new(response.parse) + sr.resource_name = resource_name + sr + end end - def persisted? = raise NotImplementedError + def persisted? = table[self.class.id_field] def body_data = raise NotImplementedError end end diff --git a/lib/blnk/transaction.rb b/lib/blnk/transaction.rb index 5624bbe..308b2eb 100644 --- a/lib/blnk/transaction.rb +++ b/lib/blnk/transaction.rb @@ -4,8 +4,8 @@ module Blnk # Transaction representation class Transaction < Resourceable def self.resource_name = :transactions + def self.id_field = :transaction_id - def persisted? = !transaction_id.nil? def body_data = {} end end diff --git a/test/blnk/test_transaction.rb b/test/blnk/test_transaction.rb new file mode 100644 index 0000000..426b257 --- /dev/null +++ b/test/blnk/test_transaction.rb @@ -0,0 +1,111 @@ +# frozen_string_literal: true + +require 'test_helper' + +def stub_find_transaction_request_with_error + stub_request(:get, %r{/transactions/(.*)}) + .to_return_json(body: { error: 'balance with ID \'transaction_id\' not found' }, status: 400) +end + +def transaction_response_body # rubocop:disable Metrics/MethodLength + { + precise_amount: 7500, + amount: 75, + rate: 0, + precision: 100, + transaction_id: 'txn_bb8e13c5-1d16-4e94-add7-efd377fd551a', + parent_transaction: '', + source: 'bln_216507cb-2fb1-4238-a184-805c455f8fe2', + destination: 'bln_469f93bc-40e9-4e0e-b6ab-d11c3638c15d', + reference: 'ref_005', + currency: 'BRLX', + description: 'For fees', + status: 'QUEUED', + hash: '74a2fecf01e73a31c005ce3bd840d285edce87e3bdb3e0ed99e0af124a6165f8', + allow_overdraft: true, + inflight: false, + created_at: '2024-06-27T20:23:17.737289826Z', + scheduled_for: '0001-01-01T00:00:00Z', + inflight_expiry_date: '0001-01-01T00:00:00Z' + } +end + +def stub_find_transaction_request_with_success + stub_request(:get, %r{/transactions/(.*)}) + .to_return_json(body: transaction_response_body, status: 200) +end + +def stub_create_transaction_request_with_success + stub_request(:post, %r{/transactions}) + .to_return_json(body: transaction_response_body, status: 200) +end + +def stub_create_transaction_request_with_error + stub_request(:post, %r{/transactions}) + .to_return_json(body: { error: 'missing transaction_id' }, status: 400) +end + +# NOTE: This route does not exists, at least for now +def stub_all_transaction_request_with_error + stub_request(:get, %r{/transactions}) + .to_return_json(body: { error: 'internal_server_error' }, status: 500) +end + +def stub_all_transaction_request_with_success + stub_request(:get, %r{/transactions}) + .to_return_json(body: [transaction_response_body], status: 200) +end + +class TestTransaction < Minitest::Test + def test_that_transaction_not_found + stub_find_transaction_request_with_error + find = Blnk::Transaction.find 'transaction_id' + + assert find.status.bad_request? + end + + def test_that_transaction_find_success + stub_find_transaction_request_with_success + find = Blnk::Transaction.find 'transaction_id' + + assert find.is_a?(Blnk::Transaction) + assert find.transaction_id.eql?(transaction_response_body[:transaction_id]) + end + + # NOTE: /transactions route does not exist, at moment + # def test_that_transaction_all_error + # stub_all_transaction_request_with_error + + # all = Blnk::Transaction.all + + # assert !all.status.success? + # end + + # def test_that_transaction_all_success + # stub_all_transaction_request_with_success + + # all = Blnk::Transaction.all + + # assert all.is_a?(Array) + # assert all.first.is_a?(Blnk::Transaction) + # assert all.first.transaction_id.eql?(transaction_response_body[:transaction_id]) + # end + + def test_that_transaction_create_errosr + stub_create_transaction_request_with_error + + create = Blnk::Transaction.create + + assert create.status.bad_request? + end + + def test_that_transaction_create_success + stub_create_transaction_request_with_success + + create = Blnk::Transaction.create(ledger_id: 'ledger_id', currency: 'USD') + + assert create.is_a?(Blnk::Transaction) + assert create.transaction_id.eql?(transaction_response_body[:transaction_id]) + assert create.name.eql?(transaction_response_body[:name]) + end +end