Sha256: 7d165cd800addd7bc47a8557ba82a142243844feb1b7585c452c2259441f3f35

Contents?: true

Size: 972 Bytes

Versions: 2

Compression:

Stored size: 972 Bytes

Contents

module Blather
class Stanza

  ##
  # Base Iq stanza
  class Iq < Stanza
    VALID_TYPES = [:get, :set, :result, :error]

    register :iq

    def self.import(node)
      klass = nil
      node.children.each { |e| break if klass = class_from_registration(e.element_name, (e.namespace.href if e.namespace)) }

      if klass && klass != self
        klass.import(node)
      else
        new(node[:type]).inherit(node)
      end
    end

    def self.new(type = nil, to = nil, id = nil)
      node = super :iq
      node.type = type || :get
      node.to = to
      node.id = id || self.next_id
      node
    end

    VALID_TYPES.each do |valid_type|
      define_method("#{valid_type}?") { self.type == valid_type }
    end

    ##
    # Ensures type is :get, :set, :result or :error
    def type=(type)
      raise ArgumentError, "Invalid Type (#{type}), use: #{VALID_TYPES*' '}" if type && !VALID_TYPES.include?(type.to_sym)
      super
    end
  end

end #Stanza
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
sprsquish-blather-0.4.0 lib/blather/stanza/iq.rb
blather-0.4.0 lib/blather/stanza/iq.rb