File: vcard.rb

package info (click to toggle)
ruby-xmpp4r 0.5.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,384 kB
  • sloc: ruby: 17,382; xml: 74; sh: 12; makefile: 4
file content (109 lines) | stat: -rw-r--r-- 2,763 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# =XMPP4R - XMPP Library for Ruby
# License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
# Website::http://xmpp4r.github.io

require 'xmpp4r/iq'
require 'xmpp4r/base64'

module Jabber
  module Vcard
    ##
    # vCard container for User Information
    # (can be specified by users themselves, mostly kept on servers)
    # (JEP 0054)
    class IqVcard < XMPPElement
      name_xmlns 'vCard', 'vcard-temp'
      force_xmlns true

      ##
      # Initialize a <vCard/> element
      # fields:: [Hash] Initialize with keys as XPath element names and values for element texts
      def initialize(fields=nil)
        super()

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

      ##
      # 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
      def [](name)
        text = nil
        each_element(name) { |child| text = child.text }
        text
      end

      ##
      # Set an elements/fields text
      # name:: [String] XPath
      # text:: [String] Value
      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

      ##
      # Get vCard field names
      #
      # Example:
      #  ["NICKNAME", "BDAY", "ORG/ORGUNIT", "PHOTO/TYPE", "PHOTO/BINVAL"]
      #
      # result:: [Array] of [String]
      def fields
        element_names(self).uniq
      end

      ##
      # Get the PHOTO/BINVAL (Avatar picture) field decoded from Base64
      # result:: [String] or [nil]
      def photo_binval
        if (binval = self['PHOTO/BINVAL'])
          Base64::decode64(binval)
        else
          nil
        end
      end

      private

      ##
      # Recursive helper function,
      # returns all element names in an array, concatenated
      # to their parent's name with a slash
      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
    end
  end
end