Class: Vines::User
Direct Known Subclasses
Instance Attribute Summary collapse
-
#jid ⇒ Object
readonly
Returns the value of attribute jid.
-
#name ⇒ Object
Returns the value of attribute name.
-
#password ⇒ Object
Returns the value of attribute password.
-
#roster ⇒ Object
Returns the value of attribute roster.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #<=>(user) ⇒ Object
-
#add_subscription_from(jid) ⇒ Object
Add the user’s jid to this contact’s roster with a subscription state of ‘from.’ This signals that this contact has approved a user’s subscription.
-
#contact(jid) ⇒ Object
Returns the contact with this jid or nil if not found.
-
#contact?(jid) ⇒ Boolean
Return true if the jid is on this user’s roster.
- #hash ⇒ Object
-
#initialize(args = {}) ⇒ User
constructor
A new instance of User.
-
#remove_contact(jid) ⇒ Object
Removes the contact with this jid from the user’s roster.
- #remove_subscription_from(jid) ⇒ Object
- #remove_subscription_to(jid) ⇒ Object
-
#request_subscription(jid) ⇒ Object
Update the contact’s jid on this user’s roster to signal that this user has requested the contact’s permission to receive their presence updates.
-
#subscribed_from?(jid) ⇒ Boolean
Returns true if the user has a presence subscription from this contact.
-
#subscribed_from_contacts ⇒ Object
Returns a list of the contacts that are subscribed to this user’s presence updates.
-
#subscribed_to?(jid) ⇒ Boolean
Returns true if the user is subscribed to this contact’s presence updates.
-
#subscribed_to_contacts ⇒ Object
Returns a list of the contacts to which this user has successfully subscribed.
-
#to_roster_xml(id) ⇒ Object
Returns this user’s roster contacts as an iq query element.
-
#update_from(user) ⇒ Object
Update this user’s information from the given user object.
Constructor Details
#initialize(args = {}) ⇒ User
Returns a new instance of User.
10 11 12 13 14 15 16 17 18 |
# 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] @token = args[:token] @roster = args[:roster] || [] end |
Instance Attribute Details
#jid ⇒ Object (readonly)
Returns the value of attribute jid.
8 9 10 |
# File 'lib/vines/user.rb', line 8 def jid @jid end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/vines/user.rb', line 7 def name @name end |
#password ⇒ Object
Returns the value of attribute password.
7 8 9 |
# File 'lib/vines/user.rb', line 7 def password @password end |
#roster ⇒ Object
Returns the value of attribute roster.
7 8 9 |
# File 'lib/vines/user.rb', line 7 def roster @roster end |
#token ⇒ Object
Returns the value of attribute token.
7 8 9 |
# File 'lib/vines/user.rb', line 7 def token @token end |
Instance Method Details
#<=>(user) ⇒ Object
20 21 22 |
# File 'lib/vines/user.rb', line 20 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 contact’s roster with a subscription state of ‘from.’ This signals that this contact has approved a user’s subscription.
93 94 95 96 97 98 99 |
# File 'lib/vines/user.rb', line 93 def add_subscription_from(jid) unless contact = contact(jid) contact = Contact.new(:jid => jid) @roster << contact end contact.subscribe_from end |
#contact(jid) ⇒ Object
Returns the contact with this jid or nil if not found.
44 45 46 47 |
# File 'lib/vines/user.rb', line 44 def contact(jid) = JID.new(jid). @roster.find {|c| c.jid. == } end |
#contact?(jid) ⇒ Boolean
Return true if the jid is on this user’s roster.
39 40 41 |
# File 'lib/vines/user.rb', line 39 def contact?(jid) !contact(jid).nil? end |
#hash ⇒ Object
26 27 28 |
# File 'lib/vines/user.rb', line 26 def hash jid.to_s.hash end |
#remove_contact(jid) ⇒ Object
Removes the contact with this jid from the user’s roster.
64 65 66 67 |
# File 'lib/vines/user.rb', line 64 def remove_contact(jid) = JID.new(jid). @roster.reject! {|c| c.jid. == } end |
#remove_subscription_from(jid) ⇒ Object
107 108 109 110 111 |
# File 'lib/vines/user.rb', line 107 def remove_subscription_from(jid) if contact = contact(jid) contact.unsubscribe_from end end |
#remove_subscription_to(jid) ⇒ Object
101 102 103 104 105 |
# File 'lib/vines/user.rb', line 101 def remove_subscription_to(jid) if contact = contact(jid) contact.unsubscribe_to end end |
#request_subscription(jid) ⇒ Object
Update the contact’s jid on this user’s roster to signal that this user has requested the contact’s permission to receive their presence updates.
83 84 85 86 87 88 89 |
# File 'lib/vines/user.rb', line 83 def request_subscription(jid) unless contact = contact(jid) contact = Contact.new(:jid => jid) @roster << contact end contact.ask = 'subscribe' if %w[none from].include?(contact.subscription) end |
#subscribed_from?(jid) ⇒ Boolean
Returns true if the user has a presence subscription from this contact. The contact is subscribed to this user’s presence.
58 59 60 61 |
# File 'lib/vines/user.rb', line 58 def subscribed_from?(jid) contact = contact(jid) contact && contact.subscribed_from? end |
#subscribed_from_contacts ⇒ Object
Returns a list of the contacts that are subscribed to this user’s presence updates.
77 78 79 |
# File 'lib/vines/user.rb', line 77 def subscribed_from_contacts @roster.select {|c| c.subscribed_from? } end |
#subscribed_to?(jid) ⇒ Boolean
Returns true if the user is subscribed to this contact’s presence updates.
51 52 53 54 |
# File 'lib/vines/user.rb', line 51 def subscribed_to?(jid) contact = contact(jid) contact && contact.subscribed_to? end |
#subscribed_to_contacts ⇒ Object
Returns a list of the contacts to which this user has successfully subscribed.
71 72 73 |
# File 'lib/vines/user.rb', line 71 def subscribed_to_contacts @roster.select {|c| c.subscribed_to? } end |
#to_roster_xml(id) ⇒ Object
Returns this user’s roster contacts as an iq query element.
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/vines/user.rb', line 114 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 |contact| query << contact.to_roster_xml end end end end |
#update_from(user) ⇒ Object
Update this user’s information from the given user object.
31 32 33 34 35 36 |
# File 'lib/vines/user.rb', line 31 def update_from(user) @name = user.name @password = user.password @token = user.token @roster = user.roster.map {|c| c.clone } end |