Class: CoinSync::CryptoClassifier

Inherits:
Object
  • Object
show all
Defined in:
lib/coinsync/crypto_classifier.rb

Constant Summary collapse

MAX_INDEX =
1_000_000

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CryptoClassifier

Returns a new instance of CryptoClassifier.



7
8
9
10
# File 'lib/coinsync/crypto_classifier.rb', line 7

def initialize(config)
  @config = config
  @base_currencies = config.base_cryptocurrencies.map { |c| CryptoCurrency.new(c) }
end

Instance Method Details

#is_purchase?(transaction) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/coinsync/crypto_classifier.rb', line 12

def is_purchase?(transaction)
  bought_index = @base_currencies.index(transaction.bought_currency) || MAX_INDEX
  sold_index = @base_currencies.index(transaction.sold_currency) || MAX_INDEX

  if bought_index < sold_index
    false
  elsif bought_index > sold_index
    true
  else
    raise "Couldn't determine which cryptocurrency is the base one: #{transaction.bought_currency.code} vs " +
      "#{transaction.sold_currency.code}. Use the `base_cryptocurrencies` setting to explicitly choose " +
      "base cryptocurrencies."
  end
end