class F2ynab::Webhooks::Starling

Constants

WEBHOOKS_TYPES

Public Class Methods

new(ynab_client, webhook, skip_foreign_currency_flag: false) click to toggle source
# File lib/f2ynab/webhooks/starling.rb, line 32
def initialize(ynab_client, webhook, skip_foreign_currency_flag: false)
  @webhook = webhook
  @ynab_client = ynab_client
  @skip_foreign_currency_flag = skip_foreign_currency_flag
end

Public Instance Methods

import() click to toggle source
# File lib/f2ynab/webhooks/starling.rb, line 38
def import
  return { warning: :unsupported_type } unless @webhook[:webhookType].in?(WEBHOOKS_TYPES)

  payee_name = @webhook[:content][:counterParty]
  amount = (@webhook[:content][:amount].to_f * 1000).to_i

  # Direct Debits need to be swapped to negative
  amount *= -1 if amount.positive? && @webhook[:webhookType] == 'TRANSACTION_DIRECT_DEBIT'

  description = @webhook[:content][:forCustomer].to_s
  flag = nil

  foreign_transaction = @webhook[:content][:sourceCurrency] != 'GBP'
  if foreign_transaction && !@skip_foreign_currency_flag
    flag = 'orange'
  end

  ::F2ynab::YNAB::TransactionCreator.new(
    @ynab_client,
    id: "S:#{@webhook[:content][:transactionUid]}",
    date: Time.parse(@webhook[:timestamp]).to_date,
    amount: amount,
    payee_name: payee_name,
    description: description.strip,
    cleared: !foreign_transaction,
    flag: flag,
  ).create
end