Sha256: f96cb1bce6ec0b3ab196ac343b5b60913b254b2f6b2d46cbb60f16c1be4bb85a

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

module Blather
class Stanza

  ##
  # Base Presence stanza
  class Presence < Stanza
    VALID_TYPES = [:unavailable, :subscribe, :subscribed, :unsubscribe, :unsubscribed, :probe, :error]

    register :presence

    ##
    # Ensure element_name is "presence" for all subclasses
    def self.new
      super :presence
    end

    ##
    # Creates a class based on the presence type
    # either a Status or Subscription object is created based
    # on the type attribute.
    # If neither is found it instantiates a Presence object
    def self.import(node)
      klass = case node['type']
      when nil, 'unavailable' then Status
      when /subscribe/        then Subscription
      else self
      end
      klass.new.inherit(node)
    end

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

    ##
    # Ensures type is one of :unavailable, :subscribe, :subscribed, :unsubscribe, :unsubscribed, :probe 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

1 entries across 1 versions & 1 rubygems

Version Path
blather-0.2 lib/blather/stanza/presence.rb