Class: BitcoinPayable::PricingProcessor

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePricingProcessor

Returns a new instance of PricingProcessor.



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

def initialize
end

Class Method Details

.performObject



4
5
6
# File 'lib/bitcoin_payable/commands/pricing_processor.rb', line 4

def self.perform
  new.perform
end

Instance Method Details

#get_btcObject



24
25
26
27
28
29
30
31
32
33
34
35
# 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_currencyObject



37
38
39
40
41
42
43
44
# 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

#performObject



11
12
13
14
15
16
17
18
19
20
21
22
# 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