Class: CryptocoinPayable::PricingProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptocoin_payable/commands/pricing_processor.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete_currency_conversions(time_ago) ⇒ Object



7
8
9
# File 'lib/cryptocoin_payable/commands/pricing_processor.rb', line 7

def self.delete_currency_conversions(time_ago)
  new.delete_currency_conversions(time_ago)
end

.performObject



3
4
5
# File 'lib/cryptocoin_payable/commands/pricing_processor.rb', line 3

def self.perform
  new.perform
end

Instance Method Details

#delete_currency_conversions(time_ago) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/cryptocoin_payable/commands/pricing_processor.rb', line 32

def delete_currency_conversions(time_ago)
  # Makes sure to keep at least one record in the db since other areas of
  # the gem assume the existence of at least one record.
  last_id = CurrencyConversion.last.id
  time = time_ago || 1.month.ago
  CurrencyConversion.where('created_at < ? AND id != ?', time, last_id).delete_all
end

#performObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cryptocoin_payable/commands/pricing_processor.rb', line 11

def perform
  rates = CurrencyConversion.coin_types.map do |coin_pair|
    coin_type = coin_pair[0].to_sym
    [
      coin_type,
      CurrencyConversion.create!(
        # TODO: Store three previous price ranges, defaulting to 100 for now.
        currency: 100,
        price: Adapters.for(coin_type).fetch_rate,
        coin_type: coin_type
      )
    ]
  end.to_h

  # Loop through all unpaid payments and update them with the new price if
  # it has been 30 mins since they have been updated.
  CoinPayment.unpaid.stale.find_each do |payment|
    payment.update_coin_amount_due(rate: rates[payment.coin_type.to_sym].price)
  end
end