Class: Blather::JID

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/blather/jid.rb

Overview

This is a simple modification of the JID class from XMPP4R

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, domain = nil, resource = nil) ⇒ JID

:nodoc:

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/blather/jid.rb', line 26

def initialize(node, domain = nil, resource = nil) # :nodoc:
  @resource = resource
  @domain = domain
  @node = node

  if @domain.nil? && @resource.nil?
    @node, @domain, @resource = @node.to_s.scan(PATTERN).first
  end

  @node.downcase!   if @node
  @domain.downcase! if @domain

  raise ArgumentError, 'Node too long'      if (@node || '').length > 1023
  raise ArgumentError, 'Domain too long'    if (@domain || '').length > 1023
  raise ArgumentError, 'Resource too long'  if (@resource || '').length > 1023
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



10
11
12
# File 'lib/blather/jid.rb', line 10

def domain
  @domain
end

#nodeObject (readonly)

Returns the value of attribute node.



10
11
12
# File 'lib/blather/jid.rb', line 10

def node
  @node
end

#resourceObject (readonly)

Returns the value of attribute resource.



10
11
12
# File 'lib/blather/jid.rb', line 10

def resource
  @resource
end

Class Method Details

.new(node, domain = nil, resource = nil) ⇒ Object

Create a new JID. If called as new(‘a@b/c’), parse the string and split (node, domain, resource).

  • node - can be any of the following:

    • a string representing the JID (“[email protected]/resource”)

    • a JID. in which case nothing will be done and the original JID will be passed back

    • a string representing the node

  • domain - the domain of the JID

  • resource - the resource the connection should be bound to



22
23
24
# File 'lib/blather/jid.rb', line 22

def self.new(node, domain = nil, resource = nil)
  node.is_a?(JID) ? node : super
end

Instance Method Details

#<=>(other) ⇒ Object

Compare two JIDs, helpful for sorting etc.

String representations are compared, see JID#to_s



75
76
77
# File 'lib/blather/jid.rb', line 75

def <=>(other)
  to_s <=> other.to_s
end

#strip!Object

Removes the resource (sets it to nil)



65
66
67
68
# File 'lib/blather/jid.rb', line 65

def strip!
  @resource = nil
  self
end

#strippedObject

Returns a new JID with resource removed.



59
60
61
# File 'lib/blather/jid.rb', line 59

def stripped
  dup.strip!
end

#stripped?Boolean

Test if JID is stripped

Returns:

  • (Boolean)


82
83
84
# File 'lib/blather/jid.rb', line 82

def stripped?
  @resource.nil?
end

#to_sObject

Returns a string representation of the JID

  • “”

  • “domain”

  • “node@domain”

  • “domain/resource”

  • “node@domain/resource”



50
51
52
53
54
55
# File 'lib/blather/jid.rb', line 50

def to_s
  s = @domain
  s = "#{@node}@#{s}" if @node
  s = "#{s}/#{@resource}" if @resource
  s
end