Class: Jabber::JID

Inherits:
Object
  • Object
show all
Defined in:
lib/jabber4r/jid.rb

Overview

The Jabber ID class is used to hold a parsed jabber identifier (account+domain+resource)

Constant Summary collapse

PATTERN =
/^(?:(?<node>[^@]*)@)??(?<domain>[^@\/]*)(?:\/(?<resource>.*?))?$/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Constructs a JID from the supplied string of the format: node@domain (e.g. “[email protected]/laptop”)

jid - String the jabber id string to parse domain - String the domain of jabber server (optional) resource - String the resource of jabber id (optional)

Examples

jid = Jabber::JID.new(“strech@localhost/attach”) jid.node # => “strech” jid.domain # => “localhost” jid.resource # => “attach”

Raises ArgumentError Returns nothing

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jabber4r/jid.rb', line 49

def initialize(jid, domain = nil, resource = nil)
  raise ArgumentError, "Node can't be empty" if jid.to_s.empty?

  @node, @domain, @resource = self.class.parse(jid)
  @node, @domain = @domain, nil if @node.nil? && @domain

  @domain = domain unless domain.nil?
  @resource = resource unless resource.nil?

  raise ArgumentError, "Couldn't create JID without domain" if @domain.to_s.empty?
end

Instance Attribute Details

#domainObject

Public: The domain indentificator (or IP address)



20
21
22
# File 'lib/jabber4r/jid.rb', line 20

def domain
  @domain
end

#nodeObject

Public: The node (account)



14
15
16
# File 'lib/jabber4r/jid.rb', line 14

def node
  @node
end

#resourceObject

Public: The resource id



17
18
19
# File 'lib/jabber4r/jid.rb', line 17

def resource
  @resource
end

Class Method Details

.to_jid(jid) ⇒ Object

Public: Convert something to Jabber::JID

jid - [String|Jabber::JID] the jid of future Jabber::JID

Returns Jabber::JID



27
28
29
30
31
# File 'lib/jabber4r/jid.rb', line 27

def self.to_jid(jid)
  return jid if jid.kind_of? self

  new jid
end

Instance Method Details

#==(jid) ⇒ Object

Public: Evalutes whether the node, resource and domain are the same

jid - Jabber::JID the other jabber id

Returns boolean



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

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

#hashObject

Public: Override #hash to hash based on the to_s method

Returns Fixnum



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

def hash
  to_s.hash
end

#same?(jid) ⇒ Boolean

Public: Compare accounts without resources

jid - Jabber::JID the other jabber id

Returns boolean

Returns:

  • (Boolean)


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

def same?(jid)
  other_jid = self.class.to_jid(jid)

  other_jid.node == node && other_jid.domain == domain
end

#stripObject

Public: Strip resource from jid and return new object

Returns Jabber::JID



84
85
86
# File 'lib/jabber4r/jid.rb', line 84

def strip
  self.class.new(node, domain)
end

#strip!Object

Public: Strip resource from jid and return the same object

Returns Jabber::JID



91
92
93
94
95
# File 'lib/jabber4r/jid.rb', line 91

def strip!
  @resource = nil

  self
end

#to_sObject

Public: String representation of JID

Returns String



100
101
102
# File 'lib/jabber4r/jid.rb', line 100

def to_s
  ["#{node}@#{domain}", resource].compact.join "/"
end