class Blather::Stanza::X

# X Stanza

[XEP-0004 Data Forms](xmpp.org/extensions/xep-0004.html)

Data Form node that allows for semi-structured data exchange

@handler :x

Constants

VALID_TYPES

@private

Public Class Methods

find_or_create(parent) click to toggle source

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

@param [Blather::Stanza] parent the parent node to search under @return [Blather::Stanza::X]

# 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 = []) click to toggle source

Create a new X node @param [:cancel, :form, :result, :submit, nil] type the x:form type @param [Array<Array, X::Field>, nil] fields a list of fields. These are passed directly to X::Field.new @return [X] a new X stanza

Calls superclass method
# 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

Public Instance Methods

cancel?() click to toggle source

Check if the x is of type :cancel

@return [true, false]

# File lib/blather/stanza/x.rb, line 94
def cancel?
  self.type == :cancel
end
field(var) click to toggle source

Find a field by var @param var the var for the field you wish to find

# File lib/blather/stanza/x.rb, line 77
def field(var)
  fields.detect { |f| f.var == var }
end
fields() click to toggle source

List of field objects @return [Blather::Stanza::X::Field]

# 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) click to toggle source

Add an array of fields to form @param fields the array of fields, passed directly to Field.new

# 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?() click to toggle source

Check if the x is of type :form

@return [true, false]

# File lib/blather/stanza/x.rb, line 101
def form?
  self.type == :form
end
instructions() click to toggle source

Retrieve the form's instructions

@return [String]

# File lib/blather/stanza/x.rb, line 122
def instructions
  content_from 'ns:instructions', :ns => self.registered_ns
end
instructions=(instructions) click to toggle source

Set the form's instructions

@param [String] instructions the form's instructions

# 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?() click to toggle source

Check if the x is of type :result

@return [true, false]

# File lib/blather/stanza/x.rb, line 108
def result?
  self.type == :result
end
submit?() click to toggle source

Check if the x is of type :submit

@return [true, false]

# File lib/blather/stanza/x.rb, line 115
def submit?
  self.type == :submit
end
title() click to toggle source

Retrieve the form's title

@return [String]

# File lib/blather/stanza/x.rb, line 141
def title
  content_from 'ns:title', :ns => self.registered_ns
end
title=(title) click to toggle source

Set the form's title

@param [String] title the form's title

# 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
type() click to toggle source

The Form's type @return [Symbol]

# File lib/blather/stanza/x.rb, line 54
def type
  read_attr :type, :to_sym
end
type=(type) click to toggle source

Set the Form's type @param [:cancel, :form, :result, :submit] type the new type for the form

# 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