Class: Blather::RosterItem
- Inherits:
-
Object
- Object
- Blather::RosterItem
- Defined in:
- lib/blather/roster_item.rb
Overview
RosterItems hold internal representations of the user’s roster including each JID’s status.
Constant Summary collapse
- VALID_SUBSCRIPTION_TYPES =
[:both, :from, :none, :remove, :to].freeze
Instance Attribute Summary collapse
-
#ask ⇒ Object
Returns the value of attribute ask.
-
#groups ⇒ Object
Returns the value of attribute groups.
-
#jid ⇒ Object
Returns the value of attribute jid.
-
#name ⇒ Object
Returns the value of attribute name.
-
#statuses ⇒ Object
readonly
Returns the value of attribute statuses.
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum<-1, 0, 1>
Compare two RosterItems by their JID.
- #==(o) ⇒ Object
-
#eql?(o, *fields) ⇒ true, false
Compare two RosterItem objects by name, type and category.
-
#initialize(item) ⇒ Blather::RosterItem
constructor
Create a new RosterItem.
-
#status(resource = nil) ⇒ Object
The status with the highest priority.
-
#status=(presence) ⇒ Object
Set the status then sorts them according to priority.
-
#subscription ⇒ :both, ...
Get the current subscription.
-
#subscription=(sub) ⇒ Object
Set the subscription Ensures it is one of VALID_SUBSCRIPTION_TYPES.
- #to_node ⇒ Object
-
#to_stanza(type = nil) ⇒ Blather::Stanza::Iq::Roster
Translate the RosterItem into a proper stanza that can be sent over the stream.
Constructor Details
#initialize(jid) ⇒ Blather::RosterItem #initialize(jid) ⇒ Blather::RosterItem #initialize(node) ⇒ Blather::RosterItem
Create a new RosterItem
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/blather/roster_item.rb', line 35 def initialize(item) @statuses = [] @groups = [] case item when JID self.jid = item.stripped when String self.jid = JID.new(item).stripped when XMPPNode self.jid = JID.new(item[:jid]).stripped self.name = item[:name] self.subscription = item[:subscription] self.ask = item[:ask] item.groups.each { |g| @groups << g } end @groups = [nil] if @groups.empty? end |
Instance Attribute Details
#ask ⇒ Object
Returns the value of attribute ask.
9 10 11 |
# File 'lib/blather/roster_item.rb', line 9 def ask @ask end |
#groups ⇒ Object
Returns the value of attribute groups.
13 14 15 |
# File 'lib/blather/roster_item.rb', line 13 def groups @groups end |
#jid ⇒ Object
Returns the value of attribute jid.
9 10 11 |
# File 'lib/blather/roster_item.rb', line 9 def jid @jid end |
#name ⇒ Object
Returns the value of attribute name.
13 14 15 |
# File 'lib/blather/roster_item.rb', line 13 def name @name end |
#statuses ⇒ Object (readonly)
Returns the value of attribute statuses.
9 10 11 |
# File 'lib/blather/roster_item.rb', line 9 def statuses @statuses end |
Class Method Details
.new(item) ⇒ Object
17 18 19 20 |
# File 'lib/blather/roster_item.rb', line 17 def self.new(item) return item if item.is_a?(self) super end |
Instance Method Details
#<=>(other) ⇒ Fixnum<-1, 0, 1>
Compare two RosterItems by their JID
127 128 129 |
# File 'lib/blather/roster_item.rb', line 127 def <=>(other) JID.new(self.jid) <=> JID.new(other.jid) end |
#==(o) ⇒ Object
141 142 143 |
# File 'lib/blather/roster_item.rb', line 141 def ==(o) eql?(o) end |
#eql?(o, *fields) ⇒ true, false
Compare two RosterItem objects by name, type and category
134 135 136 137 138 |
# File 'lib/blather/roster_item.rb', line 134 def eql?(o, *fields) o.is_a?(self.class) && o.jid == self.jid && o.groups == self.groups end |
#status(resource = nil) ⇒ Object
The status with the highest priority
103 104 105 106 107 108 109 |
# File 'lib/blather/roster_item.rb', line 103 def status(resource = nil) top = if resource @statuses.detect { |s| s.from.resource == resource } else @statuses.last end end |
#status=(presence) ⇒ Object
Set the status then sorts them according to priority
94 95 96 97 98 |
# File 'lib/blather/roster_item.rb', line 94 def status=(presence) @statuses.delete_if { |s| s.from == presence.from || s.state == :unavailable } @statuses << presence @statuses.sort! end |
#subscription ⇒ :both, ...
Get the current subscription
77 78 79 |
# File 'lib/blather/roster_item.rb', line 77 def subscription @subscription || :none end |
#subscription=(sub) ⇒ Object
Set the subscription Ensures it is one of VALID_SUBSCRIPTION_TYPES
67 68 69 70 71 72 |
# File 'lib/blather/roster_item.rb', line 67 def subscription=(sub) if sub && !VALID_SUBSCRIPTION_TYPES.include?(sub = sub.to_sym) raise ArgumentError, "Invalid Type (#{sub}), use: #{VALID_SUBSCRIPTION_TYPES*' '}" end @subscription = sub ? sub : :none end |
#to_node ⇒ Object
119 120 121 |
# File 'lib/blather/roster_item.rb', line 119 def to_node Stanza::Iq::Roster::RosterItem.new jid, name, subscription, ask, groups end |
#to_stanza(type = nil) ⇒ Blather::Stanza::Iq::Roster
Translate the RosterItem into a proper stanza that can be sent over the stream
115 116 117 |
# File 'lib/blather/roster_item.rb', line 115 def to_stanza(type = nil) Stanza::Iq::Roster.new type, to_node end |