Class: CoreLibrary::XmlHelper
- Inherits:
-
Object
- Object
- CoreLibrary::XmlHelper
- Defined in:
- lib/apimatic-core/utilities/xml_helper.rb
Overview
A utility class for handling xml parsing.
Class Method Summary collapse
-
.add_array_as_subelement(doc, root, item_name, items, wrapping_element_name: nil, datetime_format: nil) ⇒ Object
Adds array as a sub-element.
-
.add_as_attribute(root, name, value, datetime_format: nil) ⇒ Object
Adds the value as an attribute.
-
.add_as_subelement(doc, root, name, value, datetime_format: nil) ⇒ Object
Adds as a sub-element.
-
.add_hash_as_subelement(doc, root, name, entries, datetime_format: nil) ⇒ Object
Adds hash as a sub-element.
-
.convert(value, clazz, datetime_format) ⇒ Object
Basic convert method.
-
.datetime_to_s(value, datetime_format) ⇒ Object
Converts datetime to string of a specific format.
-
.deserialize_xml(xml, root_element_name, clazz, datetime_format = nil) ⇒ Object
Deserializes XML to a specific class.
-
.deserialize_xml_to_array(xml, root_element_name, item_name, clazz, datetime_format = nil) ⇒ Object
Deserializes XML to an array of a specific class.
-
.deserialize_xml_to_hash(xml, root_element_name, clazz, datetime_format = nil) ⇒ Object
Deserializes XML to an array of a specific class.
-
.from_attribute(parent, name, clazz, datetime_format: nil) ⇒ Object
Converts attribute to a specific class.
-
.from_element(parent, name, clazz, datetime_format: nil) ⇒ Object
Converts element to a specific class.
-
.from_element_to_array(parent, item_name, clazz, wrapping_element_name: nil, datetime_format: nil) ⇒ Object
Converts element to an array.
-
.from_element_to_hash(parent, name, clazz, datetime_format: nil) ⇒ Object
Converts element to hash.
-
.serialize_array_to_xml(root_element_name, item_name, value, datetime_format: nil) ⇒ Object
Serializes the provided array value to XML.
-
.serialize_hash_to_xml(root_element_name, entries, datetime_format: nil) ⇒ Object
Serializes the provided hash to XML.
-
.serialize_to_xml(root_element_name, value, datetime_format: nil) ⇒ Object
Serializes the provided value to XML.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |