Module: Justimmo::Parser

Includes:
Conversions::Attr, Conversions::Document, Conversions::Node
Defined in:
lib/justimmo/parser.rb

Overview

The XML to Hash parser.

Defined Under Namespace

Modules: Conversions

Class Method Summary collapse

Methods included from Conversions::Attr

#to_h

Methods included from Conversions::Node

#apply_mapping, #format_key, #parse_value, #to_h

Methods included from Conversions::Document

#to_h

Class Method Details

.logObject


33
34
35
# File 'lib/justimmo/parser.rb', line 33

def log
  Justimmo::Logger
end

.parse(data, mapper = nil) ⇒ Hash

Convert a XML document to a Ruby hash.


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/justimmo/parser.rb', line 18

def parse(data, mapper = nil)
  return {} unless data.is_a?(String)

  doc = Nokogiri::XML(data) do |config|
    config.noblanks # remove superfluous newline nodes
    config.norecover # fail if the XML is invalid
    config.nonet # do not allow network connections
  end

  doc.to_h(mapper)
rescue Nokogiri::XML::SyntaxError => e
  log.error("Parser error: #{e}")
  {}
end