Class: Zm::Client::SoapElement

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/client/soap_request/soap_element.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, namespace) ⇒ SoapElement

Returns a new instance of SoapElement.



26
27
28
29
30
31
32
# File 'lib/zm/client/soap_request/soap_element.rb', line 26

def initialize(name, namespace)
  @name = name
  @namespace = namespace
  @attributes = {}
  @nodes = []
  @content = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/zm/client/soap_request/soap_element.rb', line 24

def name
  @name
end

Class Method Details

.account(name) ⇒ Object



11
12
13
# File 'lib/zm/client/soap_request/soap_element.rb', line 11

def (name)
  new(name, SoapAccountConstants::NAMESPACE_STR)
end

.admin(name) ⇒ Object



19
20
21
# File 'lib/zm/client/soap_request/soap_element.rb', line 19

def admin(name)
  new(name, SoapAdminConstants::NAMESPACE_STR)
end

.create(name) ⇒ Object



7
8
9
# File 'lib/zm/client/soap_request/soap_element.rb', line 7

def create(name)
  new(name, nil)
end

.mail(name) ⇒ Object



15
16
17
# File 'lib/zm/client/soap_request/soap_element.rb', line 15

def mail(name)
  new(name, SoapMailConstants::NAMESPACE_STR)
end

Instance Method Details

#add_attribute(key, value) ⇒ Object



34
35
36
37
# File 'lib/zm/client/soap_request/soap_element.rb', line 34

def add_attribute(key, value)
  @attributes[key.to_sym] = value
  self
end

#add_attributes(hash) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/zm/client/soap_request/soap_element.rb', line 39

def add_attributes(hash)
  return self if hash.nil?

  hash.transform_keys!(&:to_sym)
  @attributes.merge!(hash)
  self
end

#add_content(content) ⇒ Object



47
48
49
50
# File 'lib/zm/client/soap_request/soap_element.rb', line 47

def add_content(content)
  @content = content
  self
end

#add_node(node) ⇒ Object



52
53
54
55
# File 'lib/zm/client/soap_request/soap_element.rb', line 52

def add_node(node)
  @nodes << node
  self
end

#propertiesObject



80
81
82
83
84
85
86
87
# File 'lib/zm/client/soap_request/soap_element.rb', line 80

def properties
  return @content.map { |content| { _content: content } } if @content.is_a?(Array)

  hash = @namespace.nil? ? {} : { _jsns: @namespace }
  hash.merge!(@attributes) unless @attributes.empty?
  hash.merge!({ _content: @content }) unless @content.nil?
  hash
end

#to_hashObject



76
77
78
# File 'lib/zm/client/soap_request/soap_element.rb', line 76

def to_hash
  { @name => to_struct }
end

#to_json(*_args) ⇒ Object



57
58
59
# File 'lib/zm/client/soap_request/soap_element.rb', line 57

def to_json(*_args)
  to_hash.to_json
end

#to_structObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zm/client/soap_request/soap_element.rb', line 61

def to_struct
  struct = properties

  @nodes.each do |node|
    if struct[node.name].nil?
      struct.merge!(node.to_hash)
    else
      struct[node.name] = [struct[node.name]] unless struct[node.name].is_a?(Array)
      struct[node.name] << node.to_struct
    end
  end

  struct
end