class Vines::User

Attributes

jid[R]
name[RW]
password[RW]
roster[RW]
token[RW]

Public Class Methods

new(args={}) click to toggle source
# 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

Public Instance Methods

<=>(user) click to toggle source
# 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) click to toggle source

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.

# 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) click to toggle source

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

# File lib/vines/user.rb, line 44
def contact(jid)
  bare = JID.new(jid).bare
  @roster.find {|c| c.jid.bare == bare }
end
contact?(jid) click to toggle source

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

# File lib/vines/user.rb, line 39
def contact?(jid)
  !contact(jid).nil?
end
hash() click to toggle source
# File lib/vines/user.rb, line 26
def hash
  jid.to_s.hash
end
remove_contact(jid) click to toggle source

Removes the contact with this jid from the user's roster.

# File lib/vines/user.rb, line 64
def remove_contact(jid)
  bare = JID.new(jid).bare
  @roster.reject! {|c| c.jid.bare == bare }
end
remove_subscription_from(jid) click to toggle source
# 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) click to toggle source
# File lib/vines/user.rb, line 101
def remove_subscription_to(jid)
  if contact = contact(jid)
    contact.unsubscribe_to
  end
end
request_subscription(jid) click to toggle source

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.

# 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) click to toggle source

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

# File lib/vines/user.rb, line 58
def subscribed_from?(jid)
  contact = contact(jid)
  contact && contact.subscribed_from?
end
subscribed_from_contacts() click to toggle source

Returns a list of the contacts that are subscribed to this user's presence updates.

# File lib/vines/user.rb, line 77
def subscribed_from_contacts
  @roster.select {|c| c.subscribed_from? }
end
subscribed_to?(jid) click to toggle source

Returns true if the user is subscribed to this contact's presence updates.

# File lib/vines/user.rb, line 51
def subscribed_to?(jid)
  contact = contact(jid)
  contact && contact.subscribed_to?
end
subscribed_to_contacts() click to toggle source

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

# File lib/vines/user.rb, line 71
def subscribed_to_contacts
  @roster.select {|c| c.subscribed_to? }
end
to_roster_xml(id) click to toggle source

Returns this user's roster contacts as an iq query element.

# 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) click to toggle source

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

# 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