raiblocks_rpc 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,130 +0,0 @@
1
- # frozen_string_literal: true
2
- class RaiblocksRpc::Proxy
3
- attr_accessor :params, :param_signature, :m
4
-
5
- private
6
-
7
- def proxy_methods
8
- raise 'Child class must override `proxy_methods` method'
9
- end
10
-
11
- def proxy_params
12
- {}
13
- end
14
-
15
- # Define proxy methods based on #proxy_methods
16
- def method_missing(m, *args, &_block)
17
- set_accessors(m)
18
- if valid_proxy_method?
19
- define_proxy_method(m)
20
- return send(m, args.first)
21
- end
22
-
23
- super
24
- end
25
-
26
- def respond_to_missing?(m, include_private = false)
27
- set_accessors(m)
28
- valid_proxy_method? || super
29
- end
30
-
31
- # Valid proxy methods are:
32
- # (1) The raw name of an `action` as passed to the RPC server
33
- # (e.g. `account_balance`)
34
- # (2) An abbreviation of an `action` such as `balance` from `account_balance`
35
- # where `account` is the lowercase name of the encapsulating class.
36
- def valid_proxy_method?
37
- rpc_action? || rpc_action_abbrev?
38
- end
39
-
40
- def rpc_action
41
- return method_expansion if rpc_action_abbrev?
42
- m
43
- end
44
-
45
- def rpc_action?
46
- proxy_method_names.include?(m)
47
- end
48
-
49
- def rpc_action_abbrev?
50
- proxy_method_names.each do |proxy_m|
51
- proxy_m = proxy_m.to_s
52
- next unless proxy_m.start_with?(action_prefix)
53
- return true if m.to_s == action_abbrev(proxy_m)
54
- end
55
-
56
- false
57
- end
58
-
59
- def action_abbrev(proxy_m)
60
- proxy_m.to_s[action_prefix.size..-1]
61
- end
62
-
63
- def method_expansion
64
- "#{action_prefix}#{m}"
65
- end
66
-
67
- def proxy_method_names
68
- @proxy_method_names ||= proxy_methods.keys
69
- end
70
-
71
- def action_prefix
72
- @action_prefix ||= self.class.name.split('::').last.downcase + '_'
73
- end
74
-
75
- def define_proxy_method(m)
76
- self.class.send(:define_method, m) do |opts = {}|
77
- set_accessors(m, opts)
78
- populate_and_validate_params!
79
- RaiblocksRpc.client.call(rpc_action, params)
80
- end
81
- end
82
-
83
- def set_accessors(m, opts = nil)
84
- self.m = m
85
- self.param_signature = proxy_methods[m.to_sym]
86
- self.params = validate_opts!(opts)
87
- end
88
-
89
- def populate_and_validate_params!
90
- proxy_params.each { |k, v| self.params[k] ||= send(v) }
91
- validate_params!
92
- end
93
-
94
- def validate_params!
95
- return if param_signature.nil?
96
- ensure_required_params!
97
- ensure_no_forbidden_params!
98
- end
99
-
100
- def allowed_params
101
- proxy_params.keys +
102
- (param_signature[:required] || []) +
103
- (param_signature[:optional] || [])
104
- end
105
-
106
- def validate_opts!(opts)
107
- return opts if opts.is_a?(Hash)
108
- return {} if opts.nil?
109
- raise RaiblocksRpc::InvalidParameterType,
110
- 'You must pass a hash to an action method'
111
- end
112
-
113
- def params_keys
114
- params.keys.map(&:to_sym)
115
- end
116
-
117
- def ensure_required_params!
118
- missing_params = param_signature[:required] - params_keys
119
- return unless missing_params.any?
120
- raise RaiblocksRpc::MissingParameters,
121
- "Missing required parameter(s): #{missing_params.join(', ')}"
122
- end
123
-
124
- def ensure_no_forbidden_params!
125
- forbidden_params = params_keys - allowed_params
126
- return unless forbidden_params.any?
127
- raise RaiblocksRpc::ForbiddenParameter,
128
- "Forbidden parameter(s) passed: #{forbidden_params.join(', ')}"
129
- end
130
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'hashie'
3
-
4
- class RaiblocksRpc::Response < Hash
5
- include ::Hashie::Extensions::MergeInitializer
6
- include ::Hashie::Extensions::IndifferentAccess
7
- include ::Hashie::Extensions::MethodAccess
8
-
9
- def initialize(hash = {})
10
- super
11
- coerce_integers
12
- end
13
-
14
- def coerce_integers
15
- merge!(self) do |_k, v|
16
- Integer(v)
17
- rescue ArgumentError
18
- v
19
- end
20
- end
21
- end
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
- module RaiblocksRpc
3
- VERSION = '0.3.0'
4
- end