Sha256: 810307e69769d82f6dba866c735e13e00fc1390cc84b2862e90bf6d1bcf74f06

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module Etherlite
  class Connection
    include Api::Rpc
    include Api::ParityRpc

    attr_reader :uri, :chain_id, :use_parity

    def initialize(_uri, _options = {})
      @uri = _uri
      @chain_id = _options[:chain_id]
      @use_parity = _options[:use_parity]
    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

1 entries across 1 versions & 1 rubygems

Version Path
etherlite-0.6.0 lib/etherlite/connection.rb