Skip to content

Commit 89a34bd

Browse files
committed
allow rpc creds to be set via config
1 parent 4d27b14 commit 89a34bd

5 files changed

Lines changed: 74 additions & 23 deletions

File tree

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,27 @@ Or install it yourself as:
2727

2828
## Authentication
2929

30-
RPC server login credentials (registered account name and password) must be stored in the following environment variables:-
30+
RPC server login credentials (registered account name and password) must be stored in the following environment variables or set in the configuation (see below):-
3131

3232
`$BITSHARES_ACCOUNT`
3333

3434
`$BITSHARES_PASSWORD`
3535

3636
## Configuration
3737

38+
Config settings may be set via a hash or Yaml file.
39+
40+
**Via a hash**
41+
42+
RPC server login credentials may be set as follows:
43+
```Ruby
44+
Bitshares.configure(:rpc_username => 'whatever')
45+
Bitshares.configure(:rpc_password => 'whatever')
46+
```
47+
3848
The Gem allows multiple wallet names and passwords to be stored so that actions requiring these data may be automated.
3949
To use this functionality - i.e. with the Wallet class (see below) wallet names and passwords must be configured in either of the following ways:
4050

41-
**Via a hash**
4251
```Ruby
4352
Bitshares.configure(:wallet => {'wallet1' => 'password1'})
4453
Bitshares.configure(:wallet => {'wallet2' => 'password2'})
@@ -177,7 +186,7 @@ cny_bts_trader.order_list # lists orders for the account and market - optional l
177186
cny_bts_trader.submit_bid(quantity, price) # buy <quantity> of Market base (BTS here) at <price> (quote/base)
178187
cny_bts_trader.submit_ask(quantity, price) # sell <quantity> of Market base (BTS here) at <price> (quote/base)
179188
# both return respective order id
180-
189+
181190
cny_bts_trader.cancel_orders(*order_ids) # cancels one or more orders for the account and market
182191
# returns array of memo's e.g. 'cancel ASK-90189b6e'
183192
```

lib/bitshares.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
# stackoverflow.com/questions/6233124/where-to-place-access-config-file-in-gem
1919
module Bitshares
2020

21-
@config = {:wallet => {nil => nil}} # name/password key/value pairs
21+
@config = {rpc_username: nil, rpc_password: nil,
22+
:wallet => {nil => nil}} # name/password key/value pairs
2223

2324
@valid_keys = @config.keys
2425

lib/bitshares/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class Err < RuntimeError; end
66

77
def self.init
88
bitshares_running?
9-
user = ENV['BITSHARES_ACCOUNT']
10-
password = ENV['BITSHARES_PASSWORD']
9+
user = ENV['BITSHARES_ACCOUNT'] || Bitshares.config[:rpc_username]
10+
password = ENV['BITSHARES_PASSWORD'] || Bitshares.config[:rpc_password]
1111
@uri = URI("http://localhost:#{rpc_http_port}/rpc")
1212
@req = Net::HTTP::Post.new(@uri)
1313
@req.content_type = 'application/json'

lib/bitshares/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Bitshares
2-
VERSION = '0.1.5'
2+
VERSION = '0.1.6'
33
end

spec/client_spec.rb

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
describe Bitshares::Client do
66

7+
before do
8+
Bitshares.configure(:rpc_username => ENV['BITSHARES_ACCOUNT'])
9+
Bitshares.configure(:rpc_password => ENV['BITSHARES_PASSWORD'])
10+
end
11+
712
let(:client) { CLIENT }
813

914
context '#new' do
@@ -31,26 +36,62 @@
3136
end
3237

3338
context '#rpc_request' do
34-
it 'with invalid username raise Bitshares::Client::Err "Bad credentials"' do
35-
stub_const('ENV', ENV.to_hash.merge('BITSHARES_ACCOUNT' => 'wrong_password'))
36-
Bitshares::Client.init
37-
expect(->{client.get_info}).to raise_error Bitshares::Client::Err, 'Bad credentials'
38-
end
39+
context 'using ENV credentials' do
40+
it 'with invalid username raises Bitshares::Client::Err "Bad credentials"' do
41+
stub_const('ENV', ENV.to_hash.merge('BITSHARES_ACCOUNT' => 'wrong_username'))
42+
Bitshares::Client.init
43+
expect(->{client.get_info}).to raise_error Bitshares::Client::Err, 'Bad credentials'
44+
end
3945

