Sha256: dea72e023e6b299ffeb13b044582c263f0e7fcf23258896e50c75f37e9ff3baa

Contents?: true

Size: 1.54 KB

Versions: 18

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'grover/utils'

class Grover
  #
  # Convert string option values to boolean, numeric, and array literals
  #
  class OptionsFixer
    FALSE_VALUES = [nil, false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].freeze

    def initialize(options)
      @options = options
    end

    def run
      fix_boolean_options!
      fix_integer_options!
      fix_float_options!
      fix_array_options!
      @options
    end

    private

    def fix_options!(*option_paths)
      option_paths.each do |option_path|
        keys = option_path.split '.'
        value = @options.dig(*keys)
        Utils.deep_assign(@options, keys, yield(value)) if value
      end
    end

    def fix_boolean_options!
      fix_options!(
        'display_header_footer', 'full_page', 'landscape', 'omit_background', 'prefer_css_page_size',
        'print_background', 'viewport.has_touch', 'viewport.is_landscape', 'viewport.is_mobile', 'bypass_csp'
      ) { |value| !FALSE_VALUES.include?(value) }
    end

    def fix_integer_options!
      fix_options!(
        'viewport.height', 'viewport.width', 'wait_for_timeout',
        &:to_i
      )
    end

    def fix_float_options!
      fix_options!(
        'clip.height', 'clip.width', 'clip.x', 'clip.y', 'quality', 'scale', 'viewport.device_scale_factor',
        'geolocation.latitude', 'geolocation.longitude',
        &:to_f
      )
    end

    def fix_array_options!
      fix_options!('launch_args') do |value|
        value.is_a?(String) ? YAML.safe_load(value) : value
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
grover-1.1.10 lib/grover/options_fixer.rb
grover-1.1.9 lib/grover/options_fixer.rb
grover-1.1.8 lib/grover/options_fixer.rb
grover-1.1.7 lib/grover/options_fixer.rb
grover-1.1.6 lib/grover/options_fixer.rb
grover-1.1.5 lib/grover/options_fixer.rb
grover-1.1.4 lib/grover/options_fixer.rb
grover-1.1.3 lib/grover/options_fixer.rb
grover-1.1.2 lib/grover/options_fixer.rb
grover-1.1.1 lib/grover/options_fixer.rb
grover-1.1.0 lib/grover/options_fixer.rb
grover-1.0.6 lib/grover/options_fixer.rb
grover-1.0.5 lib/grover/options_fixer.rb
grover-1.0.4 lib/grover/options_fixer.rb
grover-1.0.3 lib/grover/options_fixer.rb
grover-1.0.2 lib/grover/options_fixer.rb
grover-1.0.1 lib/grover/options_fixer.rb
grover-1.0.0 lib/grover/options_fixer.rb