Sha256: 3312fa2c8bea84101aa5a17e0a6ab35ddd5b00851abfc5a1c69c3f7cd661f1d3

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 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',
        'raise_on_request_failure', 'raise_on_js_error'
      ) { |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

3 entries across 3 versions & 1 rubygems

Version Path
grover-1.2.1 lib/grover/options_fixer.rb
grover-1.2.0 lib/grover/options_fixer.rb
grover-1.1.11 lib/grover/options_fixer.rb