Class: Jabber::JID

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

Overview

The JID class represents a Jabber Identifier as described by RFC3920 section 3.1.

Note that you can use JIDs also for Sorting, Hash keys, …

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/xmpp4r/jid.rb', line 26

def initialize(node = "", domain = nil, resource = nil)
  @resource = resource
  @domain = domain
  @node = node
  if @domain.nil? and @resource.nil? and @node
    @node, @domain, @resource = @node.to_s.scan(PATTERN).first
  end

  if USE_STRINGPREP
    @node = IDN::Stringprep.nodeprep(@node) if @node
    @domain = IDN::Stringprep.nameprep(@domain) if @domain
    @resource = IDN::Stringprep.resourceprep(@resource) if @resource
  else
    @node.downcase! if @node
    @domain.downcase! if @domain
  end

  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

Class Method Details

.escape(jid) ⇒ Object

Escape JID



152
153
154
# File 'lib/xmpp4r/jid.rb', line 152

def JID::escape(jid)
  return jid.to_s.gsub('@', '%')
end

Instance Method Details

#<=>(o) ⇒ Object

Compare two JIDs, helpful for sorting etc.

String representations are compared, see JID#to_s



107
108
109
# File 'lib/xmpp4r/jid.rb', line 107

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

#==(o) ⇒ Object

Ccompare to another JID

String representations are compared, see JID#to_s



98
99
100
# File 'lib/xmpp4r/jid.rb', line 98

def ==(o)
  to_s == o.to_s
end

#domainObject

Get the JID’s domain



125
126
127
128
# File 'lib/xmpp4r/jid.rb', line 125

def domain
  return nil if @domain.empty?
  @domain
end

#domain=(v) ⇒ Object

Set the JID’s domain



131
132
133
134
135
136
# File 'lib/xmpp4r/jid.rb', line 131

def domain=(v)
  @domain = v.to_s
  if USE_STRINGPREP
    @domain = IDN::Stringprep.nodeprep(@domain)
  end
end

#empty?Boolean

Test if jid is empty

Returns:

  • (Boolean)


157
158
159
# File 'lib/xmpp4r/jid.rb', line 157

def empty?
  to_s.empty?
end

#eql?(o) ⇒ Boolean

Ccompare to another JID

String representations are compared, see JID#to_s

Returns:

  • (Boolean)


90
91
92
# File 'lib/xmpp4r/jid.rb', line 90

def eql?(o)
  to_s.eql?(o.to_s)
end

#hashObject

Returns a hash value of the String representation (see JID#to_s)



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

def hash
  return to_s.hash
end

#nodeObject

Get the JID’s node



112
113
114
# File 'lib/xmpp4r/jid.rb', line 112

def node
  @node
end

#node=(v) ⇒ Object

Set the JID’s node



117
118
119
120
121
122
# File 'lib/xmpp4r/jid.rb', line 117

def node=(v)
  @node = v.to_s
  if USE_STRINGPREP
    @node = IDN::Stringprep.nodeprep(@node) if @node
  end
end

#resourceObject

Get the JID’s resource



139
140
141
# File 'lib/xmpp4r/jid.rb', line 139

def resource
  @resource
end

#resource=(v) ⇒ Object

Set the JID’s resource



144
145
146
147
148
149
# File 'lib/xmpp4r/jid.rb', line 144

def resource=(v)
  @resource = v.to_s
  if USE_STRINGPREP
    @resource = IDN::Stringprep.nodeprep(@resource)
  end
end

#stripObject Also known as: bare

Returns a new JID with resource removed.

return
JID


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

def strip
  JID.new(@node, @domain)
end

#strip!Object Also known as: bare!

Removes the resource (sets it to nil)

return
JID

self



73
74
75
76
# File 'lib/xmpp4r/jid.rb', line 73

def strip!
  @resource = nil
  self
end

#stripped?Boolean Also known as: bared?

Test id jid is strepped

Returns:

  • (Boolean)


162
163
164
# File 'lib/xmpp4r/jid.rb', line 162

def stripped?
  @resource.nil?
end

#to_sObject

Returns a string representation of the JID

  • “”

  • “domain”

  • “node@domain”

  • “domain/resource”

  • “node@domain/resource”



55
56
57
58
59
60
# File 'lib/xmpp4r/jid.rb', line 55

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