Sha256: 622d4ea8a69962f51c8d6d0437d0463666974ef2e24834d98941b06b92413466

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

Contents

# encoding: utf-8
module ProxyTester
  class CapybaraProxyPac

    attr_accessor :client_ip, :time, :pac_file, :url, :resul

    def host
      @result ||= result

      result.proxy
    end

    def port
      @result ||= result

      result.proxy_port
    end

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

    def direct?
      @result ||= result

      result.request_type == 'DIRECT'
    end

    def pac_file=(source)
      uri = Addressable::URI.heuristic_parse(source)

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

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

    private

    def result
      return PacResult.new if pac_file.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(pac_file)
      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

9 entries across 9 versions & 1 rubygems

Version Path
proxy_tester-0.0.9 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.0.8 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.0.7 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.0.6 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.0.5 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.0.4 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.0.3 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.0.2 lib/proxy_tester/capybara_proxy_pac.rb
proxy_tester-0.0.1 lib/proxy_tester/capybara_proxy_pac.rb