Class: Blather::Stanza::Iq::Vcard::Vcard

Inherits:
XMPPNode show all
Defined in:
lib/blather/stanza/iq/vcard.rb

Constant Summary collapse

VCARD_NS =
'vcard-temp'

Constants inherited from XMPPNode

XMPPNode::BASE_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XMPPNode

class_from_registration, #content_from, import, #inherit, #inherit_attrs, #inspect, #namespace=, #namespace_href, #nokogiri_namespace=, #read_attr, #read_content, register, #remove_child, #remove_children, #set_content_for, #to_stanza, #write_attr

Methods inherited from Nokogiri::XML::Node

#attr_set, #find_first, #nokogiri_xpath, #xpath

Class Method Details

.find_or_create(parent) ⇒ Vcard::Vcard

Find or create vCard node in Vcard Iq and converts it to Vcard::Vcard

Parameters:

  • parent (Vcard)

    a Vcard Iq where to find or create vCard

Returns:



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/blather/stanza/iq/vcard.rb', line 93

def self.find_or_create(parent)
  if found_vcard = parent.find_first('//ns:vCard', :ns => VCARD_NS)
    vcard = self.new found_vcard
    found_vcard.remove
  else
    vcard = self.new
  end
  parent << vcard

  vcard
end

.new(node = nil) ⇒ Vcard::Vcard

Create a new Vcard::Vcard object

Parameters:

  • node (XML::Node, nil) (defaults to: nil)

    a node to inherit from

Returns:



81
82
83
84
85
86
# File 'lib/blather/stanza/iq/vcard.rb', line 81

def self.new(node = nil)
  new_node = super :vCard
  new_node.namespace = VCARD_NS
  new_node.inherit node if node
  new_node
end

Instance Method Details

#[](name) ⇒ String?

Find the element’s value by name

Parameters:

  • name (String)

    the name of the element

Returns:

  • (String, nil)


110
111
112
113
114
115
116
117
118
# File 'lib/blather/stanza/iq/vcard.rb', line 110

def [](name)
  name = name.split("/").map{|child| "ns:#{child}"}.join("/")

  if elem = find_first(name, :ns => VCARD_NS)
    elem.content
  else
    nil
  end
end

#[]=(name, value) ⇒ String?

Set the element’s value

Parameters:

  • name (String)

    the name of the element

  • value (String, nil)

    the new value of element

Returns:

  • (String, nil)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/blather/stanza/iq/vcard.rb', line 126

def []=(name, value)
  elem = nil
  parent = self

  name.split("/").each do |child|
    elem = parent.find_first("ns:#{child}", :ns => VCARD_NS)
    unless elem
      elem = XMPPNode.new(child, parent.document)
      parent << elem
      parent = elem
    else
      parent = elem
    end
  end

  elem.content = value
end