Class: MessagePack::JSONRPC::HTTPClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/messagepack/jsonrpc/http_client.rb

Defined Under Namespace

Classes: NilOrEmptyResponseError

Constant Summary collapse

JSONRPC_VERSION =
'2.0'
CONTENT_TYPE =
'application/x-msgpack'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ HTTPClient

Returns a new instance of HTTPClient.



16
17
18
# File 'lib/messagepack/jsonrpc/http_client.rb', line 16

def initialize(endpoint)
  @endpoint = endpoint
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



9
10
11
# File 'lib/messagepack/jsonrpc/http_client.rb', line 9

def endpoint
  @endpoint
end

Instance Method Details

#encoded_payload(method, params) ⇒ Object



34
35
36
# File 'lib/messagepack/jsonrpc/http_client.rb', line 34

def encoded_payload(method, params)
  generate_payload(method, params).to_msgpack
end

#generate_payload(method, params) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/messagepack/jsonrpc/http_client.rb', line 38

def generate_payload(method, params)
  {
    method:  method,
    jsonrpc: JSONRPC_VERSION,
    params:  params,
    id:      SecureRandom.base64
  }
end

#headersObject



47
48
49
# File 'lib/messagepack/jsonrpc/http_client.rb', line 47

def headers
  { 'Content-Type' => CONTENT_TYPE }
end

#remote_call(method, params = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/messagepack/jsonrpc/http_client.rb', line 20

def remote_call(method, params={})
  resp_body = self.class.post(
    endpoint,
    body:    encoded_payload(method, params),
    headers: headers,
  ).body

  if resp_body.nil? or resp_body.empty?
    raise NilOrEmptyResponseError
  else
    MessagePack.unpack(resp_body)
  end
end