Class: CoreLibrary::XmlHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic-core/utilities/xml_helper.rb

Overview

A utility class for handling xml parsing.

Class Method Summary collapse

Class Method Details

.add_array_as_subelement(doc, root, item_name, items, wrapping_element_name: nil, datetime_format: nil) ⇒ Object

Adds array as a sub-element.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 64

def add_array_as_subelement(doc, root, item_name, items,
                            wrapping_element_name: nil,
                            datetime_format: nil)
  return if items.nil?

  if wrapping_element_name.nil?
    parent = root
  else
    parent = doc.create_element(wrapping_element_name)
    root.add_child(parent)
  end

  items.each do |item|
    add_as_subelement(doc, parent, item_name, item,
                      datetime_format: datetime_format)
  end
end

.add_as_attribute(root, name, value, datetime_format: nil) ⇒ Object

Adds the value as an attribute.



41
42
43
44
45
46
47
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 41

def add_as_attribute(root, name, value, datetime_format: nil)
  return if value.nil?

  value = datetime_to_s(value, datetime_format) if value.instance_of?(DateTime)

  root[name] = value
end

.add_as_subelement(doc, root, name, value, datetime_format: nil) ⇒ Object

Adds as a sub-element.



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 83

def add_as_subelement(doc, root, name, value, datetime_format: nil)
  return if value.nil?

  value = datetime_to_s(value, datetime_format) if value.instance_of?(DateTime)

  element = if value.respond_to? :to_xml_element
              value.to_xml_element(doc, name)
            else
              doc.create_element(name, value)
            end

  root.add_child(element)
end

.add_hash_as_subelement(doc, root, name, entries, datetime_format: nil) ⇒ Object

Adds hash as a sub-element.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 50

def add_hash_as_subelement(doc, root, name, entries,
                           datetime_format: nil)
  return if entries.nil?

  parent = doc.create_element(name)
  root.add_child(parent)

  entries.each do |key, value|
    add_as_subelement(doc, parent, key, value,
                      datetime_format: datetime_format)
  end
end

.convert(value, clazz, datetime_format) ⇒ Object

Basic convert method.



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 200

def convert(value, clazz, datetime_format)
  if clazz == DateTime
    return DateTime.rfc3339(value) if datetime_format == 'RFC3339DateTime'
    return DateTime.httpdate(value) if datetime_format == 'HttpDateTime'
    return DateTime.strptime(value, '%s') if datetime_format == 'UnixDateTime'
  end

  return value.to_f if clazz == Float
  return value.to_i if clazz == Integer
  return value.casecmp('true').zero? if clazz == TrueClass

  value
end

.datetime_to_s(value, datetime_format) ⇒ Object

Converts datetime to string of a specific format.

Parameters:

  • value

    Value to convert to string.

  • datetime_format

    Datetime format for the converted string.



100
101
102
103
104
105
106
107
108
109
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 100

def datetime_to_s(value, datetime_format)
  case datetime_format
  when 'UnixDateTime'
    value.to_time.to_i
  when 'HttpDateTime'
    value.httpdate
  else
    value
  end
end

.deserialize_xml(xml, root_element_name, clazz, datetime_format = nil) ⇒ Object

Deserializes XML to a specific class.

Parameters:

  • xml

    The XML value to deserialize.

  • root_element_name

    Root element name for the XML provided.

  • clazz

    The class to convert the XML into.



115
116
117
118
119
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 115

def deserialize_xml(xml, root_element_name, clazz, datetime_format = nil)
  doc = Nokogiri::XML::Document.parse xml
  from_element(doc, root_element_name, clazz,
               datetime_format: datetime_format)
end

.deserialize_xml_to_array(xml, root_element_name, item_name, clazz, datetime_format = nil) ⇒ Object

Deserializes XML to an array of a specific class.

Parameters:

  • xml

    The XML value to deserialize.

  • root_element_name

    Root element name for the XML provided.

  • item_name

    Item name for XML.

  • clazz

    The class to convert the XML into.



126
127
128
129
130
131
132
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 126

def deserialize_xml_to_array(xml, root_element_name, item_name, clazz,
                             datetime_format = nil)
  doc = Nokogiri::XML::Document.parse xml
  from_element_to_array(doc, item_name, clazz,
                        wrapping_element_name: root_element_name,
                        datetime_format: datetime_format)
