Class: F2ynab::Webhooks::Monzo

Inherits:
Object
  • Object
show all
Defined in:
lib/f2ynab/webhooks/monzo.rb

Instance Method Summary collapse

Constructor Details

#initialize(ynab_client, webhook, skip_tags: false, skip_foreign_currency_flag: false, skip_emoji: false) ⇒ Monzo

Returns a new instance of Monzo.



4
5
6
7
8
9
10
# File 'lib/f2ynab/webhooks/monzo.rb', line 4

def initialize(ynab_client, webhook, skip_tags: false, skip_foreign_currency_flag: false, skip_emoji: false)
  @ynab_client = ynab_client
  @webhook = webhook
  @skip_tags = skip_tags
  @skip_foreign_currency_flag = skip_foreign_currency_flag
  @skip_emoji = skip_emoji
end

Instance Method Details

#importObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/f2ynab/webhooks/monzo.rb', line 12

def import
  return { warning: :unsupported_type } unless @webhook[:type] == 'transaction.created'
  return { warning: :zero_value } if @webhook[:data][:amount].to_i.zero?
  return { warning: :declined } if @webhook[:data][:decline_reason].present?

  payee_name = @webhook[:data][:merchant].try(:[], :name)
  payee_name ||= @webhook[:data][:counterparty][:name] if @webhook[:data][:counterparty].present?
  payee_name ||= 'Topup' if @webhook[:data][:metadata][:is_topup]
  payee_name ||= @webhook[:data][:description]

  description = ''
  flag = nil

  foreign_transaction = @webhook[:data][:local_currency] != @webhook[:data][:currency]
  if foreign_transaction
    money = ::Money.new(@webhook[:data][:local_amount].abs, @webhook[:data][:local_currency])
    description.prepend("(#{money.format}) ")
    flag = 'orange' unless @skip_foreign_currency_flag
  end

  unless @skip_emoji
    description.prepend("#{@webhook[:data][:merchant][:emoji]} ") if @webhook[:data][:merchant].try(:[], :emoji)
  end

  unless @skip_tags
    description << @webhook[:data][:merchant][:metadata][:suggested_tags] if @webhook[:data][:merchant].try(:[], :metadata).try(:[], :suggested_tags)
  end

  # If this is a split repayment, then add that to the description
  if @webhook[:data][:metadata].try(:[], :p2p_initiator) == 'payment-request' && @webhook[:data][:merchant].present? && @webhook[:data][:counterparty].present?
    description << " (Repayment to #{@webhook[:data][:counterparty][:name]})"
  end

  ::F2ynab::YNAB::TransactionCreator.new(
    @ynab_client,
    id: @webhook[:data][:id],
    date: Time.parse(@webhook[:data][:created]).to_date,
    amount: @webhook[:data][:amount] * 10,
    payee_name: payee_name,
    description: description.strip,
    cleared: !foreign_transaction,
    flag: flag,
  ).create
end