Class: BrowserMob::Proxy::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/browsermob/proxy/client.rb

Constant Summary collapse

LIMITS =
{
  :upstream_kbps   => 'upstreamKbps',
  :downstream_kbps => 'downstreamKbps',
  :latency         => 'latency'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, host, port) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
# File 'lib/browsermob/proxy/client.rb', line 18

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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/browsermob/proxy/client.rb', line 5

def host
  @host
end

#portObject (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
# File 'lib/browsermob/proxy/client.rb', line 7

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

Instance Method Details

#blacklist(regexp, status_code) ⇒ Object



47
48
49
50
# File 'lib/browsermob/proxy/client.rb', line 47

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

#closeObject



81
82
83
# File 'lib/browsermob/proxy/client.rb', line 81

def close
  @resource.delete
end

#harObject



33
34
35
# File 'lib/browsermob/proxy/client.rb', line 33

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

#header(hash) ⇒ Object Also known as: headers



52
53
54
# File 'lib/browsermob/proxy/client.rb', line 52

def header(hash)
  @resource['headers'].post hash.to_json, :content_type => "application/json"
end

#limit(opts) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/browsermob/proxy/client.rb', line 63

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) ⇒ Object



24
25
26
27
# File 'lib/browsermob/proxy/client.rb', line 24

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

#new_page(ref) ⇒ Object



29
30
31
# File 'lib/browsermob/proxy/client.rb', line 29

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

#selenium_proxyObject



37
38
39
40
# File 'lib/browsermob/proxy/client.rb', line 37

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

#whitelist(regexp, status_code) ⇒ Object



42
43
44
45
# File 'lib/browsermob/proxy/client.rb', line 42

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