Class: Xml::Kit::Document

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/xml/kit/document.rb

Overview

Constant Summary collapse

NAMESPACES =
{ "ds": ::Xml::Kit::Namespaces::XMLDSIG }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(raw_xml, namespaces: NAMESPACES) ⇒ Document

Returns a new instance of Document.



13
14
15
16
17
# File 'lib/xml/kit/document.rb', line 13

def initialize(raw_xml, namespaces: NAMESPACES)
  @raw_xml = raw_xml
  @namespaces = namespaces
  @document = ::Nokogiri::XML(raw_xml)
end

Instance Method Details

#find_all(xpath) ⇒ Object

Returns all XML nodes found by searching the document with the provided XPath.

Parameters:

  • xpath (String)

    the XPath to use to search the document



29
30
31
# File 'lib/xml/kit/document.rb', line 29

def find_all(xpath)
  document.search(xpath, namespaces)
end

#find_by(xpath) ⇒ Object

Returns the first XML node found by searching the document with the provided XPath.

Parameters:

  • xpath (String)

    the XPath to use to search the document



22
23
24
# File 'lib/xml/kit/document.rb', line 22

def find_by(xpath)
  document.at_xpath(xpath, namespaces)
end

#to_xml(pretty: true) ⇒ Object

Return the XML document as a [String].

Parameters:

  • pretty (Boolean) (defaults to: true)

    return the XML string in a human readable format if true.



36
37
38
# File 'lib/xml/kit/document.rb', line 36

def to_xml(pretty: true)
  pretty ? document.to_xml(indent: 2) : raw_xml
end