Sha256: 41271ffe8e9bc1ca99f8d924372406507e985129f99feb7ec77b4de8a8c2ebff

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

module Etherlite
  class Connection
    include Api::Rpc

    attr_reader :chain_id

    def initialize(_uri, _chain_id = nil)
      @uri = _uri
      @chain_id = _chain_id
    end

    def ipc_call(_method, *_params)
      id = new_unique_id
      payload = { jsonrpc: "2.0", method: _method, params: _params, id: id }

      # TODO: support ipc
      Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: use_ssl?) do |http|
        return handle_response http.post(
          @uri.path.empty? ? '/' : @uri.path,
          payload.to_json,
          "Content-Type" => "application/json"
        ), id
      end
    end

    private

    def new_unique_id
      (Time.now.to_f * 1000.0).to_i
    end

    def use_ssl?
      @uri.scheme == 'https'
    end

    def handle_response(_response, _id)
      case _response
      when Net::HTTPSuccess
        json_body = JSON.parse _response.body
        raise NodeError.new json_body['error'] if json_body['error']
        json_body['result']
      else
        raise RPCError.new _response.code, _response.body
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
etherlite-0.1.7 lib/etherlite/connection.rb
etherlite-0.1.6 lib/etherlite/connection.rb
etherlite-0.1.5 lib/etherlite/connection.rb