Class: Blather::Stanza::Iq

Inherits:
Blather::Stanza show all
Defined in:
lib/blather/stanza/iq.rb,
lib/blather/stanza/iq/query.rb,
lib/blather/stanza/iq/vcard.rb,
lib/blather/stanza/iq/roster.rb,
lib/blather/stanza/iq/command.rb

Overview

# Iq Stanza

[RFC 3920 Section 9.2.3 - IQ Semantics](xmpp.org/rfcs/rfc3920.html#rfc.section.9.2.3)

Info/Query, or IQ, is a request-response mechanism, similar in some ways to HTTP. The semantics of IQ enable an entity to make a request of, and receive a response from, another entity. The data content of the request and response is defined by the namespace declaration of a direct child element of the IQ element, and the interaction is tracked by the requesting entity through use of the ‘id’ attribute. Thus, IQ interactions follow a common pattern of structured data exchange such as get/result or set/result (although an error may be returned in reply to a request if appropriate).

## “ID” Attribute

Iq Stanzas require the ID attribute be set. Blather will handle this automatically when a new Iq is created.

## “Type” Attribute

  • ‘:get` – The stanza is a request for information or requirements.

  • ‘:set` – The stanza provides required data, sets new values, or replaces existing values.

  • ‘:result` – The stanza is a response to a successful get or set request.

  • ‘:error` – An error has occurred regarding processing or delivery of a previously-sent get or set (see Stanza Errors).

Blather provides a helper for each possible type:

Iq#get?
Iq#set?
Iq#result?
Iq#error?

Blather treats the ‘type` attribute like a normal ruby object attribute providing a getter and setter. The default `type` is `get`.

iq = Iq.new
iq.type               # => :get
iq.get?               # => true
iq.type = :set
iq.set?               # => true
iq.get?               # => false

iq.type = :invalid    # => RuntimeError

Direct Known Subclasses

Command, Query, Vcard, PubSub, PubSubOwner

Defined Under Namespace

Classes: Command, Query, Roster, Vcard

Constant Summary collapse

VALID_TYPES =
[:get, :set, :result, :error].freeze

Constants inherited from XMPPNode

XMPPNode::BASE_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Blather::Stanza

#as_error, #from, #from=, handler_list, #id, #id=, next_id, register, #reply, #to, #to=, #type

Methods inherited from XMPPNode

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

.import(node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/blather/stanza/iq.rb', line 61

def self.import(node)
  klass = nil
  node.children.detect do |e|
    ns = e.namespace ? e.namespace.href : nil
    klass = class_from_registration(e.element_name, ns)
  end

  if klass && klass != self
    klass.import(node)
  else
    new(node[:type]).inherit(node)
  end
end

.new(type = nil, to = nil, id = nil) ⇒ Object

Create a new Iq

Parameters:

  • type (Symbol, nil) (defaults to: nil)

    the type of stanza (:get, :set, :result, :error)

  • jid (Blather::JID, String, nil)

    the JID of the inteded recipient

  • id (#to_s) (defaults to: nil)

    the stanza’s ID. Leaving this nil will set the ID to the next unique number



81
82
83
84
85
86
87
# File 'lib/blather/stanza/iq.rb', line 81

def self.new(type = nil, to = nil, id = nil)
  node = super :iq
  node.type = type || :get
  node.to = to
  node.id = id || self.next_id
  node
end

Instance Method Details

#error?true, false

Check if the IQ is of type :error

Returns:

  • (true, false)


113
114
115
# File 'lib/blather/stanza/iq.rb', line 113

def error?
  self.type == :error
end

#get?true, false

Check if the IQ is of type :get

Returns:

  • (true, false)


92
93
94
# File 'lib/blather/stanza/iq.rb', line 92

def get?
  self.type == :get
end

#reply!self

Overrides the parent method to ensure the reply is of type :result

Returns:

  • (self)


130
131
132
133
134
# File 'lib/blather/stanza/iq.rb', line 130

def reply!
  super
  self.type = :result
  self
end

#result?true, false

Check if the IQ is of type :result

Returns:

  • (true, false)


106
107
108
# File 'lib/blather/stanza/iq.rb', line 106

def result?
  self.type == :result
end

#set?true, false

Check if the IQ is of type :set

Returns:

  • (true, false)


99
100
101
# File 'lib/blather/stanza/iq.rb', line 99

def set?
  self.type == :set
end

#type=(type) ⇒ Object

Ensures type is :get, :set, :result or :error

Parameters:

  • type (#to_sym)

    the Iq type. Must be one of VALID_TYPES



120
121
122
123
124
125
# File 'lib/blather/stanza/iq.rb', line 120

def type=(type)
  if type && !VALID_TYPES.include?(type.to_sym)
    raise ArgumentError, "Invalid Type (#{type}), use: #{VALID_TYPES*' '}"
  end
  super
end