Class: BrowserMob::Proxy::Client
- Inherits:
-
Object
- Object
- BrowserMob::Proxy::Client
- Defined in:
- lib/browsermob/proxy/client.rb
Constant Summary collapse
- LIMITS =
{ :upstream_kbps => 'upstreamKbps', :downstream_kbps => 'downstreamKbps', :latency => 'latency' }
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Class Method Summary collapse
Instance Method Summary collapse
- #add_host(host, ip) ⇒ Object
- #blacklist(regexp, status_code) ⇒ Object
- #close ⇒ Object
- #har ⇒ Object
- #header(hash) ⇒ Object (also: #headers)
-
#initialize(resource, host, port) ⇒ Client
constructor
A new instance of Client.
- #limit(opts) ⇒ Object
-
#new_har(ref = nil, opts = {}) ⇒ Object
Examples:.
- #new_page(ref) ⇒ Object
- #remove_host(host) ⇒ Object
- #rewrite(regexp, replace) ⇒ Object
- #selenium_proxy(*protocols) ⇒ Object
- #whitelist(regexp, status_code) ⇒ Object
Constructor Details
#initialize(resource, host, port) ⇒ Client
Returns a new instance of Client.
22 23 24 25 26 27 |
# File 'lib/browsermob/proxy/client.rb', line 22 def initialize(resource, host, port) @resource = resource @host = host @port = port @hosts = {} end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
5 6 7 |
# File 'lib/browsermob/proxy/client.rb', line 5 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
5 6 7 |
# File 'lib/browsermob/proxy/client.rb', line 5 def port @port end |
Class Method Details
.from(server_url) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/browsermob/proxy/client.rb', line 7 def self.from(server_url) # ActiveSupport may define Object#load, so we can't use MultiJson.respond_to? here. sm = MultiJson.singleton_methods.map { |e| e.to_sym } decode_method = sm.include?(:load) ? :load : :decode port = MultiJson.send(decode_method, 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 |
Instance Method Details
#add_host(host, ip) ⇒ Object
89 90 91 92 |
# File 'lib/browsermob/proxy/client.rb', line 89 def add_host(host, ip) @hosts[host] = ip @resource['hosts'].post @hosts.to_json, :content_type => "application/json" end |
#blacklist(regexp, status_code) ⇒ Object
79 80 81 82 |
# File 'lib/browsermob/proxy/client.rb', line 79 def blacklist(regexp, status_code) regex = Regexp === regexp ? regexp.source : regexp.to_s @resource['blacklist'].put :regex => regex, :status => status_code end |
#close ⇒ Object
128 129 130 |
# File 'lib/browsermob/proxy/client.rb', line 128 def close @resource.delete end |
#har ⇒ Object
57 58 59 |
# File 'lib/browsermob/proxy/client.rb', line 57 def har HAR::Archive.from_string @resource["har"].get end |
#header(hash) ⇒ Object Also known as: headers
99 100 101 |
# File 'lib/browsermob/proxy/client.rb', line 99 def header(hash) @resource['headers'].post hash.to_json, :content_type => "application/json" end |
#limit(opts) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/browsermob/proxy/client.rb', line 110 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 |
#new_har(ref = nil, opts = {}) ⇒ Object
Examples:
client.new_har("page-name")
client.new_har("page-name", :capture_headers => true)
client.new_har(:capture_headers => true)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/browsermob/proxy/client.rb', line 37 def new_har(ref = nil, opts = {}) if opts.empty? && ref.kind_of?(Hash) opts = ref ref = nil end params = {} params[:initialPageRef] = ref if ref params[:captureHeaders] = true if opts[:capture_headers] previous = @resource["har"].put params HAR::Archive.from_string(previous) unless previous.empty? end |
#new_page(ref) ⇒ Object
53 54 55 |
# File 'lib/browsermob/proxy/client.rb', line 53 def new_page(ref) @resource['har/pageRef'].put :pageRef => ref end |
#remove_host(host) ⇒ Object
94 95 96 97 |
# File 'lib/browsermob/proxy/client.rb', line 94 def remove_host(host) @hosts.delete host @resource['hosts'].post @hosts.to_json, :content_type => "application/json" end |
#rewrite(regexp, replace) ⇒ Object
84 85 86 87 |
# File 'lib/browsermob/proxy/client.rb', line 84 def rewrite(regexp, replace) regex = Regexp === regexp ? regexp.source : regexp.to_s @resource['rewrite'].put :matchRegex => regex, :replace => replace end |
#selenium_proxy(*protocols) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/browsermob/proxy/client.rb', line 61 def selenium_proxy(*protocols) require 'selenium-webdriver' unless defined?(Selenium) protocols += [:http] if protocols.empty? unless (protocols - [:http, :ssl, :ftp]).empty? raise "Invalid protocol specified. Must be one of: :http, :ssl, or :ftp." end proxy_mapping = {} protocols.each { |proto| proxy_mapping[proto] = "#{@host}:#{@port}" } Selenium::WebDriver::Proxy.new(proxy_mapping) end |
#whitelist(regexp, status_code) ⇒ Object
74 75 76 77 |
# File 'lib/browsermob/proxy/client.rb', line 74 def whitelist(regexp, status_code) regex = Regexp === regexp ? regexp.source : regexp.to_s @resource['whitelist'].put :regex => regex, :status => status_code end |