Class: Jabber::Framework::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/xmpp4r/framework/base.rb

Direct Known Subclasses

Bot

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Base

Returns a new instance of Base.



31
32
33
34
35
36
37
38
# File 'lib/xmpp4r/framework/base.rb', line 31

def initialize(stream)
  @stream = stream
  @helpers = {}
  @helpers_lock = Mutex.new

  # Catch all unhandled iq stanzas (lowest priority)
  @stream.add_iq_callback(-1000, self, &method(:on_unhandled_iq))
end

Instance Attribute Details

#streamObject

Returns the value of attribute stream.



29
30
31
# File 'lib/xmpp4r/framework/base.rb', line 29

def stream
  @stream
end

Class Method Details

.helper(name, klass = nil, &factory) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/xmpp4r/framework/base.rb', line 7

def self.helper(name, klass=nil, &factory)
  if klass.nil? and factory.nil?
    raise "helper #{name} needs a class or factory"
  end

  define_method(name) do
    @helpers_lock.synchronize do
      if @helpers[name]
        @helpers[name]
      else
        if factory
          @helpers[name] = instance_eval { factory.call(@stream) }
        elsif klass
          @helpers[name] = klass.new(@stream)
        else
          raise
          end
      end
    end
  end
end

Instance Method Details

#on_unhandled_iq(iq) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/xmpp4r/framework/base.rb', line 40

def on_unhandled_iq(iq)
  if iq.type == :get or iq.type == :set
    answer = iq.answer(true)
    answer.type = :error
    answer.add(ErrorResponse.new('feature-not-implemented'))
    @stream.send(answer)

    true
  else
    false
  end
end