Class: Bitbank::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bitbank/client.rb

Instance Method Summary collapse

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

#accountsObject



9
10
11
12
13
14
# File 'lib/bitbank/client.rb', line 9

def accounts
   = request('listaccounts')
  .map do |, |
    Account.new(self, , )
  end
end

#balance(account_name = nil) ⇒ Object



16
17
18
# File 'lib/bitbank/client.rb', line 16

def balance(=nil)
  request('getbalance', )
end

#block_countObject

Returns the number of blocks in the longest block chain.



21
22
23
# File 'lib/bitbank/client.rb', line 21

def block_count
  request('getblockcount')
end

#difficultyObject

Returns the proof-of-work difficulty as a multiple of the minimum difficulty.



26
27
28
# File 'lib/bitbank/client.rb', line 26

def difficulty
  request('getdifficulty')
end

#get_work(data = nil) ⇒ Object



30
31
32
# File 'lib/bitbank/client.rb', line 30

def get_work(data=nil)
  request('getwork', data)
end

#infoObject



34
35
36
# File 'lib/bitbank/client.rb', line 34

def info
  request('getinfo')
end

#new_address(account_name = nil) ⇒ Object



38
39
40
# File 'lib/bitbank/client.rb', line 38

def new_address(=nil)
  request('getnewaddress', )
end

#request(method, *args) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/bitbank/client.rb', line 49

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



42
43
44
45
46
47
# File 'lib/bitbank/client.rb', line 42

def transactions(=nil, count=10)
  transaction_data = request('listtransactions', , count)
  transaction_data.map do |txdata|
    Transaction.new(self, txdata['txid'], txdata)
  end
end