Sha256: bb1b6ff6982b2e24634333eec15c3fbfc3c95950a580f66137060fe9e3160073

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module BrowserMob
  module Proxy

    class Client
      attr_reader :host, :port

      def self.from(server_url)
        port = JSON.parse(
          RestClient.post(URI.join(server_url, "proxy").to_s, '')
        ).fetch('port')

        uri = URI.parse(File.join(server_url, "proxy", port.to_s))
        resource = RestClient::Resource.new(uri.to_s)

        Client.new resource, uri.host, port
      end

      def initialize(resource, host, port)
        @resource = resource
        @host = host
        @port = port
      end

      def new_har(ref = nil)
        previous = @resource["har"].put :initialPageRef => ref
        HAR::Archive.from_string(previous) unless previous.empty?
      end

      def new_page(ref)
        @resource['har/pageRef'].put :pageRef => ref
      end

      def har
        HAR::Archive.from_string @resource["har"].get
      end

      def selenium_proxy
        require 'selenium-webdriver' unless defined?(Selenium)
        Selenium::WebDriver::Proxy.new(:http => "#{@host}:#{@port}")
      end

      def whitelist(regexp, status_code)
        regex = Regexp === regexp ? regexp.source : regexp.to_s
        @resource['har/whitelist'].put :regex => regex, :status => status_code
      end

      def blacklist(regexp, status_code)
        regex = Regexp === regexp ? regexp.source : regexp.to_s
        @resource['har/blacklist'].put :regex => regex, :status => status_code
      end

      def close
        @resource.delete
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
browsermob-proxy-0.0.3 lib/browsermob/proxy/client.rb