Sha256: 307e49847d1f8aecf3230ba764fbb458fab78a3e3a51ce9d142ea92c0e1fe5cb

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

# encoding: utf-8
module ProxyTester
  class CapybaraProxyPac
    private

    attr_reader :content

    public

    attr_accessor :client_ip, :time, :pac_file, :url, :result

    def host
      result.proxy
    end

    def port
      result.proxy_port
    end

    def blank?
      host.blank? or port.blank?
    end

    def direct?
      result.request_type == 'DIRECT'
    end

    def verbatim
      result.verbatim
    end

    def pac_file=(source)
      @pac_file = source

      uri = Addressable::URI.heuristic_parse(source)

      if uri.host.blank?
        @content = File.read(source)
      else
        @content = open(uri, { proxy: false }).string
      end

      @content
    rescue Errno::ENOENT
      raise Exceptions::PacFileNotFound, JSON.dump(file: source)
    end

    private

    def result
      return PacResult.new if content.blank?

      env_hash = {}
      env_hash[:client_ip] = client_ip if client_ip
      env_hash[:time]      = time if time

      parser = ProxyPacRb::Parser.new(ProxyPacRb::Environment.new(env_hash))

      file = parser.source(content)
      result = PacResult.new(file.find(url))

      raise Exceptions::PacResultResult, JSON.dump(file: pac_file, result: result) if result.blank?

      result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
proxy_tester-0.1.1 lib/proxy_tester/capybara_proxy_pac.rb