Sha256: 4046ebc955d87bdd6cf6c33101c047a2956087cfe699de877338a2ec08044efb

Contents?: true

Size: 994 Bytes

Versions: 4

Compression:

Stored size: 994 Bytes

Contents

module Blather # :nodoc:
class Stream # :nodoc:

  class Features
    @@features = {}
    def self.register(ns)
      @@features[ns] = self
    end

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

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

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

    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 { next! }, @fail
          @feature.receive_data stanza
        else
          next!
        end
      else
        succeed!
      end
    end

    def succeed!
      @succeed.call
    end

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

end #Stream
end #Blather

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
sprsquish-blather-0.4.0 lib/blather/stream/features.rb
sprsquish-blather-0.4.1 lib/blather/stream/features.rb
blather-0.4.0 lib/blather/stream/features.rb
blather-0.4.1 lib/blather/stream/features.rb