Class: BitWallet::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/bit_wallet/account.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wallet, name) ⇒ Account

Returns a new instance of Account.



7
8
9
10
11
12
# File 'lib/bit_wallet/account.rb', line 7

def initialize(wallet, name)
  @wallet = wallet
  @name = name
  fail ArgumentError, "account name cannot be nil" if name.nil?
  self.addresses.new
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/bit_wallet/account.rb', line 4

def name
  @name
end

#walletObject (readonly)

Returns the value of attribute wallet.



4
5
6
# File 'lib/bit_wallet/account.rb', line 4

def wallet
  @wallet
end

Instance Method Details

#==(other_account) ⇒ Object



41
42
43
# File 'lib/bit_wallet/account.rb', line 41

def ==()
  self.name == .name
end

#addressesObject



14
15
16
# File 'lib/bit_wallet/account.rb', line 14

def addresses
  @addresses ||= Addresses.new(self)
end

#balance(min_conf = BitWallet.min_conf) ⇒ Object



18
19
20
# File 'lib/bit_wallet/account.rb', line 18

def balance(min_conf=BitWallet.min_conf)
  client.getbalance(self.name, min_conf)
end

#recent_transactions(options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bit_wallet/account.rb', line 45

def recent_transactions(options={})
  count = options.delete(:limit) || 10
  # FIXME: come up with adapters to abstract the differences between
  # bitcoind and blockchain.info API
  api_result = client.listtransactions(self.name, count)
  txs = api_result
  if api_result.is_a?(Hash)
    txs = api_result.with_indifferent_access.fetch(:transactions)
  end
  txs.map do |hash|
    Transaction.new self.wallet, hash
  end
end

#send_amount(amount, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bit_wallet/account.rb', line 22

def send_amount(amount, options={})
  if options[:to]
    options[:to] = options[:to].address if options[:to].is_a?(Address)
  else
    fail ArgumentError, 'address must be specified'
  end

  client.sendfrom(self.name,
                  options[:to],
                  amount,
                  BitWallet.min_conf)
rescue Bitcoin::Errors::RPCError => e
  parse_error e.message
end

#send_many(account_values = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bit_wallet/account.rb', line 59

def send_many(={})
  addresses_values = {}
  .each do |key, value|
    address = key.respond_to?(:address) ? key.address : key
    addresses_values[address] = value
  end

  txid = client.send_many(self.name,
                          addresses_values,
                          BitWallet.min_conf)
  txid
rescue => e
  parse_error e.message
end

#total_receivedObject



37
38
39
# File 'lib/bit_wallet/account.rb', line 37

def total_received
  client.getreceivedbyaccount(self.name, BitWallet.min_conf)
end