Module: Wrest::Components::Translators::Xml
- Defined in:
- lib/wrest/components/translators/xml.rb,
lib/wrest/components/translators/xml/conversions.rb
Defined Under Namespace
Modules: Conversions
Class Method Summary collapse
- .build_nokogiri_doc(data) ⇒ Object
- .deserialise(response, options = {}) ⇒ Object
- .deserialize(response, options = {}) ⇒ Object
-
.parse(data) ⇒ Object
Parse an XML Document string or IO into a simple hash using libxml / nokogiri.
- .search(data, xpath) ⇒ Object
- .serialise(hash, _options = {}) ⇒ Object
- .serialize(hash, options = {}) ⇒ Object
- .to_xml(hash, builder = nil) ⇒ Object
Class Method Details
.build_nokogiri_doc(data) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/wrest/components/translators/xml.rb', line 68 def build_nokogiri_doc(data) doc = Nokogiri::XML(data) raise doc.errors.join("\n") if doc.errors.length.positive? doc end |
.deserialise(response, options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/wrest/components/translators/xml.rb', line 20 def deserialise(response, = {}) data = response.body data = StringIO.new(data || '') unless data.respond_to?(:read) return {} if data.eof? if [:xpath].nil? parse(data) else search(data, [:xpath]) end end |
.deserialize(response, options = {}) ⇒ Object
32 33 34 |
# File 'lib/wrest/components/translators/xml.rb', line 32 def deserialize(response, = {}) deserialise(response, ) end |
.parse(data) ⇒ Object
Parse an XML Document string or IO into a simple hash using libxml / nokogiri.
- data
-
XML Document string or IO to parse
60 61 62 |
# File 'lib/wrest/components/translators/xml.rb', line 60 def parse(data) build_nokogiri_doc(data).to_hash end |
.search(data, xpath) ⇒ Object
64 65 66 |
# File 'lib/wrest/components/translators/xml.rb', line 64 def search(data, xpath) build_nokogiri_doc(data).xpath(xpath) end |
.serialise(hash, _options = {}) ⇒ Object
36 37 38 |
# File 'lib/wrest/components/translators/xml.rb', line 36 def serialise(hash, = {}) to_xml(hash) end |
.serialize(hash, options = {}) ⇒ Object
40 41 42 |
# File 'lib/wrest/components/translators/xml.rb', line 40 def serialize(hash, = {}) serialise(hash, ) end |
.to_xml(hash, builder = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/wrest/components/translators/xml.rb', line 44 def to_xml(hash, builder = nil) builder ||= Nokogiri::XML::Builder.new hash.each_with_object(builder) do |(key, value), inner_builder| if value.is_a?(Hash) inner_builder.send(key.to_s) do |child_builder| to_xml(value, child_builder) end else inner_builder.send(key.to_s, value.to_s) end end.to_xml end |