Sha256: 87cb6742b2657673f5b2d91e809792161cce24c2ec23af9140f9c608c3bd527f

Contents?: true

Size: 891 Bytes

Versions: 1

Compression:

Stored size: 891 Bytes

Contents

require 'nokogiri'

module DragonflySvg
  module Analysers
    class SvgProperties
      def call(content)
        return {} unless content.ext
        return {} unless SUPPORTED_FORMATS.include?(content.ext.downcase)
        return {} unless doc = Nokogiri::XML(content.data)
        return {} unless node = doc.xpath("//*[name()='svg']").first

        {
          'format' => content.ext.to_s,
          'width' => width(node).to_f,
          'height' => height(node).to_f,
          'id' => id(node)
        }
      end

      private

      def viewBox(node)
        node.get_attribute('viewBox').to_s.split(/\s+/)
      end

      def width(node)
        node.get_attribute('width') || viewBox(node)[2]
      end

      def height(node)
        node.get_attribute('height') || viewBox(node)[3]
      end

      def id(node)
        node.get_attribute('id')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dragonfly_svg-1.0.1 lib/dragonfly_svg/analysers/svg_properties.rb