class Vines::Follower

Attributes

ask[RW]
groups[RW]
jid[R]
name[RW]
subscription[RW]

Public Class Methods

new(args={}) click to toggle source
# File lib/vines/follower.rb, line 10
def initialize(args={})
  @jid = JID.new(args[:jid]).bare
  raise ArgumentError, 'invalid jid' if @jid.empty?
  @name = args[:name]
  @subscription = args[:subscription] || 'none'
  @ask = args[:ask]
  @groups = args[:groups] || []
end

Public Instance Methods

<=>(follower) click to toggle source
# File lib/vines/follower.rb, line 19
def <=>(follower)
  follower.is_a?(Follower) ? self.jid.to_s <=> follower.jid.to_s : nil
end
can_subscribe?() click to toggle source

Returns true if this follower is in a state that allows the user to subscribe to their presence updates.

# File lib/vines/follower.rb, line 38
def can_subscribe?
  @ask == 'subscribe' && %w[none from].include?(@subscription)
end
hash() click to toggle source
# File lib/vines/follower.rb, line 25
def hash
  jid.to_s.hash
end
send_roster_push(recipient) click to toggle source

Write an iq stanza to the recipient stream representing this follower's current roster item state.

# File lib/vines/follower.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() click to toggle source
# File lib/vines/follower.rb, line 51
def subscribe_from
  @subscription = (@subscription == 'none') ? 'from' : 'both'
  @ask = nil
end
subscribe_to() click to toggle source
# File lib/vines/follower.rb, line 42
def subscribe_to
  @subscription = (@subscription == 'none') ? 'to' : 'both'
  @ask = nil
end
subscribed_from?() click to toggle source

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

# File lib/vines/follower.rb, line 68
def subscribed_from?
  %w[from both].include?(@subscription)
end
subscribed_to?() click to toggle source

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

# File lib/vines/follower.rb, line 62
def subscribed_to?
  %w[to both].include?(@subscription)
end
to_h() click to toggle source

Returns a hash of this follower's attributes suitable for persisting in a document store.

# File lib/vines/follower.rb, line 74
def to_h
  {
    'name' => @name,
    'subscription' => @subscription,
    'ask' => @ask,
    'groups' => @groups.sort!
  }
end
to_roster_xml() click to toggle source

Returns this follower as an xmpp <item> element.

# File lib/vines/follower.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.bare.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() click to toggle source
# File lib/vines/follower.rb, line 56
def unsubscribe_from
  @subscription = (@subscription == 'both') ? 'to' : 'none'
end
unsubscribe_to() click to toggle source
# File lib/vines/follower.rb, line 47
def unsubscribe_to
  @subscription = (@subscription == 'both') ? 'from' : 'none'
end
update_from(follower) click to toggle source
# File lib/vines/follower.rb, line 29
def update_from(follower)
  @name = follower.name
  @subscription = follower.subscription
  @ask = follower.ask
  @groups = follower.groups.clone
end