Sha256: f7e48e422455f0b0e098136f78d6aa480dca40c46bc5c246089e9bfa955d5307

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

module Blather
class Stanza
class Presence

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

    include Comparable

    register :status, :status

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

    attribute_helpers_for(:state, VALID_STATES)

    ##
    # 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)

      set_content_for :show, 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=(new_priority)
      raise ArgumentError, 'Priority must be between -128 and +127' if new_priority && !(-128..127).include?(new_priority.to_i)
      set_content_for :priority, new_priority
      
    end

    content_attr_reader :priority, :to_i

    content_attr_accessor :message, nil, :status

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

  end #Status

end #Presence
end #Stanza
end #Blather

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
sprsquish-blather-0.4.0 lib/blather/stanza/presence/status.rb
sprsquish-blather-0.4.1 lib/blather/stanza/presence/status.rb
blather-0.4.1 lib/blather/stanza/presence/status.rb
blather-0.4.0 lib/blather/stanza/presence/status.rb