nano_rpc 0.4.0 → 0.5.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51594de4d5c25a44368e84aea129bfe04cc01f28654efdd70101a77b86361111
4
- data.tar.gz: 3dcedfce43a868796ea107807a4a196cd4e78d5416ebaae4289d3911fbbb8a88
3
+ metadata.gz: d10073bf6ebd0cca384ce4de513f53f7356bbf289731f8dcdf322496a6089e68
4
+ data.tar.gz: '059b36ad240027dc29f655a9bdb07649b79e7279712472b508baeb09a31e5df1'
5
5
  SHA512:
6
- metadata.gz: 5df2f32a0c02eada1f18af7d460de1d01b1e9cf762091168047855cba46f002dbcda51f42c662ff29a1dbddc31ac14b0dbe1725d9602a57e2726bab4f650f214
7
- data.tar.gz: d0cdd6a9ba34c543f8cd7039abc6e0cc368ea862e2770cfceb2f49c9cb8af538570eea14d870c7dc1aab1012fe359ee92970df819ac5b9a8c0e59351f61b8191
6
+ metadata.gz: 2de3f22e5b894b2e421fe93f5b424b646807b8ec9b1e3fda89108eaaa589a48c1ee46eecaf00f615d593a9d0d48f9fe234118773d686e03c21b732bceae44b3b
7
+ data.tar.gz: cd8780df5f4ce82724aa42c12cc2183c9440e30d3961f5e9a25abe48895c124097f26e0d86b6b7ac5f98dd73357325045a857ee9e734cd2b7a1f46d58029ac21
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Nano RPC
1
+ ![Ruby Nano RPC Logo](https://i.imgur.com/ihmmYcp.png)
2
2
 
3
- ![Ruby Logo](https://i.imgur.com/pTKxwQq.png)
4
-
5
- An RPC wrapper for Nano digitial currency nodes written in Ruby. Arbitrary RPC access is provided along with proxy objects that expose helper methods ([Wiki](https://github.com/jcraigk/ruby_nano_rpc/wiki/Proxy-Object-Reference)).
3
+ Nano RPC is a Ruby wrapper for making Remote Procedure Calls against Nano digitial currency nodes. Arbitrary RPC access is provided along with proxy objects that expose helper methods ([Wiki](https://github.com/jcraigk/ruby_nano_rpc/wiki/Proxy-Object-Reference)).
6
4
 
7
5
  To run a Nano node locally, see [Nano Docker Docs](https://github.com/clemahieu/raiblocks/wiki/Docker-node).
8
6
 
@@ -100,6 +98,10 @@ You can point each proxy object at its own node by passing in a client instance:
100
98
 
101
99
  For a comprehensive guide, see the [Wiki](https://github.com/jcraigk/ruby_nano_rpc/wiki).
102
100
 
101
+ ## Credits
102
+
103
+ Logo created by Andrei Luca ([Twitter](https://twitter.com/lucandrei_))
104
+
103
105
  ## License
104
106
 
105
107
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -25,12 +25,31 @@ module Nano
25
25
  def call(action, params = {})
26
26
  args = { action: action }
27
27
  args.merge!(params) if params.is_a?(Hash)
28
- post(args)
28
+ args = extract_proxy_args(args)
29
+ rpc_post(args)
29
30
  end
30
31
 
31
32
  private
32
33
 
33
- def post(params)
34
+ def extract_proxy_args(args)
35
+ args.each do |k, v|
36
+ m = proxy_method(v)
37
+ args[k] = v.send(m) if m
38
+ end
39
+ args
40
+ end
41
+
42
+ def proxy_method(v)
43
+ if v.is_a?(Nano::Wallet)
44
+ :seed
45
+ elsif v.is_a?(Nano::Accounts)
46
+ :addresses
47
+ elsif v.is_a?(Nano::Account)
48
+ :address
49
+ end
50
+ end
51
+
52
+ def rpc_post(params)
34
53
  response = rest_client_post(url, params)
35
54
  ensure_status_success!(response)
36
55
 
@@ -45,6 +64,9 @@ module Nano
45
64
  rescue Errno::ECONNREFUSED
46
65
  raise Nano::NodeConnectionFailure,
47
66
  "Node connection failure at #{url}"
67
+ rescue RestClient::Exceptions::OpenTimeout
68
+ raise Nano::NodeOpenTimeout,
69
+ 'Node failed to respond in time'
48
70
  end
49
71
 
50
72
  def url
@@ -7,4 +7,5 @@ module Nano
7
7
  class InvalidRequest < Error; end
8
8
  class ForbiddenParameter < Error; end
9
9
  class MissingParameters < Error; end
10
+ class NodeOpenTimeout < Error; end
10
11
  end
@@ -10,10 +10,8 @@ module Nano::AccountHelper
10
10
  account_block_count.block_count
11
11
  end
12
12
 
13
- def history(*args)
14
- account_history(
15
- pluck_argument(args, :count)
16
- ).history
13
+ def history(count:)
14
+ account_history(count: count).history
17
15
  end
18
16
 
19
17
  def info
@@ -26,17 +24,14 @@ module Nano::AccountHelper
26
24
 
27
25
  def move(from:, to:)
28
26
  account_move(
29
- wallet: object_to_value(to),
30
- source: object_to_value(from),
27
+ source: from,
28
+ wallet: to,
31
29
  accounts: [address]
32
30
  ).moved == 1
33
31
  end
34
32
 
35
33
  def wallet_work_set(wallet:, work:)
36
- work_set(
37
- wallet: object_to_value(wallet),
38
- work: work
39
- ).success == ''
34
+ work_set(wallet: wallet, work: work).success == ''
40
35
  end
41
36
 
42
37
  def pending_balance
@@ -53,10 +48,8 @@ module Nano::AccountHelper
53
48
  end
54
49
  alias blocks_pending pending_blocks
55
50
 
56
- def remove(*args)
57
- account_remove(
58
- pluck_argument(args, :wallet)
59
- ).removed == 1
51
+ def remove(wallet:)
52
+ account_remove(wallet: wallet).removed == 1
60
53
  end
61
54
 
62
55
  def representative
@@ -65,7 +58,7 @@ module Nano::AccountHelper
65
58
 
66
59
  def representative_set(wallet:, representative:)
67
60
  account_representative_set(
68
- wallet: object_to_value(wallet),
61
+ wallet: wallet,
69
62
  representative: representative
70
63
  ).set == 1
71
64
  end
@@ -24,15 +24,14 @@ module Nano::AccountsHelper
24
24
  end
25
25
 
26
26
  def move(from:, to:)
27
- account_move(
28
- source: object_to_value(from),
29
- wallet: object_to_value(to)
30
- ).moved == 1
27
+ account_move(source: from, wallet: to).moved == 1
31
28
  end
32
29
 
33
30
  def pending(count:, threshold: nil, source: nil)
34
31
  accounts_pending(
35
- count: count, threshold: threshold, source: source
32
+ count: count,
33
+ threshold: threshold,
34
+ source: source
36
35
  ).blocks
37
36
  end
38
37
  alias pending_blocks pending
@@ -41,7 +40,7 @@ module Nano::AccountsHelper
41
40
  def [](idx)
42
41
  return unless @addresses[idx]
43
42
  @account_objects ||= []
44
- @account_objects[idx] ||= Nano::Account.new(@addresses[idx])
43
+ @account_objects[idx] ||= Nano::Account.new(@addresses[idx], client: client)
45
44
  end
46
45
 
47
46
  def <<(val)
@@ -50,7 +49,7 @@ module Nano::AccountsHelper
50
49
 
51
50
  def each(&_block)
52
51
  @addresses.each do |address|
53
- yield Nano::Account.new(address)
52
+ yield Nano::Account.new(address, client: client)
54
53
  end
55
54
  end
56
55
 
@@ -2,25 +2,6 @@
2
2
  module Nano::ApplicationHelper
3
3
  private
4
4
 
5
- def pluck_argument(args, key, arg_key = nil)
6
- k = arg_key || key
7
- arg = args.first
8
- v = arg.is_a?(Hash) ? arg[key] : arg
9
- { k => object_to_value(v) }
10
- end
11
-
12
- def object_to_value(arg)
13
- if arg.is_a?(Nano::Wallet)
14
- arg.seed
15
- elsif arg.is_a?(Nano::Account)
16
- arg.address
17
- elsif arg.is_a?(Nano::Accounts)
18
- arg.addresses
19
- else
20
- arg
21
- end
22
- end
23
-
24
5
  def inspect_prefix
25
6
  "#<#{self.class}:#{format('0x00%x', object_id << 1)}"
26
7
  end
@@ -2,44 +2,44 @@
2
2
  module Nano::NodeHelper
3
3
  include Nano::ApplicationHelper
4
4
 
5
- def account_containing_block(*args)
6
- block_account(pluck_argument(args, :hash)).account
5
+ def account_containing_block(hash:)
6
+ block_account(hash: hash).account
7
7
  end
8
8
 
9
9
  def create_wallet
10
- Nano::Wallet.new(wallet_create.wallet)
10
+ Nano::Wallet.new(wallet_create.wallet, client: client)
11
11
  end
12
12
 
13
- def knano_from_raw(*args)
14
- krai_from_raw(pluck_argument(args, :amount)).amount
13
+ def knano_from_raw(amount:)
14
+ krai_from_raw(amount: amount).amount
15
15
  end
16
16
 
17
- def knano_to_raw(*args)
18
- krai_to_raw(pluck_argument(args, :amount)).amount
17
+ def knano_to_raw(amount:)
18
+ krai_to_raw(amount: amount).amount
19
19
  end
20
20
 
21
- def mnano_from_raw(*args)
22
- mrai_from_raw(pluck_argument(args, :amount)).amount
21
+ def mnano_from_raw(amount:)
22
+ mrai_from_raw(amount: amount).amount
23
23
  end
24
24
 
25
- def mnano_to_raw(*args)
26
- mrai_to_raw(pluck_argument(args, :amount)).amount
25
+ def mnano_to_raw(amount:)
26
+ mrai_to_raw(amount: amount).amount
27
27
  end
28
28
 
29
- def nano_from_raw(*args)
30
- rai_from_raw(pluck_argument(args, :amount)).amount
29
+ def nano_from_raw(amount:)
30
+ rai_from_raw(amount: amount).amount
31
31
  end
32
32
 
33
- def nano_to_raw(*args)
34
- rai_to_raw(pluck_argument(args, :amount)).amount
33
+ def nano_to_raw(amount:)
34
+ rai_to_raw(amount: amount).amount
35
35
  end
36
36
 
37
37
  def num_frontiers
38
38
  frontier_count['count']
39
39
  end
40
40
 
41
- def pending_exists?(*args)
42
- pending_exists(pluck_argument(args, :hash)).exists == 1
41
+ def pending_exists?(hash:)
42
+ pending_exists(hash: hash).exists == 1
43
43
  end
44
44
 
45
45
  def total_supply
@@ -2,13 +2,13 @@
2
2
  module Nano::WalletHelper
3
3
  include Nano::ApplicationHelper
4
4
 
5
- def account_work(*args)
6
- work_get(pluck_argument(args, :account)).work
5
+ def account_work(account:)
6
+ work_get(account: account).work
7
7
  end
8
8
 
9
9
  def accounts
10
10
  return [] unless account_list.accounts.size.positive?
11
- Nano::Accounts.new(account_list.accounts)
11
+ Nano::Accounts.new(account_list.accounts, client: client)
12
12
  end
13
13
 
14
14
  def add_key(key:, work: true)
@@ -31,36 +31,34 @@ module Nano::WalletHelper
31
31
  payment_begin.account
32
32
  end
33
33
 
34
- def change_password(*args)
35
- password_change(
36
- pluck_argument(args, :new_password, :password)
37
- ).changed == 1
34
+ def change_password(new_password:)
35
+ password_change(password: new_password).changed == 1
38
36
  end
39
37
 
40
- def change_seed(*args)
41
- wallet_change_seed(
42
- pluck_argument(args, :new_seed, :seed)
43
- ).success == ''
38
+ def change_seed(new_seed:)
39
+ wallet_change_seed(seed: new_seed).success == ''
44
40
  end
45
41
 
46
- def contains?(*args)
47
- wallet_contains(pluck_argument(args, :account)).exists == 1
42
+ def contains?(account:)
43
+ wallet_contains(account: account).exists == 1
48
44
  end
49
45
 
50
46
  def create_account(work: true)
51
- Nano::Account.new(account_create(work: work).account)
47
+ address = account_create(work: work).account
48
+ Nano::Account.new(address, client: client)
52
49
  end
53
50
 
54
51
  def create_accounts(count:, work: true)
55
- Nano::Accounts.new(accounts_create(count: count, work: work).accounts)
52
+ addresses = accounts_create(count: count, work: work).accounts
53
+ Nano::Accounts.new(addresses, client: client)
56
54
  end
57
55
 
58
56
  def destroy
59
57
  wallet_destroy
60
58
  end
61
59
 
62
- def enter_password(*args)
63
- password_enter(pluck_argument(args, :password)).valid == 1
60
+ def enter_password(password:)
61
+ password_enter(password: password).valid == 1
64
62
  end
65
63
 
66
64
  def export
@@ -80,15 +78,11 @@ module Nano::WalletHelper
80
78
  end
81
79
 
82
80
  def move_accounts(to:, accounts:)
83
- account_move(
84
- wallet: object_to_value(to),
85
- source: seed,
86
- accounts: object_to_value(accounts)
87
- ).moved == 1
81
+ account_move(wallet: to, source: seed, accounts: accounts).moved == 1
88
82
  end
89
83
 
90
- def password_valid?(*args)
91
- password_valid(pluck_argument(args, :password)).valid == 1
84
+ def password_valid?(password:)
85
+ password_valid(password: password).valid == 1
92
86
  end
93
87
 
94
88
  def pending_balance
@@ -116,42 +110,37 @@ module Nano::WalletHelper
116
110
 
117
111
  def receive_block(account:, block:)
118
112
  receive(
119
- account: object_to_value(account),
113
+ account: account,
120
114
  block: block
121
115
  ).block
122
116
  end
123
117
 
124
- def remove_account(*args)
125
- account_remove(pluck_argument(args, :account)).removed == 1
118
+ def remove_account(account:)
119
+ account_remove(account: account).removed == 1
126
120
  end
127
121
 
128
122
  def representative
129
123
  wallet_representative.representative
130
124
  end
131
125
 
132
- def republish(*args)
133
- wallet_republish(pluck_argument(args, :count)).blocks
126
+ def republish(count:)
127
+ wallet_republish(count: count).blocks
134
128
  end
135
129
 
136
130
  def account_work_set(account:, work:)
137
- work_set(
138
- account: object_to_value(account),
139
- work: work
140
- ).success == ''
131
+ work_set(account: account, work: work).success == ''
141
132
  end
142
133
  alias set_account_work account_work_set
143
134
 
144
- def representative_set(*args)
145
- wallet_representative_set(
146
- pluck_argument(args, :representative)
147
- ).set == 1
135
+ def representative_set(representative:)
136
+ wallet_representative_set(representative: representative).set == 1
148
137
  end
149
138
  alias set_representative representative_set
150
139
 
151
140
  def send_nano(from:, to:, amount:, work: nil)
152
141
  send_currency(
153
- source: object_to_value(from),
154
- destination: object_to_value(to),
142
+ source: from,
143
+ destination: to,
155
144
  amount: amount,
156
145
  work: work
157
146
  ).block
@@ -37,6 +37,7 @@ class Nano::Account
37
37
  proxy_method :validate_account_number
38
38
  proxy_method :pending, required: %i[count], optional: %i[threshold exists]
39
39
  proxy_method :payment_wait, required: %i[amount timeout]
40
- proxy_method :work_get
40
+ proxy_method :receive, required: %i[wallet block], optional: %i[work]
41
+ proxy_method :work_get, required: %i[wallet]
41
42
  proxy_method :work_set
42
43
  end
@@ -50,6 +50,6 @@ class Nano::Wallet
50
50
  proxy_method :wallet_representative_set, required: %i[representative]
51
51
  proxy_method :wallet_republish, required: %i[count]
52
52
  proxy_method :wallet_work_get
53
- proxy_method :work_get
53
+ proxy_method :work_get, required: %i[account]
54
54
  proxy_method :work_set
55
55
  end
@@ -6,7 +6,7 @@ module Nano::Proxy
6
6
 
7
7
  def initialize(opts = {})
8
8
  @client = opts[:client] || Nano.client
9
- self.class.proxy_methods.each { |m| define_proxy_method(m) }
9
+ self.class.proxy_methods&.each { |m| define_proxy_method(m) }
10
10
  end
11
11
 
12
12
  def self.included(base)
@@ -25,7 +25,7 @@ module Nano::Proxy
25
25
  end
26
26
 
27
27
  def proxy_methods
28
- proxy_method_def.keys.sort
28
+ proxy_method_def&.keys&.sort
29
29
  end
30
30
  end
31
31
 
@@ -36,24 +36,15 @@ module Nano::Proxy
36
36
  private
37
37
 
38
38
  def define_proxy_method(m)
39
- self.class.send(:define_method, method_alias(m)) do |*args|
39
+ self.class.send(:define_method, method_alias(m)) do |args = {}|
40
40
  @m = m
41
- @call_args = hashify_args(args)
41
+ @call_args = args
42
42
 
43
43
  validate_params!
44
44
  execute_call
45
45
  end
46
46
  end
47
47
 
48
- def hashify_args(args)
49
- if args.first.is_a?(Hash)
50
- args.first
51
- else
52
- (args[1].is_a?(Hash) ? args[1] : {})
53
- .merge!(pluck_argument(args, required_params.first))
54
- end
55
- end
56
-
57
48
  def execute_call
58
49
  expose_nested_data(@client.call(@m, @call_args))
59
50
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Nano
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.0'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nano_rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Craig-Kuhn (JCK)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-07 00:00:00.000000000 Z
11
+ date: 2018-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler