Sha256: cefd5a2d8517f6bf0cad19940041b0ac1b1b0f6996111e8780782efbbf021481

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

module CryptocoinPayable
  class PricingProcessor
    def self.perform
      new.perform
    end

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

    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: payment.calculate_coin_amount_due,
          coin_conversion: rates[payment.coin_type.to_sym].price
        )
      end
    end

    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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cryptocoin_payable-1.2.0 lib/cryptocoin_payable/commands/pricing_processor.rb
cryptocoin_payable-1.1.0 lib/cryptocoin_payable/commands/pricing_processor.rb
cryptocoin_payable-1.0.1 lib/cryptocoin_payable/commands/pricing_processor.rb
cryptocoin_payable-1.0.0 lib/cryptocoin_payable/commands/pricing_processor.rb