class BlockrIo

This is a ‘driver’ meant to emulate bitcoin-client calls, by way of the blockr.io API

Public Class Methods

new(is_testing = false) click to toggle source
# File lib/blockr_io.rb, line 6
def initialize(is_testing = false)
  @is_testing = is_testing
end

Public Instance Methods

api_url() click to toggle source
# File lib/blockr_io.rb, line 10
def api_url
  'http://%s.blockr.io/api/v1' % (is_testing? ? 'tbtc' : 'btc')
end
getrawtransaction(tx_id) click to toggle source
# File lib/blockr_io.rb, line 14
def getrawtransaction(tx_id)
  json_get('tx', 'raw', tx_id.to_s)['data']['tx']['hex']
end
is_testing?() click to toggle source
# File lib/blockr_io.rb, line 23
def is_testing?
  @is_testing
end
listunconfirmed(addr) click to toggle source

Shows all the transactions that are unconfirmed for the provided address:

# File lib/blockr_io.rb, line 33
def listunconfirmed(addr)
  json_get('address','unconfirmed',addr)['data']['unconfirmed']
end
listunspent(addr, include_unconfirmed = false) click to toggle source
# File lib/blockr_io.rb, line 27
def listunspent(addr, include_unconfirmed = false)
  query = [addr,(include_unconfirmed) ? '?unconfirmed=1' : nil ].join
  json_get('address', 'unspent', query)['data']['unspent']
end
sendrawtransaction(raw_tx) click to toggle source
# File lib/blockr_io.rb, line 18
def sendrawtransaction(raw_tx)
  request('tx', 'push'){|req| req.post( {hex: raw_tx}.to_json, 
    accept: 'json', content_type: 'json' ) }['data']
end

Private Instance Methods

client(*path_parts) click to toggle source
# File lib/blockr_io.rb, line 45
def client(*path_parts)
   RestClient::Resource.new( ([api_url]+path_parts).join('/') )
end
json_get(*path) click to toggle source
# File lib/blockr_io.rb, line 49
def json_get(*path)
  request(*path){ |req| req.get content_type: 'json' }
end
request(*path, &block) click to toggle source
# File lib/blockr_io.rb, line 39
def request(*path, &block)
  json = JSON.parse(block.call(client(*path)))
  raise ResponseError unless json['status'] == 'success' && json['code'] == 200
  json
end