blather 0.4.11 → 0.4.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -41,7 +41,7 @@ class Iq
41
41
  self.action = :execute unless self.action
42
42
  self
43
43
  end
44
-
44
+
45
45
  # Overrides the parent method to ensure the reply has no action
46
46
  #
47
47
  # @return [self]
@@ -315,14 +315,7 @@ class Iq
315
315
 
316
316
  # Returns the command's x:data form child
317
317
  def form
318
- if found_x = command.find_first('ns:x', :ns => X.registered_ns)
319
- x = X.new found_x
320
- found_x.remove
321
- else
322
- x = X.new
323
- end
324
- self.command << x
325
- x
318
+ X.find_or_create self
326
319
  end
327
320
  end #Command
328
321
 
@@ -172,7 +172,7 @@ class Stanza
172
172
  klass = class_from_registration(e.element_name, ns)
173
173
  end
174
174
 
175
- if klass && klass != self
175
+ if klass && klass != self && klass != Blather::Stanza::X
176
176
  klass.import(node)
177
177
  else
178
178
  new(node[:type]).inherit(node)
@@ -326,7 +326,12 @@ class Stanza
326
326
  set_content_for :thread, thread
327
327
  find_first('thread')[:parent] = parent
328
328
  end
329
+
330
+ # Returns the message's x:data form child
331
+ def form
332
+ X.find_or_create self
333
+ end
329
334
  end
330
335
 
331
336
  end
332
- end
337
+ end
@@ -65,8 +65,6 @@ class Stanza
65
65
  # This fragment is found in many places throughout the pubsub spec
66
66
  # This is a convenience class to attach methods to the node
67
67
  class PubSubItem < XMPPNode
68
- ATOM_NS = 'http://www.w3.org/2005/Atom'.freeze
69
-
70
68
  # Create a new PubSubItem
71
69
  #
72
70
  # @param [String, nil] id the id of the stanza
@@ -96,32 +94,22 @@ class Stanza
96
94
 
97
95
  # Get the item's payload
98
96
  #
99
- # To get the XML representation use #entry
100
- #
101
- # @return [String, nil]
97
+ # @return [String, XMPPNode, nil]
102
98
  def payload
103
- self.entry.content.empty? ? nil : content
99
+ children.empty? ? nil : children.to_s
104
100
  end
105
101
 
106
102
  # Set the item's payload
107
103
  #
108
- # @param [String, nil] payload the payload
104
+ # @param [String, XMPPNode, nil] payload the payload
109
105
  def payload=(payload)
110
- self.entry.content = payload
111
- end
112
-
113
- # Get or create the entry node
114
- #
115
- # @return [Blather::XMPPNode]
116
- def entry
117
- e = find_first('ns:entry', :ns => ATOM_NS) ||
118
- find_first('entry', :ns => ATOM_NS)
119
-
120
- unless e
121
- self << (e = XMPPNode.new('entry', self.document))
122
- e.namespace = ATOM_NS
106
+ children.map &:remove
107
+ return unless payload
108
+ if payload.is_a?(String)
109
+ self.content = payload
110
+ else
111
+ self << payload
123
112
  end
124
- e
125
113
  end
126
114
  end # PubSubItem
127
115
 
@@ -33,6 +33,17 @@ class Stanza
33
33
  new_node
34
34
  end
35
35
 
36
+ def self.find_or_create(parent)
37
+ if found_x = parent.find_first('//ns:x', :ns => self.registered_ns)
38
+ x = self.new found_x
39
+ found_x.remove
40
+ else
41
+ x = self.new
42
+ end
43
+ parent << x
44
+ x
45
+ end
46
+
36
47
  # The Form's type
37
48
  # @return [Symbol]
38
49
  def type
@@ -148,8 +159,8 @@ class Stanza
148
159
  # @overload new(opts = {})
149
160
  # Creates a new Field using a hash of options
150
161
  # @param [Hash] opts a hash of options
151
- # @option opts [:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single"] :type the type of the field
152
162
  # @option opts [String] :var the variable for the field
