Class: Vines::Contact
- Inherits:
-
Object
- Object
- Vines::Contact
- Includes:
- Comparable
- Defined in:
- lib/vines/contact.rb
Instance Attribute Summary collapse
-
#ask ⇒ Object
Returns the value of attribute ask.
-
#groups ⇒ Object
Returns the value of attribute groups.
-
#jid ⇒ Object
readonly
Returns the value of attribute jid.
-
#name ⇒ Object
Returns the value of attribute name.
-
#subscription ⇒ Object
Returns the value of attribute subscription.
Instance Method Summary collapse
- #<=>(contact) ⇒ Object
-
#can_subscribe? ⇒ Boolean
Returns true if this contact is in a state that allows the user to subscribe to their presence updates.
- #hash ⇒ Object
-
#initialize(args = {}) ⇒ Contact
constructor
A new instance of Contact.
-
#send_roster_push(recipient) ⇒ Object
Write an iq stanza to the recipient stream representing this contact’s current roster item state.
- #subscribe_from ⇒ Object
- #subscribe_to ⇒ Object
-
#subscribed_from? ⇒ Boolean
Returns true if the user has a presence subscription from this contact.
-
#subscribed_to? ⇒ Boolean
Returns true if the user is subscribed to this contact’s presence updates.
-
#to_h ⇒ Object
Returns a hash of this contact’s attributes suitable for persisting in a document store.
-
#to_roster_xml ⇒ Object
Returns this contact as an xmpp <item> element.
- #unsubscribe_from ⇒ Object
- #unsubscribe_to ⇒ Object
- #update_from(contact) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Contact
Returns a new instance of Contact.
10 11 12 13 14 15 16 17 |
# File 'lib/vines/contact.rb', line 10 def initialize(args={}) @jid = JID.new(args[:jid]). raise ArgumentError, 'invalid jid' if @jid.empty? @name = args[:name] @subscription = args[:subscription] || 'none' @ask = args[:ask] @groups = args[:groups] || [] end |
Instance Attribute Details
#ask ⇒ Object
Returns the value of attribute ask.
7 8 9 |
# File 'lib/vines/contact.rb', line 7 def ask @ask end |
#groups ⇒ Object
Returns the value of attribute groups.
7 8 9 |
# File 'lib/vines/contact.rb', line 7 def groups @groups end |
#jid ⇒ Object (readonly)
Returns the value of attribute jid.
8 9 10 |
# File 'lib/vines/contact.rb', line 8 def jid @jid end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/vines/contact.rb', line 7 def name @name end |
#subscription ⇒ Object
Returns the value of attribute subscription.
7 8 9 |
# File 'lib/vines/contact.rb', line 7 def subscription @subscription end |
Instance Method Details
#<=>(contact) ⇒ Object
19 20 21 |
# File 'lib/vines/contact.rb', line 19 def <=>(contact) contact.is_a?(Contact) ? self.jid.to_s <=> contact.jid.to_s : nil end |
#can_subscribe? ⇒ Boolean
Returns true if this contact is in a state that allows the user to subscribe to their presence updates.
38 39 40 |
# File 'lib/vines/contact.rb', line 38 def can_subscribe? @ask == 'subscribe' && %w[none from].include?(@subscription) end |
#hash ⇒ Object
25 26 27 |
# File 'lib/vines/contact.rb', line 25 def hash jid.to_s.hash end |
#send_roster_push(recipient) ⇒ Object
Write an iq stanza to the recipient stream representing this contact’s current roster item state.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/vines/contact.rb', line 85 def send_roster_push(recipient) doc = Nokogiri::XML::Document.new node = doc.create_element('iq', 'id' => Kit.uuid, 'to' => recipient.user.jid.to_s, 'type' => 'set') node << doc.create_element('query', 'xmlns' => NAMESPACES[:roster]) do |query| query << to_roster_xml end recipient.write(node) end |
#subscribe_from ⇒ Object
51 52 53 54 |
# File 'lib/vines/contact.rb', line 51 def subscribe_from @subscription = (@subscription == 'none') ? 'from' : 'both' @ask = nil end |
#subscribe_to ⇒ Object
42 43 44 45 |
# File 'lib/vines/contact.rb', line 42 def subscribe_to @subscription = (@subscription == 'none') ? 'to' : 'both' @ask = nil end |
#subscribed_from? ⇒ Boolean
Returns true if the user has a presence subscription from this contact. The contact is subscribed to this user’s presence.
68 69 70 |
# File 'lib/vines/contact.rb', line 68 def subscribed_from? %w[from both].include?(@subscription) end |
#subscribed_to? ⇒ Boolean
Returns true if the user is subscribed to this contact’s presence updates.
62 63 64 |
# File 'lib/vines/contact.rb', line 62 def subscribed_to? %w[to both].include?(@subscription) end |
#to_h ⇒ Object
Returns a hash of this contact’s attributes suitable for persisting in a document store.
74 75 76 77 78 79 80 81 |
# File 'lib/vines/contact.rb', line 74 def to_h { 'name' => @name, 'subscription' => @subscription, 'ask' => @ask, 'groups' => @groups.sort! } end |
#to_roster_xml ⇒ Object
Returns this contact as an xmpp <item> element.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/vines/contact.rb', line 98 def to_roster_xml doc = Nokogiri::XML::Document.new doc.create_element('item') do |el| el['ask'] = @ask unless @ask.nil? || @ask.empty? el['jid'] = @jid..to_s el['name'] = @name unless @name.nil? || @name.empty? el['subscription'] = @subscription @groups.sort!.each do |group| el << doc.create_element('group', group) end end end |
#unsubscribe_from ⇒ Object
56 57 58 |
# File 'lib/vines/contact.rb', line 56 def unsubscribe_from @subscription = (@subscription == 'both') ? 'to' : 'none' end |
#unsubscribe_to ⇒ Object
47 48 49 |
# File 'lib/vines/contact.rb', line 47 def unsubscribe_to @subscription = (@subscription == 'both') ? 'from' : 'none' end |
#update_from(contact) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/vines/contact.rb', line 29 def update_from(contact) @name = contact.name @subscription = contact.subscription @ask = contact.ask @groups = contact.groups.clone end |