Class: Uppercut::Notifier

Inherits:
Base
  • Object
show all
Defined in:
lib/uppercut/notifier.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ :connect => true }
@@notifiers =
[]

Instance Attribute Summary

Attributes inherited from Base

#client, #roster

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#connect, #connected?, #disconnect, #reconnect, #stanza

Constructor Details

#initialize(user, pw, options = {}) ⇒ Notifier

Returns a new instance of Notifier.



19
20
21
22
23
24
25
26
27
28
# File 'lib/uppercut/notifier.rb', line 19

def initialize(user,pw,options={})
  options = DEFAULT_OPTIONS.merge(options)

  initialize_queue options[:starling], options[:queue]
  
  @user = user
  @pw = pw
  connect if options[:connect]
  listen if options[:listen]
end

Class Method Details

.notifier(name, &block) ⇒ Object



6
7
8
9
# File 'lib/uppercut/notifier.rb', line 6

def notifier(name,&block)
  @@notifiers << name
  define_method(name, &block)
end

Instance Method Details

#inspectObject

:nodoc:



48
49
50
51
52
# File 'lib/uppercut/notifier.rb', line 48

def inspect #:nodoc:
  "<Uppercut::Notifier #{@user} " +
  "#{listening? ? 'Listening' : 'Not Listening'} " + 
  "#{connected? ? 'Connected' : 'Disconnected'}>"
end

#listenObject



32
33
34
35
36
37
38
# File 'lib/uppercut/notifier.rb', line 32

def listen
  connect unless connected?

  @listen_thread = Thread.new {
    loop { notify @starling.get(@queue) }
  }
end

#listening?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/uppercut/notifier.rb', line 44

def listening?
  @listen_thread && @listen_thread.alive?
end

#notify(name, data = nil) ⇒ Object



12
13
14
15
16
17
# File 'lib/uppercut/notifier.rb', line 12

def notify(name,data=nil)
  return false unless connected?
  return nil unless @@notifiers.include?(name)

  send(name,Message.new(self),data)
end

#stopObject



40
41
42
# File 'lib/uppercut/notifier.rb', line 40

def stop
  @listen_thread.kill if listening?
end