class BitcoinPayable::PricingProcessor

Public Class Methods

new() click to toggle source
# File lib/bitcoin_payable/commands/pricing_processor.rb, line 8
def initialize
end
perform() click to toggle source
# File lib/bitcoin_payable/commands/pricing_processor.rb, line 4
def self.perform
  new.perform
end

Public Instance Methods

get_btc() click to toggle source
# File lib/bitcoin_payable/commands/pricing_processor.rb, line 24
def get_btc
  uri = URI.parse("https://api.bitcoinaverage.com/all")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Get.new(uri.request_uri)

  response = http.request(request)
  hash = JSON.parse(response.body)

  hash[BitcoinPayable.config.currency.to_s.upcase]["averages"]["24h_avg"].to_f * 100.00
end
get_currency() click to toggle source
# File lib/bitcoin_payable/commands/pricing_processor.rb, line 37
def get_currency
  #uri = URI("http://rate-exchange.appspot.com/currency?from=#{BitcoinPayable.config.currency}&to=USD")
  #rate = JSON.parse(Net::HTTP.get(uri))["rate"]

  uri = URI("http://openexchangerates.org/api/latest.json?app_id=#{BitcoinPayable.config.open_exchange_key}")
  response = JSON.parse(Net::HTTP.get(uri))
  response["rates"][BitcoinPayable.config.currency.to_s.upcase]
end
perform() click to toggle source
# File lib/bitcoin_payable/commands/pricing_processor.rb, line 11
def perform
  # => Store three previous price ranges
  # => get_currency TODO: enable this again
  # => Defaulting to 1.00 for now
  rate = CurrencyConversion.create!(currency: 1.00, btc: get_btc)

  # => Loop through all unpaid payments and update them with the new price
  # => If it has been 20 mins since they have been updated
  BitcoinPayable::BitcoinPayment.where(state: ['pending', 'partial_payment']).where("updated_at < ? OR btc_amount_due = 0", 30.minutes.ago).each do |bp|
    bp.update(btc_amount_due: bp.calculate_btc_amount_due)
  end
end