Class: Eth::Client::Http

Inherits:
Eth::Client show all
Defined in:
lib/eth/client/http.rb

Overview

Provides an HTTP/S-RPC client with basic authentication.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Http

Constructor for the HTTP Client. Should not be used; use Eth::Client.create instead.

Parameters:

  • host (String)

    an URI pointing to an HTTP RPC-API.

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/eth/client/http.rb', line 43

def initialize(host)
  super
  uri = URI.parse(host)
  raise ArgumentError, "Unable to parse the HTTP-URI!" unless ["http", "https"].include? uri.scheme
  @host = uri.host
  @port = uri.port
  @ssl = uri.scheme == "https"
  if !(uri.user.nil? && uri.password.nil?)
    @user = uri.user
    @password = uri.password
    if uri.query
      @uri = URI("#{uri.scheme}://#{uri.user}:#{uri.password}@#{@host}:#{@port}#{uri.path}?#{uri.query}")
    else
      @uri = URI("#{uri.scheme}://#{uri.user}:#{uri.password}@#{@host}:#{@port}#{uri.path}")
    end
  else
    @uri = uri
  end
  @client = HTTPX.plugin(:persistent).with(headers: { "Content-Type" => "application/json" })
end

Instance Attribute Details

#hostObject (readonly)

The host of the HTTP endpoint.



25
26
27
# File 'lib/eth/client/http.rb', line 25

def host
  @host
end

#portObject (readonly)

The port of the HTTP endpoint.



28
29
30
# File 'lib/eth/client/http.rb', line 28

def port
  @port
end

#sslObject (readonly)

Attribute indicator for SSL.



34
35
36
# File 'lib/eth/client/http.rb', line 34

def ssl
  @ssl
end

#uriObject (readonly)

The full URI of the HTTP endpoint, including path.



31
32
33
# File 'lib/eth/client/http.rb', line 31

def uri
  @uri
end

#userObject (readonly)

Attribute for user.



37
38
39
# File 'lib/eth/client/http.rb', line 37

def user
  @user
end

Instance Method Details

#send_request(payload) ⇒ String

Sends an RPC request to the connected HTTP client.

Parameters:

  • payload (Hash)

    the RPC request parameters.

Returns:

  • (String)

    a JSON-encoded response.



68
69
70
71
# File 'lib/eth/client/http.rb', line 68

def send_request(payload)
  response = @client.post(@uri, body: payload)
  response.body.to_s
end