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
- #<=>(o) ⇒ Object
- #eql?(o) ⇒ Boolean (also: #==)
-
#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_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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/blather/roster_item.rb', line 33 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.
8 9 10 |
# File 'lib/blather/roster_item.rb', line 8 def ask @ask end |
#groups ⇒ Object
Returns the value of attribute groups.
12 13 14 |
# File 'lib/blather/roster_item.rb', line 12 def groups @groups end |
#jid ⇒ Object
Returns the value of attribute jid.
8 9 10 |
# File 'lib/blather/roster_item.rb', line 8 def jid @jid end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/blather/roster_item.rb', line 12 def name @name end |
#statuses ⇒ Object (readonly)
Returns the value of attribute statuses.
8 9 10 |
# File 'lib/blather/roster_item.rb', line 8 def statuses @statuses end |
Class Method Details
.new(item) ⇒ Object
15 16 17 18 |
# File 'lib/blather/roster_item.rb', line 15 def self.new(item) return item if item.is_a?(self) super end |
Instance Method Details
#<=>(o) ⇒ Object
121 122 123 |
# File 'lib/blather/roster_item.rb', line 121 def <=>(o) self.jid.to_s <=> o.jid.to_s end |
#eql?(o) ⇒ Boolean Also known as: ==
125 126 127 128 129 |
# File 'lib/blather/roster_item.rb', line 125 def eql?(o) o.is_a?(RosterItem) && o.jid == self.jid && o.groups == self.groups end |
#status(resource = nil) ⇒ Object
The status with the highest priority
101 102 103 104 105 106 107 |
# File 'lib/blather/roster_item.rb', line 101 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
92 93 94 95 96 |
# File 'lib/blather/roster_item.rb', line 92 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
75 76 77 |
# File 'lib/blather/roster_item.rb', line 75 def subscription @subscription || :none end |
#subscription=(sub) ⇒ Object
Set the subscription Ensures it is one of VALID_SUBSCRIPTION_TYPES
65 66 67 68 69 70 |
# File 'lib/blather/roster_item.rb', line 65 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_stanza(type = nil) ⇒ Blather::Stanza::Iq::Roster
Translate the RosterItem into a proper stanza that can be sent over the stream
113 114 115 116 117 118 119 |
# File 'lib/blather/roster_item.rb', line 113 def to_stanza(type = nil) r = Stanza::Iq::Roster.new type n = Stanza::Iq::Roster::RosterItem.new jid, name, subscription, ask r.query << n n.groups = groups r end |