# frozen_string_literal: true require 'truelayer/base_repository' require 'truelayer/response' require 'truelayer/card' require 'truelayer/balance' require 'truelayer/transaction' module Truelayer class CardsRepository < BaseRepository def all_cards(webhook_uri: nil) response = get('/data/v1/cards', params: async_params(webhook_uri)) Response.build_with_results(json: response.body, results_class: Card) end def find_card(card_id, webhook_uri: nil) url = format_url('/data/v1/cards/:card_id', card_id: card_id) response = get(url, params: async_params(webhook_uri)) Response.build_with_results(json: response.body, results_class: Card) end def current_balance(card_id, webhook_uri: nil) url = format_url('/data/v1/cards/:card_id/balance', card_id: card_id) response = get(url, params: async_params(webhook_uri)) Response.build_with_results(json: response.body, results_class: Balance) end def transactions(card_id, from: nil, to: nil, webhook_uri: nil) url = format_url('/data/v1/cards/:card_id/transactions', card_id: card_id) response = get(url, params: { from: from, to: to }.merge(async_params(webhook_uri))) Response.build_with_results(json: response.body, results_class: Transaction) end private def async_params(webhook_uri) {}.tap do |params| if webhook_uri params[:async] = true params[:webhook_uri] = webhook_uri end end end end end