bit_wallet 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -19,15 +19,32 @@ Or install it yourself as:
19
19
  ## Usage
20
20
 
21
21
  wallet = BitWallet.new(:username => 'username', :password => 'password')
22
+ wallet.accounts.with_balance # returns array of the accounts with balance > 0
22
23
  account = wallet.accounts.new('account name')
23
24
  account.addresses.count # 1, as it already comes with an address
24
25
  account.balance # returns the balance of the account
25
26
  address = account.addresses.new
26
27
  address.address # 8hdsakdjh82d9327ccb64642c - the address hash
27
28
  account.send_amount 5.5, to: '8hdsakdjh82d9327ccb64642c' # sends 5.5 bitcoin to the address
29
+ account.send_amount 5.5, to: address # sends 5.5 bitcoin to the BitWallet::Address#address
28
30
  account.total_received # returns the total amount received by the account
31
+ account.transactions # returns array of 10 BitWallet::Transaction
32
+ account.transactions(limit: 5) # returns array of 5 BitWallet::Transaction
29
33
  address.total_received # returns the total amount received by the address
30
34
 
35
+ ### Transaction
36
+
37
+ A transaction has the following methods:
38
+
39
+ - `account`: the account it belongs to
40
+ - `address`: the address it belongs to
41
+ - `amount`: how much was transferred
42
+ - `category`: returns the category value of the transaction
43
+ - `confirmation`: how many times this has been confirmed by the network
44
+ - `id`: the transaction id
45
+ - `occurred_at`: Ruby Time object for the `time` value returned by bitcoind
46
+ - `received_at`: Ruby Time object for the `timereceived` value returned by bitcoind
47
+
31
48
  ## Contributing
32
49
 
33
50
  1. Fork it
data/bit_wallet.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |gem|
22
22
  gem.add_development_dependency 'rspec', '2.12.0'
23
23
  gem.add_development_dependency 'pry'
24
24
  gem.add_development_dependency 'factory_girl', '4.2.0'
25
+ gem.add_development_dependency 'bitcoin_testnet', '0.0.1'
25
26
  end
@@ -19,9 +19,12 @@ module BitWallet
19
19
  end
20
20
 
21
21
  def send_amount(amount, options={})
22
- unless options[:to]
22
+ if options[:to]
23
+ options[:to] = options[:to].address if options[:to].is_a?(Address)
24
+ else
23
25
  fail ArgumentError, 'address must be specified'
24
26
  end
