class Blather::Stream::Features

@private

Public Class Methods

from_namespace(ns) click to toggle source
# File lib/blather/stream/features.rb, line 11
def self.from_namespace(ns)
  @@features[ns]
end
new(stream, succeed, fail) click to toggle source
# File lib/blather/stream/features.rb, line 15
def initialize(stream, succeed, fail)
  @stream, @succeed, @fail = stream, succeed, fail
end
register(ns) click to toggle source
# File lib/blather/stream/features.rb, line 7
def self.register(ns)
  @@features[ns] = self
end

Public Instance Methods

fail!(msg) click to toggle source
# File lib/blather/stream/features.rb, line 64
def fail!(msg)
  @fail.call msg
end
feature?(feature) click to toggle source
# File lib/blather/stream/features.rb, line 68
def feature?(feature)
  @features && @features.children.find { |v| v.element_name == feature.to_s }
end
next!() click to toggle source
# File lib/blather/stream/features.rb, line 28
def next!
  bind = @features.at_xpath('ns:bind', ns: 'urn:ietf:params:xml:ns:xmpp-bind')
  session = @features.at_xpath('ns:session', ns: 'urn:ietf:params:xml:ns:xmpp-session')
  if bind && session && @features.children.last != session
    bind.after session
  end

  @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) click to toggle source
# File lib/blather/stream/features.rb, line 19
def receive_data(stanza)
  if @feature
    @feature.receive_data stanza
  else
    @features ||= stanza
    next!
  end
end
succeed!() click to toggle source
# File lib/blather/stream/features.rb, line 60
def succeed!
  @succeed.call
end