Class: Blather::RosterItem

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jid) ⇒ Blather::RosterItem #initialize(jid) ⇒ Blather::RosterItem #initialize(node) ⇒ Blather::RosterItem

Create a new RosterItem

Overloads:



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

#askObject

Returns the value of attribute ask.



9
10
11
# File 'lib/blather/roster_item.rb', line 9

def ask
  @ask
end

#groupsObject

Returns the value of attribute groups.



13
14
15
# File 'lib/blather/roster_item.rb', line 13

def groups
  @groups
end

#jidObject

Returns the value of attribute jid.



9
10
11
# File 'lib/blather/roster_item.rb', line 9

def jid
  @jid
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/blather/roster_item.rb', line 13

def name
  @name
end

#statusesObject (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

Parameters:

Returns:

  • (Fixnum<-1, 0, 1>)


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

Parameters:

  • o (RosterItem)

    the Identity object to compare against

Returns:

  • (true, false)


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

Parameters:

  • resource (String, nil) (defaults to: nil)

    the resource to get the status of



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

Parameters:

  • the (Blather::Stanza::Status)

    new status



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

Returns:

  • (:both, :from, :none, :remove, :to)


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

Parameters:

  • sub (#to_sym)

    the new subscription



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_nodeObject



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