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

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

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

- (Object) bindings (readonly)

Returns the value of attribute bindings



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

def bindings
  @bindings
end

- (Object) client (readonly)

Returns the value of attribute client



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

def client
  @client
end

- (Object) name (readonly)

Returns the value of attribute name



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

def name
  @name
end

- (Object) proc

Returns the value of attribute proc



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

def proc
  @proc
end

Instance Method Details

- (Object) bind(exchange, opts = {})



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

- (Object) cancel



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

def cancel
  @proc = nil
end

- (Object) delete(opts = {})



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

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

- (Object) deliver(data)



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

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

- (Boolean) has_matching_key?(exchange, route)

Returns:

  • (Boolean)


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

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

- (Object) make_regexp(key)



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

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

- (Object) unbind(exchange, opts = {})



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

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