Class: Vines::JID

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

Constant Summary collapse

PATTERN =
/\A(?:([^@]*)@)??([^@\/]*)(?:\/(.*?))?\Z/.freeze
NODE_PREP =
/[[:cntrl:] "&'\/:<>@]/.freeze
NAME_PREP =
/[[:cntrl:] ]/.freeze
RESOURCE_PREP =
/[[:cntrl:]]/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of JID.



25
26
27
28
29
30
31
32
33
34
# File 'lib/vines/jid.rb', line 25

def initialize(node, domain=nil, resource=nil)
  @node, @domain, @resource = node, domain, resource

  if @domain.nil? && @resource.nil?
    @node, @domain, @resource = @node.to_s.scan(PATTERN).first
  end
  [@node, @domain].each {|part| part.downcase! if part }

  validate
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



18
19
20
# File 'lib/vines/jid.rb', line 18

def domain
  @domain
end

#nodeObject (readonly)

Returns the value of attribute node.



18
19
20
# File 'lib/vines/jid.rb', line 18

def node
  @node
end

#resourceObject

Returns the value of attribute resource.



18
19
20
# File 'lib/vines/jid.rb', line 18

def resource
  @resource
end

Class Method Details

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



21
22
23
# File 'lib/vines/jid.rb', line 21

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

Instance Method Details

#<=>(jid) ⇒ Object



62
63
64
# File 'lib/vines/jid.rb', line 62

def <=>(jid)
  self.to_s <=> jid.to_s
end

#bareObject

Strip the resource part from this JID and return it as a new JID object. The new JID contains only the optional node part and the required domain part from the original. This JID remains unchanged.



40
41
42
# File 'lib/vines/jid.rb', line 40

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

#bare?Boolean

Return true if this is a bare JID without a resource part.

Returns:

  • (Boolean)


45
46
47
# File 'lib/vines/jid.rb', line 45

def bare?
  @resource.nil?
end

#domain?Boolean

Return true if this is a domain-only JID without a node or resource part.

Returns:

  • (Boolean)


50
51
52
# File 'lib/vines/jid.rb', line 50

def domain?
  !empty? && to_s == @domain
end

#empty?Boolean

Return true if this JID is equal to the empty string ”. That is, it’s missing the node, domain, and resource parts that form a valid JID. It makes for easier error handling to be able to create JID objects from strings and then check if they’re empty rather than nil.

Returns:

  • (Boolean)


58
59
60
# File 'lib/vines/jid.rb', line 58

def empty?
  to_s == ''
end

#eql?(jid) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(jid)
  jid.is_a?(JID) && self == jid
end

#hashObject



70
71
72
# File 'lib/vines/jid.rb', line 70

def hash
  self.to_s.hash
end

#to_sObject



74
75
76
77
78
79
# File 'lib/vines/jid.rb', line 74

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