Class: Mods::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/mods/reader.rb

Instance Method Summary collapse

Constructor Details

#initializeReader

Returns a new instance of Reader.

Parameters:

  • ns_aware

    true if the XML parsing should be strict about using namespaces. Default is true



6
7
# File 'lib/mods/reader.rb', line 6

def initialize
end

Instance Method Details

#from_file(filename, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_XML) ⇒ Object

Read in the contents of a Mods record from a file.

Examples:

foo = Mods::Reader.new.from_file('/path/to/mods/file.xml')

Parameters:

  • filename (String)
    • path to file that has mods xml as its content

Returns:

  • Nokogiri::XML::Document



30
31
32
33
34
# File 'lib/mods/reader.rb', line 30

def from_file(filename, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_XML)
  File.open(filename) do |file|
    Nokogiri::XML(file)
  end
end

#from_nk_node(node) ⇒ Object

Returns Nokogiri::XML::Document.

Parameters:

  • node (Nokogiri::XML::Node)
    • Nokogiri::XML::Node that is the top level element of a mods record

Returns:

  • Nokogiri::XML::Document



38
39
40
# File 'lib/mods/reader.rb', line 38

def from_nk_node(node)
  Nokogiri::XML(node.to_s, nil, node.document.encoding)
end

#from_str(str) ⇒ Object

Returns Nokogiri::XML::Document.

Parameters:

  • str
    • a string containing mods xml

Returns:

  • Nokogiri::XML::Document



11
12
13
# File 'lib/mods/reader.rb', line 11

def from_str(str)
  Nokogiri::XML(str, nil, str.encoding.to_s)
end

#from_url(url, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_XML) ⇒ Object

Read in the contents of a Mods file from a url.

Examples:

foo = Mods::Reader.new.from_url('http://purl.stanford.edu/bb340tm8592.mods')

Parameters:

  • url (String)
    • url that has mods xml as its content

Returns:

  • Nokogiri::XML::Document



20
21
22
23
# File 'lib/mods/reader.rb', line 20

def from_url(url, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_XML)
  require 'open-uri'
  Nokogiri::XML(URI.open(url).read)
end