I just wonder whether it is a common approach to hit the third-party API and fetch values within the initialize
method?
Ex.
class BalanceTransaction::Update
include Service
attr_reader :offset, :transactions
def initialize(offset)
@offset = offset
@transactions = fetch_transactions
end
def call
transactions.auto_paging_each do |txn|
type = txn[:type]
source = txn[:source]
case type
when 'charge', 'adjustment' # NOTE: adjustment is something
invoice = find_invoice(source)
ac_id = invoice.ac.id
update_attrs(id: invoice.id, account_id: ac_id, type:'invoice', attrs: txn)
when 'refund'
refund = find_refund(source)
ac_id = refund.invoice.account.id
update_attrs(id: refund.id, account_id: ac_id, type:'refund', attrs: txn)
end
end
end
private
def external_card_balance
Stripe::BalanceTransaction
end
def fetch_transactions
external_card_balance.all
end
Service
? \$\endgroup\$