Class: Vines::User

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/vines/user.rb

Direct Known Subclasses

Cluster::UserProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ User

Returns a new instance of User.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
# File 'lib/vines/user.rb', line 10

def initialize(args={})
  @jid = JID.new(args[:jid])
  raise ArgumentError, 'invalid jid' if @jid.empty?

  @name = args[:name]
  @password = args[:password]
  @roster = args[:roster] || []
end

Instance Attribute Details

#jidObject (readonly)

Returns the value of attribute jid.



8
9
10
# File 'lib/vines/user.rb', line 8

def jid
  @jid
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/vines/user.rb', line 7

def name
  @name
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/vines/user.rb', line 7

def password
  @password
end

#rosterObject

Returns the value of attribute roster.



7
8
9
# File 'lib/vines/user.rb', line 7

def roster
  @roster
end

Instance Method Details

#<=>(user) ⇒ Object



19
20
21
# File 'lib/vines/user.rb', line 19

def <=>(user)
  user.is_a?(User) ? self.jid.to_s <=> user.jid.to_s : nil
end

#add_subscription_from(jid) ⇒ Object

Add the user’s jid to this follower’s roster with a subscription state of ‘from.’ This signals that this follower has approved a user’s subscription.



91
92
93
94
95
96
97
# File 'lib/vines/user.rb', line 91

def add_subscription_from(jid)
  unless follower = follower(jid)
    follower = Follower.new(:jid => jid)
    @roster << follower
  end
  follower.subscribe_from
end

#follower(jid) ⇒ Object

Returns the follower with this jid or nil if not found.



42
43
44
45
# File 'lib/vines/user.rb', line 42

def follower(jid)
  bare = JID.new(jid).bare
  @roster.find {|c| c.jid.bare == bare }
end

#follower?(jid) ⇒ Boolean

Return true if the jid is on this user’s roster.

Returns:

  • (Boolean)


37
38
39
# File 'lib/vines/user.rb', line 37

def follower?(jid)
  !follower(jid).nil?
end

#hashObject



25
26
27
# File 'lib/vines/user.rb', line 25

def hash
  jid.to_s.hash
end

#remove_follower(jid) ⇒ Object

Removes the follower with this jid from the user’s roster.



62
63
64
65
# File 'lib/vines/user.rb', line 62

def remove_follower(jid)
  bare = JID.new(jid).bare
  @roster.reject! {|c| c.jid.bare == bare }
end

#remove_subscription_from(jid) ⇒ Object



105
106
107
108
109
# File 'lib/vines/user.rb', line 105

def remove_subscription_from(jid)
  if follower = follower(jid)
    follower.unsubscribe_from
  end
end

#remove_subscription_to(jid) ⇒ Object



99
100
101
102
103
# File 'lib/vines/user.rb', line 99

def remove_subscription_to(jid)
  if follower = follower(jid)
    follower.unsubscribe_to
  end
end

#request_subscription(jid) ⇒ Object

Update the follower’s jid on this user’s roster to signal that this user has requested the follower’s permission to receive their presence updates.



81
82
83
84
85
86
87
# File 'lib/vines/user.rb', line 81

def request_subscription(jid)
  unless follower = follower(jid)
    follower = Follower.new(:jid => jid)
    @roster << follower
  end
  follower.ask = 'subscribe' if %w[none from].include?(follower.subscription)
end

#subscribed_from?(jid) ⇒ Boolean

Returns true if the user has a presence subscription from this follower. The follower is subscribed to this user’s presence.

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/vines/user.rb', line 56

def subscribed_from?(jid)
  follower = follower(jid)
  follower && follower.subscribed_from?
end

#subscribed_from_followersObject

Returns a list of the followers that are subscribed to this user’s presence updates.



75
76
77
# File 'lib/vines/user.rb', line 75

def subscribed_from_followers
  @roster.select {|c| c.subscribed_from? }
end

#subscribed_to?(jid) ⇒ Boolean

Returns true if the user is subscribed to this follower’s presence updates.

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/vines/user.rb', line 49

def subscribed_to?(jid)
  follower = follower(jid)
  follower && follower.subscribed_to?
end

#subscribed_to_followersObject

Returns a list of the followers to which this user has successfully subscribed.



69
70
71
# File 'lib/vines/user.rb', line 69

def subscribed_to_followers
  @roster.select {|c| c.subscribed_to? }
end

#to_roster_xml(id) ⇒ Object

Returns this user’s roster followers as an iq query element.



112
113
114
115
116
117
118
119
120
121
# File 'lib/vines/user.rb', line 112

def to_roster_xml(id)
  doc = Nokogiri::XML::Document.new
  doc.create_element('iq', 'id' => id, 'type' => 'result') do |el|
    el << doc.create_element('query', 'xmlns' => 'jabber:iq:roster') do |query|
      @roster.sort!.each do |follower|
        query << follower.to_roster_xml
      end
    end
  end
end

#update_from(user) ⇒ Object

Update this user’s information from the given user object.



30
31
32
33
34
# File 'lib/vines/user.rb', line 30

def update_from(user)
  @name = user.name
  @password = user.password
  @roster = user.roster.map {|c| c.clone }
end