Class: Jsonrpctcp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonrpctcp/client.rb

Overview

The JSON-RPC client

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Client

Initialize

Parameters:

  • host (String)

    a hostname or IP

  • port (String, Fixnum)

    a port number



30
31
32
33
# File 'lib/jsonrpctcp/client.rb', line 30

def initialize(host, port)
  @host = host
  @port = port
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Allows to call RPC methods as if they were defined functions: client.mymethod(…)

Parameters:

  • method (Symbol)

    A RPC method name

  • args (Array)

    The arguments for the method are passed as parameters to the function



52
53
54
# File 'lib/jsonrpctcp/client.rb', line 52

def method_missing(method, *args)
  return process_call(method, args)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



25
26
27
# File 'lib/jsonrpctcp/client.rb', line 25

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



25
26
27
# File 'lib/jsonrpctcp/client.rb', line 25

def port
  @port
end

Class Method Details

.gen_idFixnum

Generate a message id - currently the current time

Returns:

  • (Fixnum)

    A time-based id



66
67
68
# File 'lib/jsonrpctcp/client.rb', line 66

def self.gen_id
  Time.now.to_i
end

.is_error?(response) ⇒ TrueClass, FalseClass

Returns whether a response does have an error key

Returns:

  • (TrueClass, FalseClass)

    returns whether a response does have an error key



43
44
45
# File 'lib/jsonrpctcp/client.rb', line 43

def self.is_error?(response)
  return !response || !response.is_a?(Hash) || response.has_key?('error')
end

.success?(response) ⇒ TrueClass, FalseClass

Returns whether a response does not have an error key

Returns:

  • (TrueClass, FalseClass)

    returns whether a response does not have an error key



37
38
39
# File 'lib/jsonrpctcp/client.rb', line 37

def self.success?(response)
  return !Client.is_error?(response)
end

Instance Method Details

#[](method, *args) ⇒ Object

Calls an RPC methods in the client[:mymethod, “arg1”,…] fashion

Parameters:

  • method (Symbol)

    A RPC method name

  • args (Array)

    The arguments for the method are passed as parameters to the function



60
61
62
# File 'lib/jsonrpctcp/client.rb', line 60

def [](method, *args)
  return process_call(method, args)
end