Class: EasyBunnyRPC::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
# File 'lib/easy_bunny_rpc/client.rb', line 7

def initialize options={}
  @options = options
  @subscribed = false

  # The timed queue which will collect incoming messages
  @timed_queue = EasyBunnyRPC::TimedQueue.new

  # Need to set a default timeout. How about 5 seconds?
  set_timeout(5)
end

Instance Method Details

#closeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/easy_bunny_rpc/client.rb', line 33

def close
  if defined?(@channel)
    channel.close
    remove_instance_variable :@channel
    remove_instance_variable :@queue
  end

  if defined?(@connection)
    connection.close
    remove_instance_variable :@connection
  end

  if defined?(@exchange)
    remove_instance_variable :@exchange
  end

  @subscribed = false
end

#popObject



18
19
20
21
22
# File 'lib/easy_bunny_rpc/client.rb', line 18

def pop
  correlation_id, payload = @timed_queue.pop_with_timeout(@timeout)

  JSON.parse(payload).merge!({ 'correlation_id' => correlation_id })
end

#publish(payload, correlation_id = default_correlation_id) ⇒ Object



28
29
30
31
# File 'lib/easy_bunny_rpc/client.rb', line 28

def publish(payload, correlation_id=default_correlation_id)
  start_subscription unless @subscribed
  exchange.publish([payload].to_json, routing_key: @options[:queue], correlation_id: correlation_id, reply_to: queue.name, expiration: (@timeout*1000).to_i)
end

#set_timeout(value) ⇒ Object



24
25
26
# File 'lib/easy_bunny_rpc/client.rb', line 24

def set_timeout(value)
  @timeout = value
end