class NanoRpc::Node
Constants
- DEFAULT_TIMEOUT
Attributes
host[R]
port[R]
Public Class Methods
new( host: 'localhost', port: 7076, auth: nil, headers: nil, timeout: DEFAULT_TIMEOUT )
click to toggle source
Calls superclass method
NanoRpc::Proxy::new
# File lib/nano_rpc/node.rb, line 20 def initialize( host: 'localhost', port: 7076, auth: nil, headers: nil, timeout: DEFAULT_TIMEOUT ) @host = host @port = port @auth = auth @headers = headers @timeout = timeout @node = self super end
Public Instance Methods
call(action, params = {})
click to toggle source
# File lib/nano_rpc/node.rb, line 37 def call(action, params = {}) args = { action: action } args.merge!(params) if params.is_a?(Hash) args = extract_proxy_args(args) rpc_post(args) end
inspect()
click to toggle source
# File lib/nano_rpc/node.rb, line 44 def inspect "#{inspect_prefix}, @url=\"#{@host}:#{port}\">" end
Private Instance Methods
ensure_valid_response(data)
click to toggle source
# File lib/nano_rpc/node.rb, line 102 def ensure_valid_response(data) return unless data['error'] raise NanoRpc::InvalidRequest, "Invalid request: #{data['error']}" end
extract_proxy_args(args)
click to toggle source
# File lib/nano_rpc/node.rb, line 50 def extract_proxy_args(args) args.each do |k, v| m = proxy_method_name(v) args[k] = v.send(m) if m end args end
headers()
click to toggle source
# File lib/nano_rpc/node.rb, line 75 def headers h = @headers || {} h['Content-Type'] = 'json' h['Authorization'] = @auth if @auth h end
http_post(params)
click to toggle source
# File lib/nano_rpc/node.rb, line 86 def http_post(params) HTTP.timeout(@timeout).post(url, headers: headers, body: params.to_json) rescue HTTP::ConnectionError raise NanoRpc::NodeConnectionFailure, "Node unreachable at #{url}" rescue HTTP::TimeoutError raise NanoRpc::NodeTimeout, "Node timeout after #{@timeout} seconds" end
node_response(params)
click to toggle source
# File lib/nano_rpc/node.rb, line 82 def node_response(params) JSON[http_post(params).body.to_s] end
proxy_method_name(obj)
click to toggle source
# File lib/nano_rpc/node.rb, line 58 def proxy_method_name(obj) if obj.is_a?(NanoRpc::Wallet) :id elsif obj.is_a?(NanoRpc::Accounts) :addresses elsif obj.is_a?(NanoRpc::Account) :address end end
rpc_post(params)
click to toggle source
# File lib/nano_rpc/node.rb, line 68 def rpc_post(params) data = NanoRpc::Response.new(node_response(params)) ensure_valid_response(data) data end
url()
click to toggle source
# File lib/nano_rpc/node.rb, line 94 def url if host.start_with?('http://', 'https://') "#{host}:#{port}" else "http://#{host}:#{port}" end end