40-
it 'with invalid password raise Bitshares::Client::Err "Bad credentials"' do
41-
stub_const('ENV', ENV.to_hash.merge('BITSHARES_PASSWORD' => 'wrong_password'))
42-
Bitshares::Client.init
43-
expect(->{client.get_info}).to raise_error Bitshares::Client::Err, 'Bad credentials'
44-
end
46+
it 'with invalid password raises Bitshares::Client::Err "Bad credentials"' do
47+
stub_const('ENV', ENV.to_hash.merge('BITSHARES_PASSWORD' => 'wrong_password'))
48+
Bitshares::Client.init
49+
expect(->{client.get_info}).to raise_error Bitshares::Client::Err, 'Bad credentials'
50+
end
4551

46-
it 'with valid credentials and invalid client command raises Bitshares::Client::Err' do
47-
Bitshares::Client.init
48-
expect(->{client.not_a_cmd}).to raise_error Bitshares::Client::Err
52+
it 'with valid credentials and invalid client command raises Bitshares::Client::Err' do
53+
Bitshares::Client.init
54+
expect(->{client.not_a_cmd}).to raise_error Bitshares::Client::Err
55+
end
56+
57+
it 'with valid credentials and valid client command returns a Hash of returned JSON data' do
58+
Bitshares::Client.init
59+
expect(client.get_info.class).to eq Hash
60+
end
4961
end
5062

51-
it 'with valid credentials and valid client command returns a Hash of returned JSON data' do
52-
Bitshares::Client.init
53-
expect(client.get_info.class).to eq Hash
63+
context 'using config credentials' do
64+
65+
it 'with invalid username raises Bitshares::Client::Err "Bad credentials"' do
66+
Bitshares.configure(:rpc_username => 'wrong_username')
67+
stub_const('ENV', ENV.to_hash.merge('BITSHARES_ACCOUNT' => nil))
68+
stub_const('ENV', ENV.to_hash.merge('BITSHARES_PASSWORD' => nil))
69+
Bitshares::Client.init
70+
expect(->{client.get_info}).to raise_error Bitshares::Client::Err, 'Bad credentials'
71+
end
72+
73+
it 'with invalid password raises Bitshares::Client::Err "Bad credentials"' do
74+
Bitshares.configure(:rpc_password => 'wrong_password')
75+
stub_const('ENV', ENV.to_hash.merge('BITSHARES_ACCOUNT' => nil))
76+
stub_const('ENV', ENV.to_hash.merge('BITSHARES_PASSWORD' => nil))
77+
Bitshares::Client.init
78+
expect(->{client.get_info}).to raise_error Bitshares::Client::Err, 'Bad credentials'
79+
end
80+
81+
it 'with valid credentials and invalid client command raises Bitshares::Client::Err' do
82+
stub_const('ENV', ENV.to_hash.merge('BITSHARES_ACCOUNT' => nil))
83+
stub_const('ENV', ENV.to_hash.merge('BITSHARES_PASSWORD' => nil))
84+
Bitshares::Client.init
85+
expect(->{client.not_a_cmd}).to raise_error Bitshares::Client::Err
86+
end
87+
88+
it 'with valid credentials and valid client command returns a Hash of returned JSON data' do
89+
stub_const('ENV', ENV.to_hash.merge('BITSHARES_ACCOUNT' => nil))
90+
stub_const('ENV', ENV.to_hash.merge('BITSHARES_PASSWORD' => nil))
91+
Bitshares::Client.init
92+
expect(client.get_info.class).to eq Hash
93+
end
94+
5495
end
5596
end
5697

0 commit comments

Comments
 (0)