Sha256: 0d42441dc8e4afb5f06b2e3e223479ed974fa9a9b32a2fabeeb65f5ddbecb01c

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

module Blather
class Stanza
class Presence

  class Status < Presence
    VALID_STATES = [:away, :chat, :dnd, :xa]

    include Comparable

    register :status

    def self.new(state = nil, message = nil)
      elem = super()
      elem.state = state
      elem.message = message
      elem
    end

    ##
    # Ensures type is nil or :unavailable
    def type=(type)
      raise ArgumentError, "Invalid type (#{type}). Must be nil or unavailable" if type && type.to_sym != :unavailable
      super
    end

    ##
    # Ensure state is one of :away, :chat, :dnd, :xa or nil
    def state=(state)
      state = state.to_sym if state
      state = nil if state == :available
      raise ArgumentError, "Invalid Status (#{state}), use: #{VALID_STATES*' '}" if state && !VALID_STATES.include?(state)

      remove_child :show
      self << XMPPNode.new('show', state) if state
    end

    ##
    # return:: :available if state is nil
    def state
      (type || content_from(:show) || :available).to_sym
    end

    ##
    # Ensure priority is between -128 and 127
    def priority=(priority)
      raise ArgumentError, 'Priority must be between -128 and +127' if priority && !(-128..127).include?(priority.to_i)

      remove_child :priority
      self << XMPPNode.new('priority', priority) if priority
    end

    def priority
      @priority ||= content_from(:priority).to_i
    end

    def message=(msg)
      remove_child :status
      self << XMPPNode.new('status', msg) if msg
    end

    def message
      content_from :status
    end

    ##
    # Compare status based on priority
    # raises an error if the JIDs aren't the same
    def <=>(o)
      raise "Cannot compare status from different JIDs: #{[self.from, o.from].inspect}" unless self.from.stripped == o.from.stripped
      self.priority <=> o.priority
    end

  end #Status

end #Presence
end #Stanza
end #Blather

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blather-0.1 lib/blather/core/stanza/presence/status.rb