Class: JRPC::BaseClient

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/jrpc/base_client.rb

Direct Known Subclasses

TcpClient

Constant Summary collapse

ID_CHARACTERS =
(('a'..'z').to_a + ('0'..'9').to_a + ('A'..'Z').to_a).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options) ⇒ BaseClient

Returns a new instance of BaseClient.



11
12
13
14
# File 'lib/jrpc/base_client.rb', line 11

def initialize(uri, options)
  @uri = uri
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params) ⇒ Object



16
17
18
# File 'lib/jrpc/base_client.rb', line 16

def method_missing(method, *params)
  invoke_request(method, *params)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/jrpc/base_client.rb', line 7

def options
  @options
end

#uriObject (readonly)

Returns the value of attribute uri.



7
8
9
# File 'lib/jrpc/base_client.rb', line 7

def uri
  @uri
end

Instance Method Details

#invoke_notification(method, *params) ⇒ Object



34
35
36
37
# File 'lib/jrpc/base_client.rb', line 34

def invoke_notification(method, *params)
  send_notification create_message(method, params).to_json
  nil
end

#invoke_request(method, *params) ⇒ Object



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

def invoke_request(method, *params)
  request = create_message(method, params)
  id = generate_id
  request['id'] = id

  response = send_command request.to_json
  response = JSON.parse response

  validate_response(response, id)
  parse_error(response['error']) if response.has_key?('error')

  response['result']
end