Sha256: 129967961b11861440809d1dadc94f78d998a4acce2cbdfe769e376ec701a9e6

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# encoding: utf-8
module ProxyPacRb
  module Cli
    # Find proxy for url
    class FindProxy < Thor::Group
      include Shared

      class_option :client_ip, default: '127.0.0.1', desc: 'Client IP-address', aliases: '-c'
      class_option :time, default: Time.now.to_s, desc: 'Time to use during lookup url', aliases: '-t'
      class_option :proxy_pac, desc: 'Proxy.pac-file', aliases: '-p', required: true
      class_option :urls, type: :array, desc: 'URLs to check against proxy pac', aliases: '-u', required: true
      class_option :use_proxy, default: false, type: :boolean, desc: 'Use proxy to download proxy pac (default false)'

      def pre_init
        enable_debug_mode
      end

      def find_proxy
        CliValidator.new(options).validate

        environment = ProxyPacRb::Environment.new(
          client_ip: IPAddr.new(options[:client_ip]),
          time: Time.parse(options[:time])
        )

        remove_proxy_environment_variables unless options[:use_proxy]

        file = ProxyPacRb::Parser.new(environment: environment).parse(options[:proxy_pac])
        return if file.blank?

        $stderr.printf("%30s: %-s\n", 'url', 'result')
        options[:urls].each do |u|
          begin
            $stderr.printf("%30s: %-s\n", u, file.find(u))
          rescue UrlInvalidError
            $stderr.puts "You provide an invalid url \"#{u}\". Please use a correct one."
          rescue ProxyPacRb::ProxyPacInvalidError => err
            $stderr.puts err.message
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
proxy_pac_rb-0.9.1 lib/proxy_pac_rb/cli/find_proxy.rb