Class: Jabber::Vcard::IqVcard

Inherits:
REXML::Element show all
Defined in:
lib/xmpp4r/vcard/iq/vcard.rb

Overview

vCard container for User Information (can be specified by users themselves, mostly kept on servers) (JEP 0054)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from REXML::Element

#delete_elements, #first_element, #first_element_text, #import, #replace_element_text, #typed_add

Constructor Details

#initialize(fields = nil) ⇒ IqVcard

Initialize a <vCard/> element

fields
Hash

Initialize with keys as XPath element names and values for element texts



17
18
19
20
21
22
23
24
25
26
# File 'lib/xmpp4r/vcard/iq/vcard.rb', line 17

def initialize(fields=nil)
  super("vCard")
  add_namespace('vcard-temp')

  unless fields.nil?
    fields.each { |name,value|
      self[name] = value
    }
  end
end

Class Method Details

.import(element) ⇒ Object

element
REXML::Element

to import

result
IqVcard

with all attributes and children copied from element



31
32
33
# File 'lib/xmpp4r/vcard/iq/vcard.rb', line 31

def IqVcard.import(element)
  IqVcard::new.import(element)
end

Instance Method Details

#[](name) ⇒ Object

Get an elements/fields text

vCards have too much possible children, so ask for them here and extract the result with iqvcard.element('…').text

name
String

XPath



41
42
43
44
45
# File 'lib/xmpp4r/vcard/iq/vcard.rb', line 41

def [](name)
  text = nil
  each_element(name) { |child| text = child.text }
  text
end

#[]=(name, text) ⇒ Object

Set an elements/fields text

name
String

XPath

text
String

Value



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xmpp4r/vcard/iq/vcard.rb', line 51

def []=(name, text)
  xe = self
  name.split(/\//).each do |elementname|
    # Does the children already exist?
    newxe = nil
    xe.each_element(elementname) { |child| newxe = child }

    if newxe.nil?
      # Create a new
      xe = xe.add_element(elementname)
    else
      # Or take existing
      xe = newxe
    end
  end
  xe.text = text
end

#element_names(xe, prefix = '') ⇒ Object

Recursive helper function, returns all element names in an array, concatenated to their parent's name with a slash



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/xmpp4r/vcard/iq/vcard.rb', line 84

def element_names(xe, prefix='')  # :nodoc:
  res = []
  xe.each_element { |child|
    if child.kind_of?(REXML::Element)
      children = element_names(child, "#{prefix}#{child.name}/")
      if children == []
        res.push("#{prefix}#{child.name}")
      else
        res += children
      end
    end
  }
  res
end

#fieldsObject

Get vCard field names

Example:

["NICKNAME", "BDAY", "ORG/ORGUNIT", "PHOTO/TYPE", "PHOTO/BINVAL"]
result
Array

of [String]



76
77
78
# File 'lib/xmpp4r/vcard/iq/vcard.rb', line 76

def fields
  element_names(self).uniq
end