Module: Resync::XML

Defined in:
lib/resync/xml.rb

Overview

Helper methods and modules related to reading and writing XML.

Defined Under Namespace

Classes: HashCodesNode

Class Method Summary collapse

Class Method Details

.element(xml) ⇒ REXML::Element

Extracts a REXML::Element from the specified object.

Parameters:

  • xml (String, IO, REXML::Document, REXML::Element)

    A string or IO-like object containing an XML document (with or without XML declaration), or an XML document, or an XML element.

Returns:

  • (REXML::Element)

    the root element of the document, or the element itself if xml is already an element.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/resync/xml.rb', line 33

def self.element(xml)
  case xml
  when REXML::Document
    xml.root
  when REXML::Element
    xml
  else
    fail ArgumentError, "Unexpected argument type; expected XML document, String, or IO source, was #{xml.class}" unless can_parse(xml)
    REXML::Document.new(xml).root
  end
end

.to_uri(url) ⇒ Object

Ensures that the provided value is a URI, parsing it if necessary.

Parameters:

  • url (URI, String)

    the URI.

Raises:

  • (URI::InvalidURIError)

    if url cannot be converted to a URI.



19
20
21
22
23
24
# File 'lib/resync/xml.rb', line 19

def self.to_uri(url)
  return nil unless url
  return url if url.is_a? URI
  stripped = url.respond_to?(:strip) ? url.strip : url.to_s.strip
  URI.parse(stripped)
end