end

.deserialize_xml_to_hash(xml, root_element_name, clazz, datetime_format = nil) ⇒ Object

Deserializes XML to an array of a specific class.

Parameters:

  • xml

    The XML value to deserialize.

  • root_element_name

    Root element name for the XML provided.



137
138
139
140
141
142
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 137

def deserialize_xml_to_hash(xml, root_element_name, clazz,
                            datetime_format = nil)
  doc = Nokogiri::XML::Document.parse xml
  from_element_to_hash(doc, root_element_name, clazz,
                       datetime_format: datetime_format)
end

.from_attribute(parent, name, clazz, datetime_format: nil) ⇒ Object

Converts attribute to a specific class.



145
146
147
148
149
150
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 145

def from_attribute(parent, name, clazz, datetime_format: nil)
  attribute = parent[name]
  return nil if attribute.nil?

  convert(attribute, clazz, datetime_format)
end

.from_element(parent, name, clazz, datetime_format: nil) ⇒ Object

Converts element to a specific class.



153
154
155
156
157
158
159
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 153

def from_element(parent, name, clazz, datetime_format: nil)
  element = parent.at_xpath(name)
  return nil if element.nil?
  return clazz.from_element element if clazz.respond_to? :from_element

  convert(element.text, clazz, datetime_format)
end

.from_element_to_array(parent, item_name, clazz, wrapping_element_name: nil, datetime_format: nil) ⇒ Object

Converts element to an array.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 162

def from_element_to_array(parent, item_name, clazz,
                          wrapping_element_name: nil,
                          datetime_format: nil)
  elements = if wrapping_element_name.nil?
               parent.xpath(item_name)
             elsif parent.at_xpath(wrapping_element_name).nil?
               nil
             else
               parent.at_xpath(wrapping_element_name).xpath(item_name)
             end

  return nil if elements.nil?

  if clazz.respond_to? :from_element
    elements.map { |element| clazz.from_element element }
  else
    elements.map do |element|
      convert(element.text, clazz, datetime_format)
    end
  end
end

.from_element_to_hash(parent, name, clazz, datetime_format: nil) ⇒ Object

Converts element to hash.



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 185

def from_element_to_hash(parent, name, clazz,
                         datetime_format: nil)
  entries = parent.at_xpath(name)
  return nil if entries.nil? || entries.children.nil?

  hash = {}

  entries.element_children.each do |element|
    hash[element.name] = convert(element.text, clazz, datetime_format)
  end

  hash
end

.serialize_array_to_xml(root_element_name, item_name, value, datetime_format: nil) ⇒ Object

Serialized the provided array value to XML.

Parameters:

  • root_element_name

    Root element for the xml provided.

  • item_name

    Item name for XML.

  • value

    Value to convert to XML.



21
22
23
24
25
26
27
28
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 21

def serialize_array_to_xml(root_element_name, item_name, value,
                           datetime_format: nil)
  doc = Nokogiri::XML::Document.new
  add_array_as_subelement(doc, doc, item_name, value,
                          wrapping_element_name: root_element_name,
                          datetime_format: datetime_format)
  doc.to_xml
end

.serialize_hash_to_xml(root_element_name, entries, datetime_format: nil) ⇒ Object

Serialized the provided hash to XML.

Parameters:

  • root_element_name

    Root element for the XML provided.



32
33
34
35
36
37
38
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 32

def serialize_hash_to_xml(root_element_name, entries,
                          datetime_format: nil)
  doc = Nokogiri::XML::Document.new
  add_hash_as_subelement(doc, doc, root_element_name, entries,
                         datetime_format: datetime_format)
  doc.to_xml
end

.serialize_to_xml(root_element_name, value, datetime_format: nil) ⇒ Object

Serialized the provided value to XML.

Parameters:

  • root_element_name

    Root element for the XML provided.

  • value

    Value to convert to XML.



10
11
12
13
14
15
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 10

def serialize_to_xml(root_element_name, value, datetime_format: nil)
  doc = Nokogiri::XML::Document.new
  add_as_subelement(doc, doc, root_element_name, value,
                    datetime_format: datetime_format)
  doc.to_xml
end