Module: Justimmo::Parser Private

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

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


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

def log
  Justimmo::Logger
end

.parse(data, mapper = nil) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert a XML document to a Ruby hash.

Parameters:

  • data (String)

    The XML string to convert

  • mapper (Mapper) (defaults to: nil)

    The optional mapper to convert keys and attributes.

Returns:

  • (Hash)

    A converted structure or an empty hash


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

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