Sha256: 92785c4b541a9ccbe17d94a8f2ffe48412c31813bfcb0d782afeac7550da0bcd

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

require 'ox'
require 'escape_utils'

module XmlHasher
  class Handler < ::Ox::Sax
    def initialize(options = {})
      @options = options
      @stack = []
    end

    def to_hash
      @hash || {}
    end

    def start_element(name)
      @stack.push(Node.new(transform(name)))
    end

    def attr(name, value)
      unless ignore_attribute?(name)
        @stack.last.attributes[transform(name)] = escape(value) unless @stack.empty?
      end
    end

    def text(value)
      @stack.last.text = escape(value)
    end

    def end_element(name)
      if @stack.size == 1
        @hash = @stack.pop.to_hash
      else
        node = @stack.pop
        @stack.last.children << node
      end
    end

    private

    def transform(name)
      name = name.to_s.split(':').last if @options[:ignore_namespaces]
      name = Util.snakecase(name) if @options[:snakecase]
      name = name.to_sym unless @options[:string_keys]
      name
    end

    def escape(value)
      EscapeUtils.unescape_html(value)
    end

    def ignore_attribute?(name)
      @options[:ignore_namespaces] ? !name.to_s[/^(xmlns|xsi)/].nil? : false
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
xmlhasher_with_attributes-1.0.1 lib/xmlhasher/handler.rb
xmlhasher_with_attributes-1.0.0 lib/xmlhasher/handler.rb
xmlhasher-1.0.3 lib/xmlhasher/handler.rb
xmlhasher-1.0.2 lib/xmlhasher/handler.rb
xmlhasher-1.0.1 lib/xmlhasher/handler.rb
xmlhasher-1.0.0 lib/xmlhasher/handler.rb
xmlhasher-0.0.6 lib/xmlhasher/handler.rb