Class: Web3::Eth::Rpc
- Inherits:
-
Object
- Object
- Web3::Eth::Rpc
- Defined in:
- lib/web3ethereum/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
-
#eth ⇒ Object
readonly
Returns the value of attribute eth.
-
#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.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/web3ethereum/rpc.rb', line 22 def initialize host: DEFAULT_HOST, port: DEFAULT_PORT, connect_options: DEFAULT_CONNECT_OPTIONS @client_id = Random.rand 10000000 @uri = URI(([:use_ssl] ? 'https' : 'http')+ "://#{host}:#{port}#{[:rpc_path]}") @connect_options = @eth = EthModule.new self @trace = TraceModule.new self end |
Instance Attribute Details
#eth ⇒ Object (readonly)
Returns the value of attribute eth.
20 21 22 |
# File 'lib/web3ethereum/rpc.rb', line 20 def eth @eth end |
#trace ⇒ Object (readonly)
Returns the value of attribute trace.
20 21 22 |
# File 'lib/web3ethereum/rpc.rb', line 20 def trace @trace end |
Instance Method Details
#request(method, params = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/web3ethereum/rpc.rb', line 35 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 |