Sha256: 1858fd76c4b3c3ec81a0a3f084e631f45bd85a6b28c92887e6ba25a4ba70d331

Contents?: true

Size: 1.17 KB

Versions: 17

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require 'grover/utils'
require 'grover/options_fixer'

class Grover
  #
  # Build options from Grover.configuration, meta_options, and passed-in options
  #
  class OptionsBuilder < Hash
    def initialize(options, url) # rubocop:disable Lint/MissingSuper
      @url = url
      combined = grover_configuration
      Utils.deep_merge! combined, Utils.deep_stringify_keys(options)
      Utils.deep_merge! combined, meta_options unless url_source?

      update OptionsFixer.new(combined).run
    end

    private

    def grover_configuration
      Utils.deep_stringify_keys Grover.configuration.options
    end

    #
    # Extract out options from meta tags in the source - based on code from PDFKit project
    #
    def meta_options
      meta_opts = {}

      meta_tags.each do |meta|
        tag_name = meta['name'] && meta['name'][/#{Grover.configuration.meta_tag_prefix}([a-z_-]+)/, 1]
        next unless tag_name

        Utils.deep_assign meta_opts, tag_name.split('-'), meta['content']
      end

      meta_opts
    end

    def meta_tags
      Nokogiri::HTML(@url).xpath('//meta')
    end

    def url_source?
      @url.match(/\Ahttp/i)
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
grover-1.1.4 lib/grover/options_builder.rb
grover-1.1.3 lib/grover/options_builder.rb
grover-1.1.2 lib/grover/options_builder.rb
grover-1.1.1 lib/grover/options_builder.rb
grover-1.1.0 lib/grover/options_builder.rb
grover-1.0.6 lib/grover/options_builder.rb
grover-1.0.5 lib/grover/options_builder.rb
grover-1.0.4 lib/grover/options_builder.rb
grover-1.0.3 lib/grover/options_builder.rb
grover-1.0.2 lib/grover/options_builder.rb
grover-1.0.1 lib/grover/options_builder.rb
grover-1.0.0 lib/grover/options_builder.rb
grover-0.14.2 lib/grover/options_builder.rb
grover-0.14.1 lib/grover/options_builder.rb
grover-0.13.3 lib/grover/options_builder.rb
grover-0.13.2 lib/grover/options_builder.rb
grover-0.13.1 lib/grover/options_builder.rb