Class: Blather::Stanza::Iq::Command
- Inherits:
-
Blather::Stanza::Iq
- Object
- Nokogiri::XML::Node
- XMPPNode
- Blather::Stanza
- Blather::Stanza::Iq
- Blather::Stanza::Iq::Command
- Defined in:
- lib/blather/stanza/iq/command.rb
Overview
# Command Stanza
[XEP-0050 Ad-Hoc Commands](xmpp.org/extensions/xep-0050.html)
This is a base class for any command based Iq stanzas. It provides a base set of methods for working with command stanzas
Constant Summary collapse
- VALID_ACTIONS =
[:cancel, :execute, :complete, :next, :prev].freeze
- VALID_STATUS =
[:executing, :completed, :canceled].freeze
- VALID_NOTE_TYPES =
[:info, :warn, :error].freeze
Constants inherited from Blather::Stanza::Iq
Constants inherited from XMPPNode
Class Method Summary collapse
-
.new(type = :set, node = nil, action = :execute) ⇒ Command
Overrides the parent method to ensure a command node is created.
Instance Method Summary collapse
-
#action ⇒ Symbol?
Get the action of the command.
-
#action=(action) ⇒ Object
Set the action of the command.
-
#actions ⇒ Blather::XMPPNode
Command actions accessor If a command actions element exists it will be returned.
-
#allowed_actions ⇒ [Symbol]
Get the command’s allowed actions.
-
#allowed_actions=(allowed_actions) ⇒ Object
Add allowed actions to the command.
-
#cancel? ⇒ true, false
Check if the command action is :cancel.
-
#canceled? ⇒ true, false
Check if the command status is :canceled.
-
#command ⇒ Blather::XMPPNode
Command node accessor If a command node exists it will be returned.
-
#complete? ⇒ true, false
Check if the command action is :complete.
-
#completed? ⇒ true, false
Check if the command status is :completed.
-
#error? ⇒ true, false
Check if the command status is :error.
-
#execute? ⇒ true, false
Check if the command action is :execute.
-
#executing? ⇒ true, false
Check if the command status is :executing.
-
#form ⇒ Object
Returns the command’s x:data form child.
-
#info? ⇒ true, false
Check if the command status is :info.
-
#inherit(node) ⇒ Object
Overrides the parent method to ensure the current command node is destroyed and the action is set to execute if no action provided.
-
#new_sessionid! ⇒ Object
Generate a new session ID (SHA-1 hash).
-
#next? ⇒ true, false
Check if the command action is :next.
-
#node ⇒ String?
Get the name of the node.
-
#node=(node) ⇒ Object
Set the name of the node.
-
#note ⇒ Blather::XMPPNode
Command note accessor If a command note exists it will be returned.
-
#note_text ⇒ Object
Get the text of the command’s note.
-
#note_text=(note_text) ⇒ Object
Set the command’s note text.
-
#note_type ⇒ Symbol?
Get the note_type of the command.
-
#note_type=(note_type) ⇒ Object
Set the note_type of the command.
-
#prev? ⇒ true, false
Check if the command action is :prev.
- #primary_allowed_action ⇒ Object
- #primary_allowed_action=(a) ⇒ Object
-
#remove_allowed_actions! ⇒ Object
Remove allowed actions from the command.
-
#reply! ⇒ self
Overrides the parent method to ensure the reply has no action.
-
#sessionid ⇒ String?
Get the sessionid of the command.
-
#sessionid=(sessionid) ⇒ Object
Set the sessionid of the command.
-
#sessionid? ⇒ true, false
Check if there is a sessionid set.
-
#status ⇒ Symbol?
Get the status of the command.
-
#status=(status) ⇒ Object
Set the status of the command.
-
#warn? ⇒ true, false
Check if the command status is :warn.
Methods inherited from Blather::Stanza::Iq
#get?, import, #result?, #set?, #type=
Methods inherited from Blather::Stanza
#as_error, #from, #from=, handler_list, #id, #id=, next_id, register, #reply, #to, #to=, #type, #type=
Methods inherited from XMPPNode
class_from_registration, #content_from, import, #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
.new(type = :set, node = nil, action = :execute) ⇒ Command
Overrides the parent method to ensure a command node is created
26 27 28 29 30 31 32 |
# File 'lib/blather/stanza/iq/command.rb', line 26 def self.new(type = :set, node = nil, action = :execute) new_node = super type new_node.command new_node.node = node new_node.action = action new_node end |
Instance Method Details
#action ⇒ Symbol?
Get the action of the command
116 117 118 |
# File 'lib/blather/stanza/iq/command.rb', line 116 def action (val = command[:action]) && val.to_sym end |
#action=(action) ⇒ Object
Set the action of the command
158 159 160 161 162 163 |
# File 'lib/blather/stanza/iq/command.rb', line 158 def action=(action) if action && !VALID_ACTIONS.include?(action.to_sym) raise ArgumentError, "Invalid Action (#{action}), use: #{VALID_ACTIONS*' '}" end command[:action] = action end |
#actions ⇒ Blather::XMPPNode
Command actions accessor If a command actions element exists it will be returned. Otherwise a new actions element will be created and returned
208 209 210 211 212 213 214 |
# File 'lib/blather/stanza/iq/command.rb', line 208 def actions unless a = self.command.find_first('ns:actions', :ns => self.class.registered_ns) (self.command << (a = XMPPNode.new('actions', self.document))) a.namespace = self.command.namespace end a end |
#allowed_actions ⇒ [Symbol]
Get the command’s allowed actions
219 220 221 |
# File 'lib/blather/stanza/iq/command.rb', line 219 def allowed_actions ([:execute] + actions.children.map { |action| action.name.to_sym }).uniq end |
#allowed_actions=(allowed_actions) ⇒ Object
Add allowed actions to the command
237 238 239 240 241 242 243 244 |
# File 'lib/blather/stanza/iq/command.rb', line 237 def allowed_actions=(allowed_actions) allowed_actions = ([allowed_actions].flatten.map(&:to_sym) + [:execute]).uniq if (invalid_actions = allowed_actions - VALID_ACTIONS).size > 0 raise ArgumentError, "Invalid Action(s) (#{invalid_actions*' '}), use: #{VALID_ACTIONS*' '}" end actions.children.map(&:remove) allowed_actions.each { |action| actions << XMPPNode.new(action.to_s) } end |
#cancel? ⇒ true, false
Check if the command action is :cancel
123 124 125 |
# File 'lib/blather/stanza/iq/command.rb', line 123 def cancel? self.action == :cancel end |
#canceled? ⇒ true, false
Check if the command status is :canceled
189 190 191 |
# File 'lib/blather/stanza/iq/command.rb', line 189 def canceled? self.status == :canceled end |
#command ⇒ Blather::XMPPNode
Command node accessor If a command node exists it will be returned. Otherwise a new node will be created and returned
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/blather/stanza/iq/command.rb', line 59 def command c = if self.class.registered_ns find_first('ns:command', :ns => self.class.registered_ns) else find_first('command') end unless c (self << (c = XMPPNode.new('command', self.document))) c.namespace = self.class.registered_ns end c end |
#complete? ⇒ true, false
Check if the command action is :complete
137 138 139 |
# File 'lib/blather/stanza/iq/command.rb', line 137 def complete? self.action == :complete end |
#completed? ⇒ true, false
Check if the command status is :completed
182 183 184 |
# File 'lib/blather/stanza/iq/command.rb', line 182 def completed? self.status == :completed end |
#error? ⇒ true, false
Check if the command status is :error
290 291 292 |
# File 'lib/blather/stanza/iq/command.rb', line 290 def error? self.status == :error end |
#execute? ⇒ true, false
Check if the command action is :execute
130 131 132 |
# File 'lib/blather/stanza/iq/command.rb', line 130 def execute? self.action == :execute end |
#executing? ⇒ true, false
Check if the command status is :executing
175 176 177 |
# File 'lib/blather/stanza/iq/command.rb', line 175 def executing? self.status == :executing end |
#form ⇒ Object
Returns the command’s x:data form child
317 318 319 |
# File 'lib/blather/stanza/iq/command.rb', line 317 def form X.find_or_create command end |
#info? ⇒ true, false
Check if the command status is :info
276 277 278 |
# File 'lib/blather/stanza/iq/command.rb', line 276 def info? self.note_type == :info end |
#inherit(node) ⇒ Object
Overrides the parent method to ensure the current command node is destroyed and the action is set to execute if no action provided
38 39 40 41 42 43 |
# File 'lib/blather/stanza/iq/command.rb', line 38 def inherit(node) command.remove super self.action = :execute unless self.action self end |
#new_sessionid! ⇒ Object
Generate a new session ID (SHA-1 hash)
109 110 111 |
# File 'lib/blather/stanza/iq/command.rb', line 109 def new_sessionid! self.sessionid = "commandsession-#{id}" end |
#next? ⇒ true, false
Check if the command action is :next
144 145 146 |
# File 'lib/blather/stanza/iq/command.rb', line 144 def next? self.action == :next end |
#node ⇒ String?
Get the name of the node
76 77 78 |
# File 'lib/blather/stanza/iq/command.rb', line 76 def node command[:node] end |
#node=(node) ⇒ Object
Set the name of the node
83 84 85 |
# File 'lib/blather/stanza/iq/command.rb', line 83 def node=(node) command[:node] = node end |
#note ⇒ Blather::XMPPNode
Command note accessor If a command note exists it will be returned. Otherwise a new note will be created and returned
258 259 260 261 262 263 264 |
# File 'lib/blather/stanza/iq/command.rb', line 258 def note unless n = self.command.find_first('ns:note', :ns => self.class.registered_ns) (self.command << (n = XMPPNode.new('note', self.document))) n.namespace = self.command.namespace end n end |
#note_text ⇒ Object
Get the text of the command’s note
305 306 307 |
# File 'lib/blather/stanza/iq/command.rb', line 305 def note_text content_from :note end |
#note_text=(note_text) ⇒ Object
Set the command’s note text
312 313 314 |
# File 'lib/blather/stanza/iq/command.rb', line 312 def note_text=(note_text) set_content_for :note, note_text end |
#note_type ⇒ Symbol?
Get the note_type of the command
269 270 271 |
# File 'lib/blather/stanza/iq/command.rb', line 269 def note_type (val = note[:type]) && val.to_sym end |
#note_type=(note_type) ⇒ Object
Set the note_type of the command
297 298 299 300 301 302 |
# File 'lib/blather/stanza/iq/command.rb', line 297 def note_type=(note_type) if note_type && !VALID_NOTE_TYPES.include?(note_type.to_sym) raise ArgumentError, "Invalid Action (#{note_type}), use: #{VALID_NOTE_TYPES*' '}" end note[:type] = note_type end |
#prev? ⇒ true, false
Check if the command action is :prev
151 152 153 |
# File 'lib/blather/stanza/iq/command.rb', line 151 def prev? self.action == :prev end |
#primary_allowed_action ⇒ Object
223 224 225 |
# File 'lib/blather/stanza/iq/command.rb', line 223 def primary_allowed_action (actions[:execute] || :execute).to_sym end |
#primary_allowed_action=(a) ⇒ Object
227 228 229 230 231 232 |
# File 'lib/blather/stanza/iq/command.rb', line 227 def primary_allowed_action=(a) if a && ![:prev, :next, :complete, :execute].include?(a.to_sym) raise ArgumentError, "Invalid Action (#{a}), use: #{[:prev, :next, :complete, :execute]*' '}" end actions[:execute] = a end |
#remove_allowed_actions! ⇒ Object
Remove allowed actions from the command
249 250 251 |
# File 'lib/blather/stanza/iq/command.rb', line 249 def remove_allowed_actions! actions.remove end |
#reply! ⇒ self
Overrides the parent method to ensure the reply has no action
48 49 50 51 52 |
# File 'lib/blather/stanza/iq/command.rb', line 48 def reply! super self.action = nil self end |
#sessionid ⇒ String?
Get the sessionid of the command
90 91 92 |
# File 'lib/blather/stanza/iq/command.rb', line 90 def sessionid command[:sessionid] end |
#sessionid=(sessionid) ⇒ Object
Set the sessionid of the command
104 105 106 |
# File 'lib/blather/stanza/iq/command.rb', line 104 def sessionid=(sessionid) command[:sessionid] = Digest::SHA1.hexdigest(sessionid) end |
#sessionid? ⇒ true, false
Check if there is a sessionid set
97 98 99 |
# File 'lib/blather/stanza/iq/command.rb', line 97 def sessionid? !sessionid.nil? end |
#status ⇒ Symbol?
Get the status of the command
168 169 170 |
# File 'lib/blather/stanza/iq/command.rb', line 168 def status ((val = command[:status]) && val.to_sym) || :executing end |
#status=(status) ⇒ Object
Set the status of the command
196 197 198 199 200 201 |
# File 'lib/blather/stanza/iq/command.rb', line 196 def status=(status) if status && !VALID_STATUS.include?(status.to_sym) raise ArgumentError, "Invalid Action (#{status}), use: #{VALID_STATUS*' '}" end command[:status] = status end |
#warn? ⇒ true, false
Check if the command status is :warn
283 284 285 |
# File 'lib/blather/stanza/iq/command.rb', line 283 def warn? self.status == :warn end |