Class: Blather::Stream::Features

Inherits:
Object
  • Object
show all
Defined in:
lib/blather/stream/features.rb

Direct Known Subclasses

Register, Resource, SASL, Session, TLS

Constant Summary collapse

@@features =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, succeed, fail) ⇒ Features

Returns a new instance of Features.



15
16
17
18
19
# File 'lib/blather/stream/features.rb', line 15

def initialize(stream, succeed, fail)
  @stream = stream
  @succeed = succeed
  @fail = fail
end

Class Method Details

.from_namespace(ns) ⇒ Object



11
12
13
# File 'lib/blather/stream/features.rb', line 11

def self.from_namespace(ns)
  @@features[ns]
end

.register(ns) ⇒ Object



7
8
9
# File 'lib/blather/stream/features.rb', line 7

def self.register(ns)
  @@features[ns] = self
end

Instance Method Details

#fail!(msg) ⇒ Object



60
61
62
# File 'lib/blather/stream/features.rb', line 60

def fail!(msg)
  @fail.call msg
end

#feature?(feature) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/blather/stream/features.rb', line 64

def feature?(feature)
  @features && @features.children.find { |v| v.element_name == feature.to_s }
end

#next!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/blather/stream/features.rb', line 30

def next!
  @idx = @idx ? @idx+1 : 0
  if stanza = @features.children[@idx]
    if stanza.namespaces['xmlns'] && (klass = self.class.from_namespace(stanza.namespaces['xmlns']))
      @feature = klass.new(
        @stream,
        proc {
          if (klass == Blather::Stream::Register && stanza = feature?(:mechanisms))
            @idx = @features.children.index(stanza)
            @feature = Blather::Stream::SASL.new @stream, proc { next! }, @fail
            @feature.receive_data stanza
          else
            next!
          end
        },
        (klass == Blather::Stream::SASL && feature?(:register)) ? proc { next! } : @fail
      )
      @feature.receive_data stanza
    else
      next!
    end
  else
    succeed!
  end
end

#receive_data(stanza) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/blather/stream/features.rb', line 21

def receive_data(stanza)
  if @feature
    @feature.receive_data stanza
  else
    @features ||= stanza
    next!
  end
end

#succeed!Object



56
57
58
# File 'lib/blather/stream/features.rb', line 56

def succeed!
  @succeed.call
end