Class: Web3::Hpb::Rpc
- Inherits:
-
Object
- Object
- Web3::Hpb::Rpc
- Defined in:
- lib/web3/hpb/rpc.rb
Constant Summary collapse
- JSON_RPC_VERSION =
'2.0'
- DEFAULT_CONNECT_OPTIONS =
{ use_ssl: false, open_timeout: 10, read_timeout: 70 }
- DEFAULT_HOST =
'localhost'
- DEFAULT_PORT =
8545
Instance Attribute Summary collapse
-
#hpb ⇒ Object
readonly
Returns the value of attribute hpb.
-
#trace ⇒ Object
readonly
Returns the value of attribute trace.
Instance Method Summary collapse
-
#initialize(host: DEFAULT_HOST, port: DEFAULT_PORT, connect_options: DEFAULT_CONNECT_OPTIONS) ⇒ Rpc
constructor
A new instance of Rpc.
- #request(method, params = nil) ⇒ Object
Constructor Details
#initialize(host: DEFAULT_HOST, port: DEFAULT_PORT, connect_options: DEFAULT_CONNECT_OPTIONS) ⇒ Rpc
Returns a new instance of Rpc.
21 22 23 24 25 26 27 28 29 |
# File 'lib/web3/hpb/rpc.rb', line 21 def initialize(host: DEFAULT_HOST, port:DEFAULT_PORT, connect_options: DEFAULT_CONNECT_OPTIONS) @client_id = Random.rand(1000_000) @uri = URI(([:use_ssl] ? 'https' : 'http') + "://#{host}:#{port}#{[:rpc_path]}") @connect_options = @hpb = HpbModule.new(self) @trace = TraceModule.new(self) end |
Instance Attribute Details
#hpb ⇒ Object (readonly)
Returns the value of attribute hpb.
19 20 21 |
# File 'lib/web3/hpb/rpc.rb', line 19 def hpb @hpb end |
#trace ⇒ Object (readonly)
Returns the value of attribute trace.
19 20 21 |
# File 'lib/web3/hpb/rpc.rb', line 19 def trace @trace end |
Instance Method Details
#request(method, params = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/web3/hpb/rpc.rb', line 31 def request(method, params = nil) Net::HTTP.start(@uri.host, @uri.port, @connect_options) do |http| request = Net::HTTP::Post.new(@uri, {"Content-Type": "application/json"}) request.body = {jsonrpc: JSON_RPC_VERSION, method: method, params: params, id: @client_id}.compact.to_json response = http.request(request) raise "Error code #{response.code} on request #{@uri.to_s} #{request.body}" unless response.kind_of? Net::HTTPOK body = JSON.parse(response.body) if body["result"] body["result"] elsif body["error"] raise "Error #{@uri.to_s} #{body['error']} on request #{@uri.to_s} #{request.body}" else raise "No response on request #{@uri.to_s} #{request.body}" end end end |