Class: Bitbank::Client
- Inherits:
-
Object
- Object
- Bitbank::Client
- Defined in:
- lib/bitbank/client.rb
Instance Method Summary collapse
-
#account(account_name) ⇒ Object
Retrieve a particular named account.
-
#account_by_address(address) ⇒ Object
Rerieve the account that the given Bitcoin address belongs to.
-
#accounts ⇒ Object
Returns a list of local accounts.
-
#balance(account_name = nil) ⇒ Object
If an account is not specified, returns the server’s total available balance.
-
#block_count ⇒ Object
Returns the number of blocks in the longest block chain.
-
#block_number ⇒ Object
Returns the block number of the latest block in the longest block chain.
-
#connection_count ⇒ Object
Returns the number of connections to other nodes.
-
#difficulty ⇒ Object
Returns the proof-of-work difficulty as a multiple of the minimum difficulty.
-
#get_work(data = nil) ⇒ Object
If data is not specified, returns formatted hash data to work on.
-
#info ⇒ Object
Returns a hash containing bitcoind status information.
-
#initialize(config = {}) ⇒ Client
constructor
A new instance of Client.
-
#new_account(name) ⇒ Object
Returns a new bitcoin account for receiving payments (along with a new address).
-
#new_address(account_name = nil) ⇒ Object
Returns a new bitcoin address for receiving payments.
- #request(method, *args) ⇒ Object
-
#transactions(account_name = nil, count = 10) ⇒ Object
Returns the most recent transactions for the specified account.
-
#validate_address(address, locals_invalid = false) ⇒ Object
Determine if the given address is valid.
Constructor Details
#initialize(config = {}) ⇒ Client
Returns a new instance of Client.
3 4 5 6 7 |
# File 'lib/bitbank/client.rb', line 3 def initialize(config={}) @config = config @endpoint = "http://#{config[:username]}:#{config[:password]}" + "@#{config[:host]}:#{config[:port]}" end |
Instance Method Details
#account(account_name) ⇒ Object
Retrieve a particular named account.
10 11 12 |
# File 'lib/bitbank/client.rb', line 10 def account(account_name) Bitbank::Account.new(self, account_name, nil, true) end |
#account_by_address(address) ⇒ Object
Rerieve the account that the given Bitcoin address belongs to. Returns nil if the account
16 17 18 19 20 21 22 23 |
# File 'lib/bitbank/client.rb', line 16 def account_by_address(address) account_name = request('getaccount', address) if account_name.present? account(account_name) else nil end end |
#accounts ⇒ Object
Returns a list of local accounts.
26 27 28 29 30 31 |
# File 'lib/bitbank/client.rb', line 26 def accounts account_data = request('listaccounts') account_data.map do |account_name, account_value| Account.new(self, account_name, account_value, false) end end |
#balance(account_name = nil) ⇒ Object
If an account is not specified, returns the server’s total available balance.
If an account is specified, returns the balance in the account.
37 38 39 |
# File 'lib/bitbank/client.rb', line 37 def balance(account_name=nil) request('getbalance', account_name) end |
#block_count ⇒ Object
Returns the number of blocks in the longest block chain.
42 43 44 |
# File 'lib/bitbank/client.rb', line 42 def block_count request('getblockcount') end |
#block_number ⇒ Object
Returns the block number of the latest block in the longest block chain.
47 48 49 |
# File 'lib/bitbank/client.rb', line 47 def block_number request('getblocknumber') end |
#connection_count ⇒ Object
Returns the number of connections to other nodes.
52 53 54 |
# File 'lib/bitbank/client.rb', line 52 def connection_count request('getconnectioncount') end |
#difficulty ⇒ Object
Returns the proof-of-work difficulty as a multiple of the minimum difficulty.
58 59 60 |
# File 'lib/bitbank/client.rb', line 58 def difficulty request('getdifficulty') end |
#get_work(data = nil) ⇒ Object
If data is not specified, returns formatted hash data to work on.
If data is specified, bitcoind will try to solve the block and will return true or false indicating whether or not it was successful.
66 67 68 |
# File 'lib/bitbank/client.rb', line 66 def get_work(data=nil) request('getwork', data) end |
#info ⇒ Object
Returns a hash containing bitcoind status information.
71 72 73 |
# File 'lib/bitbank/client.rb', line 71 def info request('getinfo') end |
#new_account(name) ⇒ Object
Returns a new bitcoin account for receiving payments (along with a new address).
77 78 79 |
# File 'lib/bitbank/client.rb', line 77 def new_account(name) account(name) end |
#new_address(account_name = nil) ⇒ Object
Returns a new bitcoin address for receiving payments.
If an account is specified (recommended), the new address is added to the address book so payments received with the address are credited to the account.
86 87 88 |
# File 'lib/bitbank/client.rb', line 86 def new_address(account_name=nil) request('getnewaddress', account_name) end |
#request(method, *args) ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/bitbank/client.rb', line 110 def request(method, *args) body = { 'id' => 'jsonrpc', 'method' => method } body['params'] = args unless args.empty? || args.first.nil? response_json = RestClient.post(@endpoint, body.to_json) response = JSON.parse(response_json) response['result'] end |
#transactions(account_name = nil, count = 10) ⇒ Object
Returns the most recent transactions for the specified account.
91 92 93 94 95 96 |
# File 'lib/bitbank/client.rb', line 91 def transactions(account_name=nil, count=10) transaction_data = request('listtransactions', account_name, count) transaction_data.map do |txdata| Transaction.new(self, txdata['txid'], txdata) end end |
#validate_address(address, locals_invalid = false) ⇒ Object
Determine if the given address is valid.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/bitbank/client.rb', line 99 def validate_address(address, locals_invalid=false) status = request('validateaddress', address) if locals_invalid && status['ismine'] warn "WARNING: Bitcoin address '#{address}' belongs to local account '#{status['account']}'." return false end status['isvalid'] end |