163
+ # @option opts [:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single"] :type the type of the field
153
164
  # @option opts [String] :label the label for the field
154
165
  # @option [String, nil] :value the value for the field
155
166
  # @option [String, nil] :description the description for the field
@@ -158,31 +169,31 @@ class Stanza
158
169
  # These are passed directly to X::Field::Option.new
159
170
  # @overload new(type, var = nil, label = nil)
160
171
  # Create a new Field by name
161
- # @param [:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single"] type the type of the field
162
172
  # @param [String, nil] var the variable for the field
173
+ # @param [:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single"] type the type of the field
163
174
  # @param [String, nil] label the label for the field
164
175
  # @param [String, nil] value the value for the field
165
176
  # @param [String, nil] description the description for the field
166
177
  # @param [true, false, nil] required the required flag for the field
167
178
  # @param [Array<Array, X::Field::Option>, nil] options a list of field options.
168
179
  # These are passed directly to X::Field::Option.new
169
- def self.new(type, var = nil, label = nil, value = nil, description = nil, required = false, options = [])
180
+ def self.new(var, type = nil, label = nil, value = nil, description = nil, required = false, options = [])
170
181
  new_node = super :field
171
182
 
172
- case type
183
+ case var
173
184
  when Nokogiri::XML::Node
174
- new_node.inherit type
185
+ new_node.inherit var
175
186
  when Hash
176
- new_node.type = type[:type]
177
- new_node.var = type[:var]
178
- new_node.label = type[:label]
179
- new_node.value = type[:value]
180
- new_node.desc = type[:description]
181
- new_node.required = type[:required]
182
- new_node.options = type[:options]
187
+ new_node.var = var[:var]
188
+ new_node.type = var[:type]
189
+ new_node.label = var[:label]
190
+ new_node.value = var[:value]
191
+ new_node.desc = var[:description]
192
+ new_node.required = var[:required]
193
+ new_node.options = var[:options]
183
194
  else
184
- new_node.type = type
185
195
  new_node.var = var
196
+ new_node.type = type
186
197
  new_node.label = label
187
198
  new_node.value = value
188
199
  new_node.desc = description
@@ -384,4 +395,4 @@ class Stanza
384
395
  end # X
385
396
 
386
397
  end #Stanza
387
- end
398
+ end
@@ -1,21 +1,27 @@
1
1
  require File.join(File.dirname(__FILE__), *%w[.. .. spec_helper])
2
2
 
3
+ def message_xml
4
+ <<-XML
5
+ <message
6
+ to='romeo@example.net'
7
+ from='juliet@example.com/balcony'
8
+ type='chat'
9
+ xml:lang='en'>
10
+ <body>Wherefore art thou, Romeo?</body>
11
+ <x xmlns='jabber:x:data' type='form'>
12
+ <field var='field-name' type='text-single' label='description' />
13
+ </x>
14
+ </message>
15
+ XML
16
+ end
17
+
3
18
  describe Blather::Stanza::Message do
4
19
  it 'registers itself' do
5
20
  Blather::XMPPNode.class_from_registration(:message, nil).must_equal Blather::Stanza::Message
6
21
  end
7
22
 
8
23
  it 'must be importable' do
9
- doc = parse_stanza <<-XML
10
- <message
11
- to='romeo@example.net'
12
- from='juliet@example.com/balcony'
13
- type='chat'
14
- xml:lang='en'>
15
- <body>Wherefore art thou, Romeo?</body>
16
- </message>
17
- XML
18
- Blather::XMPPNode.import(doc.root).must_be_instance_of Blather::Stanza::Message
24
+ Blather::XMPPNode.import(parse_stanza(message_xml).root).must_be_instance_of Blather::Stanza::Message
19
25
  end
20
26
 
21
27
  it 'provides "attr_accessor" for body' do
@@ -129,4 +135,15 @@ describe Blather::Stanza::Message do
129
135
  msg.xhtml = xhtml
130
136
  msg.xhtml.must_equal(xhtml)
