class Nori

Constants

PARSERS
VERSION

Public Class Methods

hash_key(name, options = {}) click to toggle source
# 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
new(options = {}) click to toggle source
# 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,
    :scrub_xml                     => true,
    :parser                        => :nokogiri
  }

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

Public Instance Methods

find(hash, *path) click to toggle source
# File lib/nori.rb, line 33
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) click to toggle source
# File lib/nori.rb, line 43
def parse(xml)
  cleaned_xml = scrub_xml(xml).strip
  return {} if cleaned_xml.empty?

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

Private Instance Methods

convert_tags_to(reset = nil, &block) click to toggle source

Expects a block which receives a tag to convert. Accepts nil for a reset to the default behavior of not converting tags.

# File lib/nori.rb, line 59
def convert_tags_to(reset = nil, &block)
  @convert_tag = reset || block
end
find_value(hash, key) click to toggle source
# File lib/nori.rb, line 72
def find_value(hash, key)
  hash.each do |k, v|
    key_without_namespace = k.to_s.split(':').last
    return v if key_without_namespace == key.to_s
  end

  nil
end
load_parser(parser) click to toggle source
# File lib/nori.rb, line 52
def load_parser(parser)
  require "nori/parser/#{parser}"
  Parser.const_get PARSERS[parser]
end
scrub_xml(string) click to toggle source
# File lib/nori.rb, line 81
def scrub_xml(string)
  if @options[:scrub_xml]
    if string.respond_to? :scrub
      string.scrub
    else
      if string.valid_encoding?
        string
      else
        enc = string.encoding
        mid_enc = (["UTF-8", "UTF-16BE"].map { |e| Encoding.find(e) } - [enc]).first
        string.encode(mid_enc, undef: :replace, invalid: :replace).encode(enc)
      end
    end
  else
    string 
  end
end
validate_options!(available_options, options) click to toggle source
# File lib/nori.rb, line 63
def validate_options!(available_options, options)
  spurious_options = options - available_options

  unless spurious_options.empty?
    raise ArgumentError, "Spurious options: #{spurious_options.inspect}\n" \
                         "Available options are: #{available_options.inspect}"
  end
end