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

+ (Blather::Stanza::X) find_or_create(parent)

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

Parameters:

Returns:



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

+ (X) new(type = nil, fields = [])

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



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

- (true, false) cancel?

Check if the x is of type :cancel

Returns:

  • (true, false)


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

def cancel?
  self.type == :cancel
end

- (Object) field(var)

Find a field by var

Parameters:

  • var

    the var for the field you wish to find



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

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

- (Blather::Stanza::X::Field) fields

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

- (Object) fields=(fields)

Add an array of fields to form

Parameters:

  • fields

    the array of fields, passed directly to Field.new



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

- (true, false) form?

Check if the x is of type :form

Returns:

  • (true, false)


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

def form?
  self.type == :form
end

- (String) instructions

Retrieve the form's instructions

Returns:

  • (String)


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

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

- (Object) instructions=(instructions)

Set the form's instructions

Parameters:

  • instructions (String)

    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

- (true, false) result?

Check if the x is of type :result

Returns:

  • (true, false)


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

def result?
  self.type == :result
end

- (true, false) submit?

Check if the x is of type :submit

Returns:

  • (true, false)


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

def submit?
  self.type == :submit
end

- (String) title

Retrieve the form's title

Returns:

  • (String)


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

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

- (Object) title=(title)

Set the form's title

Parameters:

  • title (String)

    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

- (Symbol) type

The Form's type

Returns:

  • (Symbol)


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

def type
  read_attr :type, :to_sym
end

- (Object) type=(type)

Set the Form's type

Parameters:

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

    the new type for the form



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