131
137
  end
138
+
139
+ it 'makes a form child available' do
140
+ n = Blather::XMPPNode.import(parse_stanza(message_xml).root)
141
+ n.form.fields.size.must_equal 1
142
+ n.form.fields.map { |f| f.class }.uniq.must_equal [Blather::Stanza::X::Field]
143
+ n.form.must_be_instance_of Blather::Stanza::X
144
+
145
+ r = Blather::Stanza::Message.new
146
+ r.form.type = :form
147
+ r.form.type.must_equal :form
148
+ end
132
149
  end
@@ -59,9 +59,4 @@ describe Blather::Stanza::PubSub::PubSubItem do
59
59
  item.payload = nil
60
60
  item.payload.must_be_nil
61
61
  end
62
-
63
- it 'must have an entry child to item' do
64
- item = Blather::Stanza::PubSub::Items::PubSubItem.new 'foo', 'payload'
65
- item.find_first('ns:entry', :ns => 'http://www.w3.org/2005/Atom').wont_be_nil
66
- end
67
62
  end
@@ -94,8 +94,8 @@ describe Blather::Stanza::X do
94
94
  {:label => 'label1', :type => 'text-single', :var => 'var1'},
95
95
  ]
96
96
 
97
- control = [ Blather::Stanza::X::Field.new(*%w[text-single var label]),
98
- Blather::Stanza::X::Field.new(*%w[text-single var1 label1])]
97
+ control = [ Blather::Stanza::X::Field.new(*%w[var text-single label]),
98
+ Blather::Stanza::X::Field.new(*%w[var1 text-single label1])]
99
99
 
100
100
  di = Blather::Stanza::X.new nil, fields
101
101
  di.fields.size.must_equal 2
@@ -103,8 +103,8 @@ describe Blather::Stanza::X do
103
103
  end
104
104
 
105
105
  it 'takes a list of Field objects as fields' do
106
- control = [ Blather::Stanza::X::Field.new(*%w[text-single var label1]),
107
- Blather::Stanza::X::Field.new(*%w[text-single var1 label1])]
106
+ control = [ Blather::Stanza::X::Field.new(*%w[var text-single label1]),
107
+ Blather::Stanza::X::Field.new(*%w[var1 text-single label1])]
108
108
 
109
109
  di = Blather::Stanza::X.new nil, control
110
110
  di.fields.size.must_equal 2
@@ -114,11 +114,11 @@ describe Blather::Stanza::X do
114
114
  it 'takes a mix of hashes and field objects as fields' do
115
115
  fields = [
116
116
  {:label => 'label', :type => 'text-single', :var => 'var'},
117
- Blather::Stanza::X::Field.new(*%w[text-single var1 label1]),
117
+ Blather::Stanza::X::Field.new(*%w[var1 text-single label1]),
118
118
  ]
119
119
 
120
- control = [ Blather::Stanza::X::Field.new(*%w[text-single var label]),
121
- Blather::Stanza::X::Field.new(*%w[text-single var1 label1])]
120
+ control = [ Blather::Stanza::X::Field.new(*%w[var text-single label]),
121
+ Blather::Stanza::X::Field.new(*%w[var1 text-single label1])]
122
122
 
123
123
  di = Blather::Stanza::X.new nil, fields
124
124
  di.fields.size.must_equal 2
@@ -130,7 +130,7 @@ describe Blather::Stanza::X do
130
130
  di.fields.size.must_equal 0
131
131
  di.fields = [{:label => 'label', :type => 'text-single', :var => 'var', :required => true}]
132
132
  di.fields.size.must_equal 1
133
- di.fields += [Blather::Stanza::X::Field.new(*%w[text-single var1 label1])]
133
+ di.fields += [Blather::Stanza::X::Field.new(*%w[var1 text-single label1])]
134
134
  di.fields.size.must_equal 2
135
135
  end
136
136
 
@@ -146,28 +146,28 @@ describe Blather::Stanza::X::Field do
146
146
  end
147
147
 