27
+
25
28
  client.sendfrom(self.name,
26
29
  options[:to],
27
30
  amount,
@@ -34,6 +37,15 @@ module BitWallet
34
37
  client.getreceivedbyaccount(self.name, BitWallet.config.min_conf)
35
38
  end
36
39
 
40
+ def ==(other_account)
41
+ self.name == other_account.name
42
+ end
43
+
44
+ def recent_transactions(options={})
45
+ count = options.delete(:limit) || 10
46
+ client.listtransactions self.name, count
47
+ end
48
+
37
49
  private
38
50
 
39
51
  def parse_error(response)
@@ -4,6 +4,10 @@ module BitWallet
4
4
  attr_reader :wallet
5
5
  delegate :client, to: :wallet
6
6
 
7
+ def with_balance
8
+ self.select { |account| account.balance > 0 }
9
+ end
10
+
7
11
  def initialize(wallet)
8
12
  @wallet = wallet
9
13
 
@@ -18,5 +18,9 @@ module BitWallet
18
18
  client.getreceivedbyaddress(self.address, BitWallet.config.min_conf)
19
19
  end
20
20
 
21
+ def ==(other_address)
22
+ self.address == other_address.address
23
+ end
24
+
21
25
  end
22
26
  end
@@ -0,0 +1,32 @@
1
+ module BitWallet
2
+ class Transaction
3
+
4
+ attr_reader(:account,
5
+ :amount,
6
+ :category,
7
+ :confirmations,
8
+ :id,
9
+ :occurred_at,
10
+ :received_at)
11
+
12
+ def initialize(wallet, args)
13
+ args = args.with_indifferent_access
14
+ @wallet = wallet
15
+ @account = wallet.accounts.new(args[:account])
16
+ @id = args[:txid]
17
+ @address_str = args[:address]
18
+ @amount = args[:amount]
19
+ @confirmations = args[:confirmations]
20
+ @occurred_at = Time.at(args[:time])
21
+ @received_at = Time.at(args[:timereceived])
22
+ @category = args[:category]
23
+ end
24
+
25
+ def address
26
+ @address ||= @account.addresses.find do |address|
27
+ address.address == @address_str
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module BitWallet
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -9,6 +9,13 @@ module BitWallet
9
9
  @accounts ||= Accounts.new(self)
10
10
  end
11
11
 
12
+ def recent_transactions(options={})
13
+ count = options.delete(:limit) || 10
14
+ client.listtransactions(nil, count).map do |hash|
15
+ Transaction.new self, hash
16
+ end
17
+ end
18
+
12
19
  private
13
20
 
14
21
  def client
data/lib/bit_wallet.rb CHANGED
@@ -11,6 +11,7 @@ require 'bit_wallet/account'
11
11
  require 'bit_wallet/accounts'
12
12
  require 'bit_wallet/addresses'
13
13
  require 'bit_wallet/address'
14
+ require 'bit_wallet/transaction'
14
15
  require 'bit_wallet/errors'
15
16
 
16
17
  module BitWallet
@@ -67,6 +67,18 @@ describe BitWallet::Account do
67
67
  nona_account.balance.should == expected_balance
68
68
  end
69
69
 
70
+ context ':to is a BitWallet::Address' do
71
+ it 'should send it to the #address of the given BitWallet::Address' do
72
+ default_account = wallet.accounts.new('')
73
+ nona_account = wallet.accounts.new('nona')
74
+ nona_address = nona_account.addresses.first
75
+
76
+ expected_balance = nona_account.balance + 5.2
77
+ default_account.send_amount 5.2, to: nona_address
78
+ nona_account.balance.should == expected_balance
79
+ end
80
+ end
81
+
70
82
  context 'account does not have enough money' do
71
83
  it 'should fail with the InsufficientFunds error' do
72
84
  default_account = wallet.accounts.new('')
@@ -89,4 +101,32 @@ describe BitWallet::Account do
89
101
  end
90
102
  end
91
103
 
104
+ describe '#recent_transactions' do
105
+ it 'should default to list 10 transactions' do
106
+ wallet = build(:wallet)
107
+ default_account = wallet.accounts.new('')
108
+ account_1 = wallet.accounts.new('1')
109
+
110
+ 1.upto(11).each do |n|
111
+ default_account.send_amount n, to: account_1.addresses.first
112
+ end
113
+
114
+ account_1.recent_transactions.size.should == 10
115
+ end
116
+
117
+ context 'when transaction limit is 5' do
118
+ it 'should list the 5 most recent transactions' do
119
+ wallet = build(:wallet)
120
+ default_account = wallet.accounts.new('')
121
+ account_1 = wallet.accounts.new('1')
122
+
123
+ 1.upto(6).each do |n|
124
+ default_account.send_amount n, to: account_1.addresses.first
125
+ end
126
+
127
+ account_1.recent_transactions(limit: 5).size.should == 5
128
+ end
129
+ end
130
+ end
131
+
92
132
  end
@@ -27,4 +27,23 @@ describe BitWallet::Accounts do
27
27
  end
28
28
  end
29
29
 
30
+ describe '.with_balance' do
31
+ it 'should return accounts with a balance > 0' do
32
+ default_account = subject.new('')
33
+ account_1 = subject.new('nomoney')
34
+ account_2 = subject.new('moneyd')
35
+
36
+ default_account.send_amount 10, to: account_2.addresses.first
37
+
38
+ accounts_with_balance = subject.with_balance
39
+ accounts_with_balance.should include(default_account)
40
+ accounts_with_balance.should_not include(account_1)
41
+ accounts_with_balance.should include(account_2)
42
+
43
+ account_1.send_amount account_1.balance, to: default_account.addresses.first
44
+
45
+ subject.with_balance.should_not include(account_1)
46
+ end
47
+ end
48
+
30
49
  end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe BitWallet::Transaction do
4
+
5
+ describe 'on initialization' do
6
+ it 'should be able to take a bitcoind hash' do
7
+ wallet = build(:wallet)
8
+ account = wallet.accounts.new('numba')
9
+ address = account.addresses.new('mi3G43CcN')
10
+ args = {"account" => 'numba',
11
+ "address" => "mi3G43CcN",
12
+ "category" => "receive",
13
+ "amount" => 1.5,
14
+ "confirmations" => 0,
15
+ "txid" => "a363e027",
16
+ "time" => 1362239334,
17
+ "timereceived" => 1362239346}
18
+ transaction = described_class.new(wallet, args)
19
+ transaction.account.should == account
20
+ transaction.address.should == address
21
+ transaction.category.should == 'receive'
22
+ transaction.amount.should == 1.5
23
+ transaction.confirmations.should == 0
24
+ transaction.id.should == 'a363e027'
25
+ transaction.occurred_at.to_i.should == 1362239334
26
+ transaction.received_at.to_i.should == 1362239346
27
+ end
28
+ end
29
+
30
+ end
@@ -9,4 +9,31 @@ describe BitWallet::Wallet do
9
9
  end
10
10
  end
11
11
 
12
+ describe '#recent_transactions' do
13
+ it 'should return the most recent transactions of all accounts defaulting to 10 transactions' do
14
+ wallet = build(:wallet)
15
+ default_account = wallet.accounts.new('')
16
+ account_1 = wallet.accounts.new('1')
17
+
18
+ 1.upto(11).each do |n|
19
+ default_account.send_amount n, to: account_1.addresses.first
20
+ end
21
+
22
+ wallet.recent_transactions.size.should == 10
23
+ end
24
+
25
+ it 'should allow overriding of the transaction limit' do
26
+ pending "Fix when bitcoin-client this is resolved: https://github.com/sinisterchipmunk/bitcoin-client/issues/4"
27
+ wallet = build(:wallet)
28
+ default_account = wallet.accounts.new('')
29
+ account_1 = wallet.accounts.new('1')
30
+
31
+ 1.upto(11).each do |n|
32
+ default_account.send_amount n, to: account_1.addresses.first
33
+ end
34
+
35
+ wallet.recent_transactions(limit: 5).size.should == 5
36
+ end
37
+ end
38
+
12
39
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'bundler/setup'
2
2
  APP_ENV = 'test'
3
- Bundler.require(APP_ENV)
3
+ Bundler.require(:default, APP_ENV)
4
4
 
5
5
  require 'bit_wallet'
6
6
  require 'ostruct'
@@ -9,6 +9,7 @@ require 'active_support/core_ext/hash/slice'
9
9
  require 'pry'
10
10
  require 'factory_girl'
11
11
  require_relative 'factories'
12
+ require 'bitcoin_testnet'
12
13
 
13
14
  Config = {}
14
15
  yaml_config_file = File.open(File.join(File.dirname(__FILE__), 'config.yml'))
@@ -16,22 +17,14 @@ yaml_config = YAML.load(yaml_config_file).with_indifferent_access
16
17
  Config[:username] = yaml_config[:rpcuser]
17
18
  Config[:port] = yaml_config[:rpcport]
18
19
  Config[:password] = yaml_config[:rpcpassword]
19
- TESTNET_DIR = File.join(File.dirname(__FILE__), 'testnet')
20
+
21
+ BitcoinTestnet.configure_rspec!
22
+ BitcoinTestnet.dir = File.join(File.dirname(__FILE__), 'testnet')
20
23
 
21
24
  RSpec.configure do |config|
22
25
  config.include FactoryGirl::Syntax::Methods
23
- config.before(:suite) do
24
- # Ensure that the testnet is running.
25
- bitcoin_processes = `ps -ef | grep bitcoin | grep -v grep`.split("\n")
26
- unless bitcoin_processes.size == 2
27
- fail "Please make sure the testnet has started. Run `cd #{TESTNET_DIR} && make start`"
28
- end
29
- end
30
26
 
31
27
  config.before(:each) do
32
- system("cd #{TESTNET_DIR} && make clean > /dev/null")
33
-
34
- # Do not wait for confirmations in tests -- too long!
35
28
  BitWallet.config.min_conf = 0
36
29
  end
37
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bit_wallet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-01 00:00:00.000000000 Z
12
+ date: 2013-03-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bitcoin-client
@@ -91,6 +91,22 @@ dependencies:
91
91
  - - '='
92
92
  - !ruby/object:Gem::Version
93
93
  version: 4.2.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: bitcoin_testnet
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - '='
100
+ - !ruby/object:Gem::Version
101
+ version: 0.0.1
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - '='
108
+ - !ruby/object:Gem::Version
109
+ version: 0.0.1
94
110
  description: Ruby-esque handling of Bitcoin wallet
95
111
  email:
96
112
  - ramon.tayag@gmail.com
@@ -110,6 +126,8 @@ files:
110
126
  - lib/bit_wallet/accounts.rb
111
127
  - lib/bit_wallet/address.rb
112
128
  - lib/bit_wallet/addresses.rb
129
+ - lib/bit_wallet/errors.rb
130
+ - lib/bit_wallet/transaction.rb
113
131
  - lib/bit_wallet/version.rb
114
132
  - lib/bit_wallet/wallet.rb
115
133
  - rvmrc.sample
@@ -117,7 +135,7 @@ files:
117
135
  - spec/bit_wallet/accounts_spec.rb
118
136
  - spec/bit_wallet/address_spec.rb
119
137
  - spec/bit_wallet/addresses_spec.rb
120
- - spec/bit_wallet/errors.rb
138
+ - spec/bit_wallet/transaction_spec.rb
121
139
  - spec/bit_wallet/wallet_spec.rb
122
140
  - spec/bit_wallet_spec.rb
123
141
  - spec/config.yml
@@ -152,7 +170,7 @@ test_files:
152
170
  - spec/bit_wallet/accounts_spec.rb
153
171
  - spec/bit_wallet/address_spec.rb
154
172
  - spec/bit_wallet/addresses_spec.rb
155
- - spec/bit_wallet/errors.rb
173
+ - spec/bit_wallet/transaction_spec.rb
156
174
  - spec/bit_wallet/wallet_spec.rb
157
175
  - spec/bit_wallet_spec.rb
158
176
  - spec/config.yml
File without changes