File: configuration.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 (86 lines) | stat: -rw-r--r-- 2,053 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
# =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/xmppelement'
require 'xmpp4r/pubsub/iq/pubsub'

module Jabber
  module PubSub

    class Configuration < XMPPElement
      ##
      # get the underlying XData form element
      def form
        children.each do |c|
          return c if c.kind_of?(Jabber::Dataforms::XData)
        end

        nil
      end

      def form=(form)
        children.each do |c|
          delete_element(c)
          if c.kind_of?(Jabber::Dataforms::XData)
            delete(c)
          end
        end
        add(form)
      end

      # Implemented by NodeConfig and SubscriptionConfig
      def form_type
        ''
      end

      def options=(options)
        if options
          self.form = form_for_options( form_type, options )
        end
      end

      ##
      # get a list of the configured options represented by this stanza
      # return:: [Hash] the options and their values
      def options
        configured_options = {}

        if !form.nil?
          form.fields.each do |f|
            f.values.size == 1 ? configured_options[f.var] = f.values.first : configured_options[f.var] = f.values
          end
        end

        configured_options
      end

      def node=(node)
        attributes['node'] = node
      end

      def node
        attributes['node']
      end

      private

      ##
      # creates a Dataform for configuration
      # config:: [Hash] the desired configuration options
      def form_for_options(type, options)
        form = Jabber::Dataforms::XData.new(:submit)
        pubsub_config = Jabber::Dataforms::XDataField.new('FORM_TYPE', :hidden)
        pubsub_config.values = [type]
        form.add(pubsub_config)
        options.each_pair do |key, value|
          f = Jabber::Dataforms::XDataField.new(key)
          f.values = value.is_a?(Array) ? value : [value]
          form.add(f)
        end

        form
      end
    end
  end
end