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)


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

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
end

Instance Attribute Details

#hostObject (readonly)

The host of the HTTP endpoint.



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

def host
  @host
end

#portObject (readonly)

The port of the HTTP endpoint.



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

def port
  @port
end

#sslObject (readonly)

Attribute indicator for SSL.



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

def ssl
  @ssl
end

#uriObject (readonly)

The full URI of the HTTP endpoint, including path.



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

def uri
  @uri
end

#userObject (readonly)

Attribute for user.



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

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.



66
67
68
69
70
71
72
73
74
# File 'lib/eth/client/http.rb', line 66

def send_request(payload)
  http = Net::HTTP.new(@host, @port)
  http.use_ssl = @ssl
  header = { "Content-Type" => "application/json" }
  request = Net::HTTP::Post.new(@uri, header)
  request.body = payload
  response = http.request(request)
  response.body
end