Class: CoinSync::Outputs::List

Inherits:
Base
  • Object
show all
Defined in:
lib/coinsync/outputs/list.rb

Direct Known Subclasses

SplitList

Instance Method Summary collapse

Methods inherited from Base

#initialize, register_output

Constructor Details

This class inherits a constructor from CoinSync::Outputs::Base

Instance Method Details

#fiat_transaction_to_csv(tx) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/coinsync/outputs/list.rb', line 60

def fiat_transaction_to_csv(tx)
  csv = [
    tx.number || 0,
    tx.exchange,
    @config.translate(tx.type.to_s.capitalize),
    @formatter.format_time(tx.time),
    @formatter.format_crypto(tx.crypto_amount),
    tx.crypto_currency.code,
    @formatter.format_fiat(tx.fiat_amount),
    @formatter.format_fiat_price(tx.price),
    tx.fiat_currency.code || ''
  ]

  if @config.currency_conversion
    if tx.converted
      csv += [
        @formatter.format_fiat(tx.converted.fiat_amount),
        @formatter.format_fiat_price(tx.converted.price),
        tx.converted.exchange_rate && @formatter.format_float(tx.converted.exchange_rate, precision: 4)
      ]
    else
      csv += [
        @formatter.format_fiat(tx.fiat_amount),
        @formatter.format_fiat_price(tx.price),
        nil
      ]
    end
  end

  csv
end

#headersObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/coinsync/outputs/list.rb', line 24

def headers
  line = [
    'No.',
    'Exchange',
    'Type',
    'Date',
    'Amount',
    'Asset',
    'Total value',
    'Price',
    'Currency'
  ].map { |l| @config.translate(l) }

  if options = @config.currency_conversion
    line += [
      @config.translate('Total value ($CURRENCY)').gsub('$CURRENCY', options.currency.code),
      @config.translate('Price ($CURRENCY)').gsub('$CURRENCY', options.currency.code),
      @config.translate('Exchange rate')
    ]
  end

  line
end

#process_transaction(tx, csv) ⇒ Object



48
49
50
# File 'lib/coinsync/outputs/list.rb', line 48

def process_transaction(tx, csv)
  csv << transaction_to_csv(tx)
end

#process_transactions(transactions, *args) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/coinsync/outputs/list.rb', line 14

def process_transactions(transactions, *args)
  CSV.open(@target_file, 'w', col_sep: @config.column_separator) do |csv|
    csv << headers

    transactions.each do |tx|
      process_transaction(tx, csv)
    end
  end
end

#requires_currency_conversion?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/coinsync/outputs/list.rb', line 10

def requires_currency_conversion?
  true
end

#swap_transaction_to_csv(tx) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/coinsync/outputs/list.rb', line 92

def swap_transaction_to_csv(tx)
  if @classifier.is_purchase?(tx)
    tx_type = Transaction::TYPE_PURCHASE
    asset = tx.bought_currency
    asset_amount = tx.bought_amount
    currency = tx.sold_currency
    currency_amount = tx.sold_amount
  else
    tx_type = Transaction::TYPE_SALE
    asset = tx.sold_currency
    asset_amount = tx.sold_amount
    currency = tx.bought_currency
    currency_amount = tx.bought_amount
  end

  csv = [
    tx.number || 0,
    tx.exchange,
    @config.translate(tx_type.to_s.capitalize),
    @formatter.format_time(tx.time),
    @formatter.format_crypto(asset_amount),
    asset.code,
    @formatter.format_crypto(currency_amount),
    @formatter.format_crypto(currency_amount / asset_amount),
    currency.code
  ]

  if @config.currency_conversion
    csv += [nil, nil, nil]
  end

  csv
end

#transaction_to_csv(tx) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/coinsync/outputs/list.rb', line 52

def transaction_to_csv(tx)
  if tx.purchase? || tx.sale?
    fiat_transaction_to_csv(tx)
  else
    swap_transaction_to_csv(tx)
  end
end