class FakePlaid::StubApp

Private Instance Methods

_params() click to toggle source

Monkey-paching `params` method.

Requests from Plaid come in but the `params` are not populated.

# File lib/fake_plaid/stub_app.rb, line 46
def _params
  return params if params.present?

  @_params ||= JSON.parse(request.body.read)
end
derive_transactions_from_params() click to toggle source
# File lib/fake_plaid/stub_app.rb, line 52
def derive_transactions_from_params
  if valid_transaction_params?
    # Fetch base transactions. (Count: 100)
    transactions_arr = JSON.parse(fixture('transactions')).dig('transactions')

    # We need to update the dates on these transactions so they are
    # dynamic.
    #
    # Dates are set like this:
    # 1.year.ago > date >= Date.current
    #
    # Date format:
    # 'YYYY-MM-DD'
    #
    # Update all transactions except 5 with random date.
    # Update at least 5 with current date.
    #
    # Current date is needed to replicate 'new' fetched transactions.
    transactions_arr.drop(5).each_slice(5) do |txns|
      # Generate random date every 5 transactions
      date = rand_date

      txns.each do |txn|
        txn['date'] = date.strftime('%Y-%m-%d')
      end
    end

    # Update 5 transactions with current date.
    transactions_arr[0..4].each do |txn|
      txn['date'] = Date.current.strftime('%Y-%m-%d')
    end

    transactions = transactions_arr.select do |txn|
      txn.dig('date') > start_date && txn.dig('date') <= end_date
    end

    JSON.generate(
      'transactions' =>
      if transaction_count && transaction_count > 0
        transactions[0..(transaction_count - 1)]
      else
        transactions
      end
    )
  end
end
end_date() click to toggle source
# File lib/fake_plaid/stub_app.rb, line 115
def end_date
  _params.dig('end_date')
end
fixture(file_name) click to toggle source
# File lib/fake_plaid/stub_app.rb, line 38
def fixture(file_name)
  file_path = File.join(FakePlaid.fixture_path, "#{file_name}.json")
  File.open(file_path, 'rb').read
end
json_response(response_code, response_body) click to toggle source
# File lib/fake_plaid/stub_app.rb, line 32
def json_response(response_code, response_body)
  content_type :json
  status response_code
  response_body
end
rand_date(from=1.year.ago.to_date, to=Date.current) click to toggle source
# File lib/fake_plaid/stub_app.rb, line 99
def rand_date(from=1.year.ago.to_date, to=Date.current)
  rand(from..to)
end
sandbox_credentials_valid?() click to toggle source
# File lib/fake_plaid/stub_app.rb, line 27
def sandbox_credentials_valid?
  params.dig('credentials', 'username') == 'user_good' &&
    params.dig('credentials', 'password') == 'pass_good'
end
start_date() click to toggle source
# File lib/fake_plaid/stub_app.rb, line 111
def start_date
  _params.dig('start_date')
end
transaction_count() click to toggle source
# File lib/fake_plaid/stub_app.rb, line 119
def transaction_count
  _params.dig('options', 'count')
end
valid_transaction_params?() click to toggle source
# File lib/fake_plaid/stub_app.rb, line 103
def valid_transaction_params?
  _params.dig('access_token') &&
    _params.dig('secret') &&
    _params.dig('client_id') &&
    _params.dig('start_date') &&
    _params.dig('end_date')
end