148
148
  it 'has a type attribute' do
149
- n = Blather::Stanza::X::Field.new 'text-single'
149
+ n = Blather::Stanza::X::Field.new 'var', 'text-single'
150
150
  n.type.must_equal 'text-single'
151
151
  n.type = 'hidden'
152
152
  n.type.must_equal 'hidden'
153
153
  end
154
154
 
155
155
  it 'has a var attribute' do
156
- n = Blather::Stanza::X::Field.new 'text-single', 'name'
156
+ n = Blather::Stanza::X::Field.new 'name', 'text-single'
157
157
  n.var.must_equal 'name'
158
158
  n.var = 'email'
159
159
  n.var.must_equal 'email'
160
160
  end
161
161
 
162
162
  it 'has a label attribute' do
163
- n = Blather::Stanza::X::Field.new 'text-single', 'subject', 'Music from the time of Shakespeare'
163
+ n = Blather::Stanza::X::Field.new 'subject', 'text-single', 'Music from the time of Shakespeare'
164
164
  n.label.must_equal 'Music from the time of Shakespeare'
165
165
  n.label = 'Books by and about Shakespeare'
166
166
  n.label.must_equal 'Books by and about Shakespeare'
167
167
  end
168
168
 
169
169
  it 'has a desc attribute' do
170
- n = Blather::Stanza::X::Field.new 'text-single', 'subject', 'Music from the time of Shakespeare'
170
+ n = Blather::Stanza::X::Field.new 'subject', 'text-single', 'Music from the time of Shakespeare'
171
171
  n.desc.must_equal nil
172
172
  n.desc = 'Books by and about Shakespeare'
173
173
  n.desc.must_equal 'Books by and about Shakespeare'
@@ -176,7 +176,7 @@ describe Blather::Stanza::X::Field do
176
176
  end
177
177
 
178
178
  it 'has a required? attribute' do
179
- n = Blather::Stanza::X::Field.new 'text-single', 'subject', 'Music from the time of Shakespeare'
179
+ n = Blather::Stanza::X::Field.new 'subject', 'text-single', 'Music from the time of Shakespeare'
180
180
  n.required?.must_equal false
181
181
  n.required = true
182
182
  n.required?.must_equal true
@@ -185,7 +185,7 @@ describe Blather::Stanza::X::Field do
185
185
  end
186
186
 
187
187
  it 'has a value attribute' do
188
- n = Blather::Stanza::X::Field.new 'text-single', 'subject', 'Music from the time of Shakespeare'
188
+ n = Blather::Stanza::X::Field.new 'subject', 'text-single', 'Music from the time of Shakespeare'
189
189
  n.value.must_equal nil
190
190
  n.value = 'book1'
191
191
  n.value.must_equal 'book1'
@@ -204,14 +204,14 @@ describe Blather::Stanza::X::Field do
204
204
  end
205
205
 
206
206
  it 'raises an error when compared against a non X::Field' do
207
- a = Blather::Stanza::X::Field.new('hidden', 'secret_message')
207
+ a = Blather::Stanza::X::Field.new('secret_message', 'hidden')
208
208
  lambda { a == 'test' }.must_raise RuntimeError
209
209
  end
210
210
 
211
211
  it 'can determine equality' do
212
- a = Blather::Stanza::X::Field.new('text-single', 'subject')
213
- a.must_equal Blather::Stanza::X::Field.new('text-single', 'subject')
214
- a.wont_equal Blather::Stanza::X::Field.new('text-single', 'subject1')
212
+ a = Blather::Stanza::X::Field.new('subject', 'text-single')
213
+ a.must_equal Blather::Stanza::X::Field.new('subject', 'text-single')
214
+ a.wont_equal Blather::Stanza::X::Field.new('subject1', 'text-single')
215
215
  end
216
216
  end
217
217
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blather
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 11
10
- version: 0.4.11
9
+ - 12
10
+ version: 0.4.12
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeff Smick
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-29 00:00:00 -07:00
18
+ date: 2010-08-09 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency