Class: Klay::Client::Http
- Inherits:
-
Klay::Client
- Object
- Klay::Client
- Klay::Client::Http
- Defined in:
- lib/klay/client/http.rb
Overview
Provides an HTTP/S-RPC client.
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
The host of the HTTP endpoint.
-
#port ⇒ Object
readonly
The port of the HTTP endpoint.
-
#ssl ⇒ Object
readonly
Attribute indicator for SSL.
-
#uri ⇒ Object
readonly
The full URI of the HTTP endpoint, including path.
Instance Method Summary collapse
-
#initialize(host) ⇒ Http
constructor
Constructor for the HTTP Client.
-
#send(payload) ⇒ String
Sends an RPC request to the connected HTTP client.
Constructor Details
#initialize(host) ⇒ Http
Constructor for the HTTP Client. Should not be used; use Klay::Client.create intead.
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
#host ⇒ Object (readonly)
The host of the HTTP endpoint.
24 25 26 |
# File 'lib/klay/client/http.rb', line 24 def host @host end |
#port ⇒ Object (readonly)
The port of the HTTP endpoint.
27 28 29 |
# File 'lib/klay/client/http.rb', line 27 def port @port end |
#ssl ⇒ Object (readonly)
Attribute indicator for SSL.
33 34 35 |
# File 'lib/klay/client/http.rb', line 33 def ssl @ssl end |
#uri ⇒ Object (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.
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 |