class F2ynab::Import::Monzo

Constants

BASE_URL

Public Class Methods

new(ynab_client, access_token, monzo_account_id, from: 1.year.ago, skip_tags: false, skip_foreign_currency_flag: false, skip_emoji: false) click to toggle source
# File lib/f2ynab/import/monzo.rb, line 6
def initialize(ynab_client, access_token, monzo_account_id, from: 1.year.ago, skip_tags: false, skip_foreign_currency_flag: false, skip_emoji: false)
  @access_token = access_token
  @monzo_account_id = monzo_account_id
  @ynab_client = ynab_client
  @from = from

  @skip_tags = skip_tags
  @skip_foreign_currency_flag = skip_foreign_currency_flag
  @skip_emoji = skip_emoji
end

Public Instance Methods

import() click to toggle source
# File lib/f2ynab/import/monzo.rb, line 17
def import
  transactions_to_create = []
  transactions.reject { |t| t[:decline_reason].present? || t[:amount].zero? }.each do |transaction|
    transactions_to_create << transaction_hash(transaction)
  end

  ::F2ynab::YNAB::BulkTransactionCreator.new(@ynab_client, transactions_to_create).create
end

Private Instance Methods

description_and_flag(transaction) click to toggle source
# File lib/f2ynab/import/monzo.rb, line 36
def description_and_flag(transaction)
  description = ''
  flag = nil

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

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

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

  [description.strip, flag]
end
get(url) click to toggle source
# File lib/f2ynab/import/monzo.rb, line 82
def get(url)
  parse_response(RestClient.get(BASE_URL + url, 'Authorization' => "Bearer #{@access_token}"))
end
parse_response(response) click to toggle source
# File lib/f2ynab/import/monzo.rb, line 86
def parse_response(response)
  JSON.parse(response.body, symbolize_names: true)
end
payee_name(transaction) click to toggle source
# File lib/f2ynab/import/monzo.rb, line 28
def payee_name(transaction)
  payee_name = transaction[:merchant].try(:[], :name)
  payee_name ||= transaction[:counterparty][:name] if transaction[:counterparty].present?
  payee_name ||= 'Topup' if transaction[:is_load]
  payee_name ||= transaction[:description]
  payee_name
end
transaction_hash(transaction) click to toggle source
# File lib/f2ynab/import/monzo.rb, line 58
def transaction_hash(transaction)
  description, flag = description_and_flag(transaction)
  timestamp = Time.parse(transaction[:created])
  id = transaction[:id]

  # We used to prepend "m" to monzo transactions. Keep doing that for now to stop any duplicates.
  id.prepend('M') if timestamp < Time.at(1580652776)

  {
    id: id,
    amount: transaction[:amount] * 10,
    payee_name: payee_name(transaction),
    date: timestamp.to_date,
    description: description,
    cleared: transaction[:settled].present?,
    flag: flag,
  }
end
transactions() click to toggle source
# File lib/f2ynab/import/monzo.rb, line 77
def transactions
  since = @from.present? ? "&since=#{@from.strftime('%FT%TZ')}" : nil
  get("/transactions?account_id=#{@monzo_account_id}#{since}&expand[]=merchant")[:transactions]
end