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.

Parameters:

  • doc (Nokogiri::XML::Document)

    Document to add the hash to.

  • root (REXML::Element)

    Root element of the XML to add the attribute to.

  • item_name (String)

    Individual array item names.

  • items (Array)

    Array of items.

  • wrapping_element_name (String) (defaults to: nil)

    Main array item name.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    The format to convert the date time into.



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

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.

Parameters:

  • root (REXML::Element)

    Root element of the XML to add the attribute to.

  • name (string)

    Attribute name.

  • value (string)

    Attribute value.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    The format to convert the date time into.



49
50
51
52
53
54
55
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 49

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.

Parameters:

  • doc (Nokogiri::XML::Document)

    Document to add the hash to.

  • root (REXML::Element)

    Root element of the XML to add the attribute to.

  • name (String)

    Item name.

  • value (Array)

    Item value.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    The format to convert the date time into.



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 107

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.

Parameters:

  • doc (Nokogiri::XML::Document)

    Document to add the hash to.

  • root (REXML::Element)

    Root element of the XML to add the attribute to.

  • name (string)

    Attribute name.

  • entries (Hash)

    Hash to add as value.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    The format to convert the date time into.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 63

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.

Parameters:

  • value (String)

    Value to convert type of.

  • clazz (Class)

    Type to convert into.

  • datetime_format (CoreLibrary::DateTimeFormat)

    Datetime format to use for conversion..

Returns:

  • (Object)

    Converted value.



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 249

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 (DateTime)

    Value to convert to string.

  • datetime_format (CoreLibrary::DateTimeFormat)

    Datetime format to use for conversion..



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

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 (String)

    The XML value to deserialize.

  • root_element_name (String)

    Root element name for the XML provided.

  • clazz (Class)

    The class to convert the XML into.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    Datetime format to use for conversion..



140
141
142
143
144
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 140

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 (String)

    The XML value to deserialize.

  • root_element_name (String)

    Root element name for the XML provided.

  • item_name (String)

    Item name for XML.

  • clazz (Class)

    The class to convert the XML into.



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

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 (String)

    The XML value to deserialize.

  • root_element_name (String)

    Root element name for the XML provided.

  • clazz (Class)

    The class to convert the XML into.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    Datetime format to use for conversion..



164
165
166
167
168
169
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 164

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.

Parameters:

  • parent (String)

    Parent XML.

  • name (String)

    Attribute name.

  • clazz (Class)

    Attribute class.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    Datetime format to use for conversion..



176
177
178
179
180
181
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 176

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.

Parameters:

  • parent (String)

    Parent XML.

  • name (String)

    Attribute name.

  • clazz (Class)

    Attribute class.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    Datetime format to use for conversion..



188
189
190
191
192
193
194
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 188

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.

Parameters:

  • parent (String)

    Parent XML.

  • item_name (String)

    Element name.

  • clazz (Class)

    Element class.

  • wrapping_element_name (String) (defaults to: nil)

    Main element name.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    Datetime format to use for conversion..



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 202

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.

Parameters:

  • parent (Nokogiri::XML::Document)

    Parent document.

  • name (String)

    Element name.

  • clazz (Class)

    Hash class to convert to.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    Datetime format to use for conversion.

Returns:

  • (Object)

    Converted value.



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 230

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

Serializes the provided array value to XML.

Parameters:

  • root_element_name (String)

    Root element for the xml provided.

  • item_name (String)

    Item name for XML.

  • value

    Value to convert to XML.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    The format to convert the date time into.



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

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

Serializes the provided hash to XML.

Parameters:

  • root_element_name (string)

    Root element for the XML provided.

  • entries (Hash)

    Entries to convert to XML.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    The format to convert the date time into.



36
37
38
39
40
41
42
# File 'lib/apimatic-core/utilities/xml_helper.rb', line 36

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

Serializes the provided value to XML.

Parameters:

  • root_element_name (String)

    Root element for the XML provided.

  • value (String)

    Value to convert to XML.

  • datetime_format (CoreLibrary::DateTimeFormat) (defaults to: nil)

    The format to convert the date time into.



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

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