class Sibit::BestOf

Best of API.

Public Class Methods

new(list, log: Sibit::Log.new, verbose: false) click to toggle source

Constructor.

# File lib/sibit/bestof.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/bestof.rb, line 50
def balance(address)
  best_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/bestof.rb, line 96
def block(hash)
  best_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/bestof.rb, line 72
def fees
  best_of('fees', &:fees)
end
height(hash) click to toggle source

Get the height of the block.

# File lib/sibit/bestof.rb, line 57
def height(hash)
  best_of('height') do |api|
    api.height(hash)
  end
end
latest() click to toggle source

Latest block hash.

# File lib/sibit/bestof.rb, line 84
def latest
  best_of('latest', &:latest)
end
next_of(hash) click to toggle source

Get the hash of the next block.

# File lib/sibit/bestof.rb, line 64
def next_of(hash)
  best_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/bestof.rb, line 43
def price(currency = 'USD')
  best_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/bestof.rb, line 89
def push(hex)
  best_of('push') do |api|
    api.push(hex)
  end
end
utxos(keys) click to toggle source

Fetch all unspent outputs per address.

# File lib/sibit/bestof.rb, line 77
def utxos(keys)
  best_of('utxos') do |api|
    api.utxos(keys)
  end
end

Private Instance Methods

best_of(method) { |list| ... } click to toggle source
# File lib/sibit/bestof.rb, line 104
    def best_of(method)
      return yield @list unless @list.is_a?(Array)
      results = []
      errors = []
      @list.each do |api|
        results << yield(api)
      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
      if results.empty?
        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
      results.group_by(&:to_s).values.max_by(&:size)[0]
    end