Class: C3s::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/pubsub/node.rb

Constant Summary collapse

PATTERN =
/^(?:([^@\/:]*):?)?(?:([^@]*)@)??([^@\/]*)(?:\/(.*?))?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider = "", jid = nil) ⇒ Node

Create a new c3s pubsub node. If called as new(‘provider:jid’) or new(‘provider:jid/resource’) parse the string and split (provider, jid, resource)



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pubsub/node.rb', line 11

def initialize(provider = "", jid = nil)
  @jid = jid
  @provider = provider
  
  if @jid.nil? and @provider
    @provider, node, domain, resource = @provider.to_s.scan(PATTERN).first
    @jid = Jabber::JID.new(node, domain, resource)
  end
  
  @jid.resource = "default" if !resource and @jid
end

Instance Attribute Details

#jidObject (readonly)

Returns the value of attribute jid.



5
6
7
# File 'lib/pubsub/node.rb', line 5

def jid
  @jid
end

#providerObject

Returns the value of attribute provider.



6
7
8
# File 'lib/pubsub/node.rb', line 6

def provider
  @provider
end

Instance Method Details

#leaf?Boolean

Checks if a node is a leaf node on c3s pubsub

Returns:

  • (Boolean)


49
50
51
# File 'lib/pubsub/node.rb', line 49

def leaf?
  resource ? true : false
end

#resourceObject

Returns the resource for this node



36
37
38
39
# File 'lib/pubsub/node.rb', line 36

def resource
  return nil unless @jid
  @jid.resource
end

#resource=(res) ⇒ Object

Sets the resource for the node jid



43
44
45
# File 'lib/pubsub/node.rb', line 43

def resource=(res)
  @jid.resource = res.to_s
end

#to_sObject

Returns a string representation of the node

  • “”

  • “provider”

  • “provider:user@domain”

  • “provider:user@domain/resource”



29
30
31
32
# File 'lib/pubsub/node.rb', line 29

def to_s
  s = @provider
  s = "#{s}:#{@jid.to_s}" if @jid
end