Class: BasicJRPC::Client

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

Direct Known Subclasses

FAFClient

Instance Method Summary collapse

Constructor Details

#initialize(queue, timeout = 10, host = "redis") ⇒ Client

Returns a new instance of Client.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/basicjrpc/client.rb', line 4

def initialize(queue, timeout=10, host="redis")
  @queue = queue
  @payload = {}
  @timeout = timeout
  @instance_id = SecureRandom.uuid
  if host.is_a?(Array)
    @redis = Redis.new(cluster: host.map { |n| "redis://#{n}:6381" }, driver: :hiredis)
  elsif host.is_a?(String)
    @redis = Redis.new(host: @host, port: 6381)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



16
17
18
# File 'lib/basicjrpc/client.rb', line 16

def method_missing(m, *args, &block)
  send_request({ 'method_name' => m.to_s, 'method_arguments' => args, 'callers' => caller.first(10) })
end

Instance Method Details

#send_request(payload) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/basicjrpc/client.rb', line 20

def send_request payload
  payload['message_id'] = SecureRandom.uuid
  payload['instance_id'] = @instance_id
  payload['response'] = true
  my_message = false
  
  @redis.rpush(@queue, Oj.dump(payload))
  Oj.load(@redis.blpop(payload['message_id'], timeout: @timeout)[1], :symbol_keys => true)
end