Sha256: e1f8bf920519ce82b04b41ca10c14b21c5478206978ee6079e136ca6859c808c

Contents?: true

Size: 1.31 KB

Versions: 7

Compression:

Stored size: 1.31 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)
      uri = Addressable::URI.heuristic_parse(source)

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

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

    private

    def result
      return PacResult.new if content.blank?
      return PacResult.new if url.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

7 entries across 7 versions & 1 rubygems

Version Path
proxy_tester-0.1.10 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.1.8 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.1.6 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.1.5 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.1.4 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.1.3 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.1.2 lib/proxy_tester/capybara_proxy_pac.rb