class Sibit::FirstOf
First of API.
Public Class Methods
new(list, log: Sibit::Log.new, verbose: false)
click to toggle source
Constructor.
# File lib/sibit/firstof.rb, line 36 def initialize(list, log: Sibit::Log.new, verbose: false) @list = list @log = log @verbose = verbose end
Public Instance Methods
balance(address)
click to toggle source
Gets the balance of the address, in satoshi.
# File lib/sibit/firstof.rb, line 50 def balance(address) first_of('balance') do |api| api.balance(address) end end
block(hash)
click to toggle source
This method should fetch a block and return as a hash.
# File lib/sibit/firstof.rb, line 96 def block(hash) first_of('block') do |api| api.block(hash) end end
fees()
click to toggle source
Get recommended fees, in satoshi per byte. The method returns a hash: { S: 12, M: 45, L: 100, XL: 200 }
# File lib/sibit/firstof.rb, line 72 def fees first_of('fees', &:fees) end
height(hash)
click to toggle source
Get the height of the block.
# File lib/sibit/firstof.rb, line 57 def height(hash) first_of('height') do |api| api.height(hash) end end
latest()
click to toggle source
Latest block hash.
# File lib/sibit/firstof.rb, line 84 def latest first_of('latest', &:latest) end
next_of(hash)
click to toggle source
Get the hash of the next block.
# File lib/sibit/firstof.rb, line 64 def next_of(hash) first_of('next_of') do |api| api.next_of(hash) end end
price(currency = 'USD')
click to toggle source
Current price of BTC in USD (float returned).
# File lib/sibit/firstof.rb, line 43 def price(currency = 'USD') first_of('price') do |api| api.price(currency) end end
push(hex)
click to toggle source
Push this transaction (in hex format) to the network.
# File lib/sibit/firstof.rb, line 89 def push(hex) first_of('push') do |api| api.push(hex) end end
utxos(keys)
click to toggle source
Fetch all unspent outputs per address.
# File lib/sibit/firstof.rb, line 77 def utxos(keys) first_of('utxos') do |api| api.utxos(keys) end end
Private Instance Methods
first_of(method) { |list| ... }
click to toggle source
# File lib/sibit/firstof.rb, line 104 def first_of(method) return yield @list unless @list.is_a?(Array) errors = [] done = false result = nil @list.each do |api| @log.info("Calling #{api.class.name}##{method}()...") begin result = yield api done = true break rescue Sibit::NotSupportedError # Just ignore it rescue Sibit::Error => e errors << e @log.info("The API #{api.class.name} failed at #{method}(): #{e.message}") if @verbose end end unless done errors.each { |e| @log.info(Backtrace.new(e).to_s) } raise Sibit::Error, "No APIs out of #{@list.length} managed to succeed at #{method}(): \ #{@list.map { |a| a.class.name }.join(', ')}" end result end