Sha256: 291464de7fc5fafb330fd14a198ebba87c6668be519ae33234041edc72af4f29

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true
module ProxyPacRb
  # Validator for commandline options
  class CliValidator
    private

    attr_reader :options

    public

    def initialize(options)
      @options = options
    end

    def validate
      exit_with_message 'You need to provide at least one url. Multiple urls need to be separated by a space.' if empty_url?
      exit_with_message 'You need to provide a proxy pac file.' if empty_pac_file?
      exit_with_message %(You need to provide a path to an existing proxy pac file. The file "#{options[:proxy_pac]}" does not exist.) if non_existing_proxy_pac_file?
    end

    private

    def proxy_pac_url?
      url = Addressable::URI.parse(options[:proxy_pac])
      return true if url.host

      false
    rescue StandardError
      false
    end

    def non_existing_proxy_pac_file?
      !proxy_pac_url? && !File.file?(options[:proxy_pac])
    end

    def empty_url?
      options[:urls].blank?
    end

    def empty_pac_file?
      options[:proxy_pac].blank?
    end

    def exit_with_message(msg)
      $stderr.puts msg
      exit 1
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
proxy_pac_rb-3.0.0 lib/proxy_pac_rb/cli_validator.rb
proxy_pac_rb-2.1.0 lib/proxy_pac_rb/cli_validator.rb
proxy_pac_rb-2.0.0 lib/proxy_pac_rb/cli_validator.rb
proxy_pac_rb-1.0.0 lib/proxy_pac_rb/cli_validator.rb