Class: Nori

Inherits:
Object
  • Object
show all
Defined in:
lib/nori.rb,
lib/nori/version.rb,
lib/nori/parser/rexml.rb,
lib/nori/core_ext/hash.rb,
lib/nori/string_io_file.rb,
lib/nori/core_ext/object.rb,
lib/nori/core_ext/string.rb,
lib/nori/parser/nokogiri.rb,
lib/nori/xml_utility_node.rb,
lib/nori/string_with_attributes.rb

Defined Under Namespace

Modules: CoreExt, Parser Classes: StringIOFile, StringWithAttributes, XMLUtilityNode

Constant Summary collapse

PARSERS =
{ :rexml => "REXML", :nokogiri => "Nokogiri" }
VERSION =
'2.6.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Nori

Returns a new instance of Nori.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nori.rb', line 16

def initialize(options = {})
  defaults = {
    :strip_namespaces              => false,
    :delete_namespace_attributes   => false,
    :convert_tags_to               => nil,
    :convert_attributes_to         => nil,
    :empty_tag_value               => nil,
    :advanced_typecasting          => true,
    :convert_dashes_to_underscores => true,
    :parser                        => :nokogiri
  }

  validate_options! defaults.keys, options.keys
  @options = defaults.merge(options)
end

Class Method Details

.hash_key(name, options = {}) ⇒ Object



7
8
9
10
11
12
# File 'lib/nori.rb', line 7

def self.hash_key(name, options = {})
  name = name.tr("-", "_") if options[:convert_dashes_to_underscores]
  name = name.split(":").last if options[:strip_namespaces]
  name = options[:convert_tags_to].call(name) if options[:convert_tags_to].respond_to? :call
  name
end

Instance Method Details

#find(hash, *path) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/nori.rb', line 32

def find(hash, *path)
  return hash if path.empty?

  key = path.shift
  key = self.class.hash_key(key, @options)

  value = find_value(hash, key)
  find(value, *path) if value
end

#parse(xml) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/nori.rb', line 42

def parse(xml)
  cleaned_xml = xml.strip
  return {} if cleaned_xml.empty?

  parser = load_parser @options[:parser]
  parser.parse(cleaned_xml, @options)
end