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['whitelist'].put :regex => regex, :status => status_code end def blacklist(regexp, status_code) regex = Regexp === regexp ? regexp.source : regexp.to_s @resource['blacklist'].put :regex => regex, :status => status_code end LIMITS = { :upstream_kbps => 'upstreamKbps', :downstream_kbps => 'downstreamKbps', :latency => 'latency' } def limit(opts) params = {} opts.each do |key, value| unless LIMITS.member?(key) raise ArgumentError, "invalid: #{key.inspect} (valid options: #{LIMITS.keys.inspect})" end params[LIMITS[key]] = Integer(value) end if params.empty? raise ArgumentError, "must specify one of #{LIMITS.keys.inspect}" end @resource['limit'].put params end def close @resource.delete end end # Client end # Proxy end # BrowserMob