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)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ JID

Constructs a JID from the supplied string of the format:

node@host[/resource] (e.g. "[email protected]/laptop")
id
String

The jabber id string to parse



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jabber4r/jid.rb', line 35

def initialize(id)
  at_loc = id.index('@')
  slash_loc = id.index('/')
  if at_loc.nil? and slash_loc.nil?
    @host = id
  end
  if at_loc
    @node = id[0,at_loc]
    host_end = slash_loc ? slash_loc-(at_loc+1) : id.size-(at_loc+1)
    @host = id[at_loc+1,host_end]
    @resource = id[slash_loc+1, id.size] if slash_loc
  end
end

Instance Attribute Details

#hostObject

The host name (or IP address)



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

def host
  @host
end

#nodeObject

The node (account)



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

def node
  @node
end

#resourceObject

The resource id



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

def resource
  @resource
end

Class Method Details

.to_jid(id) ⇒ Object



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

def JID.to_jid(id)
  return id if id.kind_of? JID
  return JID.new(id)
end

Instance Method Details

#==(other) ⇒ Object

Evalutes whether the node, resource and host are the same

other
Jabber::JID

The other jabber id

return
Boolean

True if they match



54
55
56
57
# File 'lib/jabber4r/jid.rb', line 54

def ==(other)
  return true if other.node==@node and other.resource==@resource and other.host==@host
  return false
end

#hashObject

Override #hash to hash based on the to_s method



87
88
89
# File 'lib/jabber4r/jid.rb', line 87

def hash
  return to_s.hash
end

#same_account?(other) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/jabber4r/jid.rb', line 59

def same_account?(other)
  other = JID.to_jid(other)
  return true if other.node==@node  and other.host==@host
   return false
end

#strip_resourceObject

Removes the resource from this JID



68
69
70
71
# File 'lib/jabber4r/jid.rb', line 68

def strip_resource
  @resource=nil
  return self
end

#to_sObject

Returns the string (“node@host/resource”) representation of this JID

return
String

String form of JID



78
79
80
81
82
# File 'lib/jabber4r/jid.rb', line 78

def to_s
  result = (@node.to_s+"@"+@host.to_s)
  result += ("/"+@resource) if @resource
  return result
end