Class: Blather::Stanza::X

Inherits:
XMPPNode
  • Object
show all
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

VALID_TYPES =
[:cancel, :form, :result, :submit].freeze

Constants inherited from XMPPNode

XMPPNode::BASE_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XMPPNode

class_from_registration, #decorate, decorator_modules, import, parse, register, #to_stanza

Class Method Details

.find_or_create(parent) ⇒ Blather::Stanza::X

Find the X node on the parent or create a new one



41
42
43
44
45
46
47
48
49
50
# File 'lib/blather/stanza/x.rb', line 41

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/blather/stanza/x.rb', line 21

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



94
95
96
# File 'lib/blather/stanza/x.rb', line 94

def cancel?
  self.type == :cancel
end

#field(var) ⇒ Object

Find a field by var



77
78
79
# File 'lib/blather/stanza/x.rb', line 77

def field(var)
  fields.detect { |f| f.var == var }
end

#fieldsBlather::Stanza::X::Field

List of field objects



69
70
71
72
73
# File 'lib/blather/stanza/x.rb', line 69

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



83
84
85
86
87
88
89
# File 'lib/blather/stanza/x.rb', line 83

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



101
102
103
# File 'lib/blather/stanza/x.rb', line 101

def form?
  self.type == :form
end

#instructionsString

Retrieve the form's instructions



122
123
124
# File 'lib/blather/stanza/x.rb', line 122

def instructions
  content_from 'ns:instructions', :ns => self.registered_ns
end

#instructions=(instructions) ⇒ Object

Set the form's instructions



129
130
131
132
133
134
135
136
# File 'lib/blather/stanza/x.rb', line 129

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



108
109
110
# File 'lib/blather/stanza/x.rb', line 108

def result?
  self.type == :result
end

#submit?true, false

Check if the x is of type :submit



115
116
117
# File 'lib/blather/stanza/x.rb', line 115

def submit?
  self.type == :submit
end

#titleString

Retrieve the form's title



141
142
143
# File 'lib/blather/stanza/x.rb', line 141

def title
  content_from 'ns:title', :ns => self.registered_ns
end

#title=(title) ⇒ Object

Set the form's title



148
149
150
151
152
153
154
155
# File 'lib/blather/stanza/x.rb', line 148

def title=(title)
  self.remove_children :title
  if title
    self << (t = XMPPNode.new(:title))
    t.namespace = self.namespace
    t << title
  end
end

#typeSymbol

The Form's type



54
55
56
# File 'lib/blather/stanza/x.rb', line 54

def type
  read_attr :type, :to_sym
end

#type=(type) ⇒ Object

Set the Form's type



60
61
62
63
64
65
# File 'lib/blather/stanza/x.rb', line 60

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