Class: Blather::Stanza::X
- Inherits:
-
XMPPNode
- Object
- Nokogiri::XML::Node
- XMPPNode
- Blather::Stanza::X
- Defined in:
- lib/blather/stanza/x.rb
Overview
# X Stanza
[XEP-0004 Data Forms](xmpp.org/extensions/xep-0004.html)
Data Form node that allows for semi-structured data exchange
Defined Under Namespace
Classes: Field
Constant Summary collapse
- VALID_TYPES =
[:cancel, :form, :result, :submit].freeze
Constants inherited from XMPPNode
Class Method Summary collapse
- .find_or_create(parent) ⇒ Object
-
.new(type = nil, fields = []) ⇒ X
Create a new X node These are passed directly to X::Field.new.
Instance Method Summary collapse
-
#cancel? ⇒ true, false
Check if the x is of type :cancel.
-
#field(var) ⇒ Object
Find a field by var.
-
#fields ⇒ Blather::Stanza::X::Field
List of field objects.
-
#fields=(fields) ⇒ Object
Add an array of fields to form.
-
#form? ⇒ true, false
Check if the x is of type :form.
-
#instructions ⇒ String
Retrieve the form’s instructions.
-
#instructions=(instructions) ⇒ Object
Set the form’s instructions.
-
#result? ⇒ true, false
Check if the x is of type :result.
-
#submit? ⇒ true, false
Check if the x is of type :submit.
-
#title ⇒ String
Retrieve the form’s title.
-
#title=(title) ⇒ Object
Set the form’s title.
-
#type ⇒ Symbol
The Form’s type.
-
#type=(type) ⇒ Object
Set the Form’s type.
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) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/blather/stanza/x.rb', line 36 def self.find_or_create(parent) if found_x = parent.find_first('//ns:x', :ns => self.registered_ns) x = self.new found_x found_x.remove else x = self.new end parent << x x end |
.new(type = nil, fields = []) ⇒ X
Create a new X node These are passed directly to X::Field.new
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/blather/stanza/x.rb', line 20 def self.new(type = nil, fields = []) new_node = super :x case type when Nokogiri::XML::Node new_node.inherit type when Hash new_node.type = type[:type] new_node.fields = type[:fields] else new_node.type = type new_node.fields = fields end new_node end |
Instance Method Details
#cancel? ⇒ true, false
Check if the x is of type :cancel
89 90 91 |
# File 'lib/blather/stanza/x.rb', line 89 def cancel? self.type == :cancel end |
#field(var) ⇒ Object
Find a field by var
72 73 74 |
# File 'lib/blather/stanza/x.rb', line 72 def field(var) fields.detect { |f| f.var == var } end |
#fields ⇒ Blather::Stanza::X::Field
List of field objects
64 65 66 67 68 |
# File 'lib/blather/stanza/x.rb', line 64 def fields self.find('ns:field', :ns => self.class.registered_ns).map do |field| Field.new(field) end end |
#fields=(fields) ⇒ Object
Add an array of fields to form
78 79 80 81 82 83 84 |
# File 'lib/blather/stanza/x.rb', line 78 def fields=(fields) remove_children :field [fields].flatten.each do |field| self << (f = Field.new(field)) f.namespace = self.namespace end end |
#form? ⇒ true, false
Check if the x is of type :form
96 97 98 |
# File 'lib/blather/stanza/x.rb', line 96 def form? self.type == :form end |
#instructions ⇒ String
Retrieve the form’s instructions
117 118 119 |
# File 'lib/blather/stanza/x.rb', line 117 def instructions content_from 'ns:instructions', :ns => self.registered_ns end |
#instructions=(instructions) ⇒ Object
Set the form’s instructions
124 125 126 127 128 129 130 131 |
# File 'lib/blather/stanza/x.rb', line 124 def instructions=(instructions) self.remove_children :instructions if instructions self << (i = XMPPNode.new(:instructions, self.document)) i.namespace = self.namespace i << instructions end end |
#result? ⇒ true, false
Check if the x is of type :result
103 104 105 |
# File 'lib/blather/stanza/x.rb', line 103 def result? self.type == :result end |
#submit? ⇒ true, false
Check if the x is of type :submit
110 111 112 |
# File 'lib/blather/stanza/x.rb', line 110 def submit? self.type == :submit end |
#title ⇒ String
Retrieve the form’s title
136 137 138 |
# File 'lib/blather/stanza/x.rb', line 136 def title content_from 'ns:title', :ns => self.registered_ns end |
#title=(title) ⇒ Object
Set the form’s title
143 144 145 146 147 148 149 150 |
# File 'lib/blather/stanza/x.rb', line 143 def title=(title) self.remove_children :title if title self << (t = XMPPNode.new(:title)) t.namespace = self.namespace t << title end end |
#type ⇒ Symbol
The Form’s type
49 50 51 |
# File 'lib/blather/stanza/x.rb', line 49 def type read_attr :type, :to_sym end |
#type=(type) ⇒ Object
Set the Form’s type
55 56 57 58 59 60 |
# File 'lib/blather/stanza/x.rb', line 55 def type=(type) if type && !VALID_TYPES.include?(type.to_sym) raise ArgumentError, "Invalid Type (#{type}), use: #{VALID_TYPES*' '}" end write_attr :type, type end |