Class: Vx::Lib::Consumer::Rpc::RpcProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/vx/lib/consumer/rpc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consumer) ⇒ RpcProxy

Returns a new instance of RpcProxy.



20
21
22
23
24
25
# File 'lib/vx/lib/consumer/rpc.rb', line 20

def initialize(consumer)
  @parent   = consumer
  @client   = RpcClient.new consumer
  @methods  = {}
  @defined  = false
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



18
19
20
# File 'lib/vx/lib/consumer/rpc.rb', line 18

def client
  @client
end

Instance Method Details

#action(name, fn) ⇒ Object



43
44
45
46
# File 'lib/vx/lib/consumer/rpc.rb', line 43

def action(name, fn)
  define unless @defined
  @methods[name.to_s] = fn
end

#call(method, args, options = {}) ⇒ Object



27
28
29
30
# File 'lib/vx/lib/consumer/rpc.rb', line 27

def call(method, args, options = {})
  ns = @parent.params.consumer_id
  @client.call ns, method, args, options
end

#defineObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/vx/lib/consumer/rpc.rb', line 32

def define
  @parent.exchange RPC_EXCHANGE_NAME
  @parent.queue    "vx.rpc.#{@parent.params.consumer_id}".freeze, durable: false, auto_delete: true

  @parent.send :define_method, :perform do |payload|
    self.class.rpc.process_payload(properties, payload)
  end

  @defined = true
end

#process_payload(properties, payload) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vx/lib/consumer/rpc.rb', line 48

def process_payload(properties, payload)
  m = payload[RPC_PAYLOAD_METHOD]
  p = payload[RPC_PAYLOAD_PARAMS]

  if fn = @methods[m]
    re = fn.call(*p)
    @parent.publish(
      { 'result' => re },
      routing_key:    properties[:reply_to],
      correlation_id: properties[:correlation_id],
      content_type:   JSON_CONTENT_TYPE
    )
  end
end