Module: Shale::Adapter::Nokogiri

Defined in:
lib/shale/adapter/nokogiri.rb,
lib/shale/adapter/nokogiri/node.rb,
lib/shale/adapter/nokogiri/document.rb

Overview

Nokogiri adapter

Defined Under Namespace

Classes: Document, Node

Class Method Summary collapse

Class Method Details

.create_document(version = nil) ⇒ Object

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.

Create Shale::Adapter::Nokogiri::Document instance

Parameters:

  • declaration (true, false, String, nil)


75
76
77
# File 'lib/shale/adapter/nokogiri.rb', line 75

def self.create_document(version = nil)
  Document.new(version)
end

.dump(doc, pretty: false, declaration: false, encoding: false) ⇒ String

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.

Serialize Nokogiri document into XML

Parameters:

  • doc (::Nokogiri::XML::Document)

    Nokogiri document

  • pretty (true, false) (defaults to: false)
  • declaration (true, false, String) (defaults to: false)
  • encoding (true, false, String) (defaults to: false)

Returns:

  • (String)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/shale/adapter/nokogiri.rb', line 46

def self.dump(doc, pretty: false, declaration: false, encoding: false)
  save_with = ::Nokogiri::XML::Node::SaveOptions::AS_XML

  if pretty
    save_with |= ::Nokogiri::XML::Node::SaveOptions::FORMAT
  end

  unless declaration
    save_with |= ::Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
  end

  if encoding
    doc.encoding = encoding == true ? 'UTF-8' : encoding
  end

  result = doc.to_xml(save_with: save_with)

  unless pretty
    result = result.sub("\n", '')
  end

  result
end

.load(xml) ⇒ Shale::Adapter::Nokogiri::Node

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.

Parse XML into Nokogiri document

Parameters:

  • xml (String)

    XML document

Returns:

Raises:



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/shale/adapter/nokogiri.rb', line 24

def self.load(xml)
  doc = ::Nokogiri::XML::Document.parse(xml) do |config|
    config.noblanks
  end

  unless doc.errors.empty?
    raise ParseError, "Document is invalid: #{doc.errors}"
  end

  Node.new(doc.root)
end