Class: Klay::Client::Http

Inherits:
Klay::Client show all
Defined in:
lib/klay/client/http.rb

Overview

Provides an HTTP/S-RPC client.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Http

Constructor for the HTTP Client. Should not be used; use Klay::Client.create intead.

Parameters:

  • host (String)

    an URI pointing to an HTTP RPC-API.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
# File 'lib/klay/client/http.rb', line 39

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"
  @uri = URI("#{uri.scheme}://#{@host}:#{@port}#{uri.path}")
end

Instance Attribute Details

#hostObject (readonly)

The host of the HTTP endpoint.



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

def host
  @host
end

#portObject (readonly)

The port of the HTTP endpoint.



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

def port
  @port
end

#sslObject (readonly)

Attribute indicator for SSL.



33
34
35
# File 'lib/klay/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/klay/client/http.rb', line 30

def uri
  @uri
end

Instance Method Details

#send(payload) ⇒ String

Sends an RPC request to the connected HTTP client.

Parameters:

  • payload (Hash)

    the RPC request parameters.

Returns:

  • (String)

    a JSON-encoded response.



53
54
55
56
57
58
59
60
61
# File 'lib/klay/client/http.rb', line 53

def send(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