Exception: Blather::StreamError

Inherits:
BlatherError show all
Defined in:
lib/blather/errors/stream_error.rb

Overview

Stream Errors [RFC3920 Section 9.3](xmpp.org/rfcs/rfc3920.html#streams-error-rules)

Constant Summary collapse

STREAM_ERR_NS =
'urn:ietf:params:xml:ns:xmpp-streams'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BlatherError

handler_list, #id, register

Constructor Details

#initialize(name, text = nil, extras = []) ⇒ StreamError

Create a new Stream Error [RFC3920 Section 4.7.2](xmpp.org/rfcs/rfc3920.html#rfc.section.4.7.2)

error

Parameters:

  • name (String)

    the error name

  • text (String, nil) (defaults to: nil)

    optional error text

  • extras (Array<Blather::XMPPNode>) (defaults to: [])

    an array of extras to attach to the



36
37
38
39
40
# File 'lib/blather/errors/stream_error.rb', line 36

def initialize(name, text = nil, extras = [])
  @name = name
  @text = text
  @extras = extras
end

Instance Attribute Details

#extrasObject (readonly)

Returns the value of attribute extras.



13
14
15
# File 'lib/blather/errors/stream_error.rb', line 13

def extras
  @extras
end

#textObject (readonly)

Returns the value of attribute text.



13
14
15
# File 'lib/blather/errors/stream_error.rb', line 13

def text
  @text
end

Class Method Details

.import(node) ⇒ Object

Factory method for instantiating the proper class for the error

Parameters:



18
19
20
21
22
23
24
25
26
27
# File 'lib/blather/errors/stream_error.rb', line 18

def self.import(node)
  name = node.find_first('descendant::*[name()!="text"]', STREAM_ERR_NS).element_name

  text = node.find_first 'descendant::*[name()="text"]', STREAM_ERR_NS
  text = text.content if text

  extras = node.find("descendant::*[namespace-uri()!='#{STREAM_ERR_NS}']").map { |n| n }

  self.new name, text, extras
end

Instance Method Details

#inspectObject Also known as: to_s



77
78
79
# File 'lib/blather/errors/stream_error.rb', line 77

def inspect
  "Stream Error (#{@name}): #{self.text}" + (self.extras.empty? ? '' : " [#{self.extras}]")
end

#nameSymbol

The error name

Returns:

  • (Symbol)


45
46
47
# File 'lib/blather/errors/stream_error.rb', line 45

def name
  @name.gsub('-','_').to_sym
end

#to_nodeBlather::XMPPNode

Creates an XML node from the error

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/blather/errors/stream_error.rb', line 52

def to_node
  node = XMPPNode.new('error')
  node.namespace = {'stream' => Blather::Stream::STREAM_NS}

  node << (err = XMPPNode.new(@name, node.document))
  err.namespace = 'urn:ietf:params:xml:ns:xmpp-streams'

  if self.text
    node << (text = XMPPNode.new('text', node.document))
    text.namespace = 'urn:ietf:params:xml:ns:xmpp-streams'
    text.content = self.text
  end

  self.extras.each { |extra| node << extra.dup }
  node
end

#to_xmlString

Convert the object to a proper node then convert it to a string

Returns:

  • (String)


72
73
74
# File 'lib/blather/errors/stream_error.rb', line 72

def to_xml
  to_node.to_s
end