Class: Isimud::TestClient::Queue

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/isimud/test_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, #logger

Constructor Details

#initialize(client, name, proc = Proc.new { |_|}) ⇒ Queue

Returns a new instance of Queue.



17
18
19
20
21
22
# File 'lib/isimud/test_client.rb', line 17

def initialize(client, name, proc = Proc.new { |_|})
  @client   = client
  @name     = name
  @bindings = Hash.new { |hash, key| hash[key] = Set.new }
  @proc     = proc
end

Instance Attribute Details

#bindingsObject (readonly)

Returns the value of attribute bindings.



14
15
16
# File 'lib/isimud/test_client.rb', line 14

def bindings
  @bindings
end

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/isimud/test_client.rb', line 14

def client
  @client
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/isimud/test_client.rb', line 14

def name
  @name
end

#procObject

Returns the value of attribute proc.



15
16
17
# File 'lib/isimud/test_client.rb', line 15

def proc
  @proc
end

Instance Method Details

#bind(exchange, opts = {}) ⇒ Object



24
25
26
27
28
# File 'lib/isimud/test_client.rb', line 24

def bind(exchange, opts = {})
  routing_key = opts[:routing_key]
  log "TestClient: adding routing key #{routing_key} for exchange #{exchange} to queue #{name}"
  @bindings[exchange] << routing_key
end

#cancelObject



30
31
# File 'lib/isimud/test_client.rb', line 30

def cancel
end

#delete(opts = {}) ⇒ Object



33
34
35
36
37
# File 'lib/isimud/test_client.rb', line 33

def delete(opts = {})
  log "TestClient: delete queue #{name}"
  @bindings.clear
  @proc = nil
end

#deliver(data) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/isimud/test_client.rb', line 53

def deliver(data)
  begin
    @proc.try(:call, data)
  rescue => e
    log "TestClient: error delivering message: #{e.message}\n  #{e.backtrace.join("\n  ")}", :error
    client.run_exception_handlers(e)
  end
end

#has_matching_key?(exchange, route) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/isimud/test_client.rb', line 49

def has_matching_key?(exchange, route)
  @bindings[exchange].any? { |key| route =~ make_regexp(key) }
end

#make_regexp(key) ⇒ Object



45
46
47
# File 'lib/isimud/test_client.rb', line 45

def make_regexp(key)
  Regexp.new(key.gsub(/\./, "\\.").gsub(/\*/, '[^.]*').gsub(/#/, '.*'))
end

#unbind(exchange, opts = {}) ⇒ Object



39
40
41
42
43
# File 'lib/isimud/test_client.rb', line 39

def unbind(exchange, opts = {})
  routing_key = opts[:routing_key]
  log "TestClient: removing routing key #{routing_key} for exchange #{exchange} from queue #{name}"
  @bindings[exchange].delete(routing_key)
end