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+host+resource)

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

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

Examples

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

Raises ArgumentError Returns nothing

Raises:

  • (ArgumentError)


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

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

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

  @host = host unless host.nil?
  @resource = resource unless resource.nil?

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

Instance Attribute Details

#hostObject

The host name (or IP address)



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

def host
  @host
end

#nodeObject

The node (account)



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

def node
  @node
end

#resourceObject

The resource id



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

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



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

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 host are the same

jid - Jabber::JID the other jabber id

Returns boolean



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

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

#hashObject

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

Returns Fixnum



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

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)


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

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

  other_jid.node == node && other_jid.host == host
end

#stripObject

Public: Strip resource from jid and return new object

Returns Jabber::JID



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

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

#strip!Object

Public: Strip resource from jid and return the same object

Returns Jabber::JID



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

def strip!
  @resource = nil

  self
end

#to_sObject

Public: String representation of JID

Returns String



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

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