Class: Blather::Stanza::X

Inherits:
XMPPNode 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 collapse

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, #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

Parameters:

  • type (:cancel, :form, :result, :submit, nil) (defaults to: nil)

    the x:form type

  • fields (Array<Array, X::Field>, nil) (defaults to: [])

    a list of fields.

Returns:

  • (X)

    a new X stanza



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

Returns:

  • (true, false)


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

Parameters:

  • var

    the var for the field you wish to find



72
73
74
# File 'lib/blather/stanza/x.rb', line 72

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

#fieldsBlather::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

Parameters:

  • fields

    the array of fields, passed directly to Field.new



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

Returns:

  • (true, false)


96
97
98
# File 'lib/blather/stanza/x.rb', line 96

def form?
  self.type == :form
end

#instructionsString

Retrieve the form’s instructions

Returns:

  • (String)


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

Parameters:

  • instructions (String)

    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

Returns:

  • (true, false)


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

Returns:

  • (true, false)


110
111
112
# File 'lib/blather/stanza/x.rb', line 110

def submit?
  self.type == :submit
end

#titleString

Retrieve the form’s title

Returns:

  • (String)


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

Parameters:

  • title (String)

    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

#typeSymbol

The Form’s type

Returns:

  • (Symbol)


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

Parameters:

  • type (:cancel, :form, :result, :submit)

    the new type for the form



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