Class: Webpagetest::Client

Inherits:
Object
  • Object
show all
Includes:
Connection
Defined in:
lib/webpagetest/client.rb

Constant Summary collapse

TEST_BASE =
'runtest.php'
LOCATIONS_BASE =
'getLocations.php'
RESULT_BASE =
'jsonResult.php'

Constants included from Connection

Webpagetest::Connection::ENDPOINT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Connection

#get_connection

Constructor Details

#initialize(params = {}) ⇒ Client

Main params for running tests



13
14
15
16
17
18
19
20
# File 'lib/webpagetest/client.rb', line 13

def initialize(params = {})
  params = Hashie::Mash.new(params)
  required_params params      
  params.f ||= :json
  params.options ||= nil
  @params = params
  @connection = get_connection params.options
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/webpagetest/client.rb', line 6

def connection
  @connection
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/webpagetest/client.rb', line 6

def params
  @params
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/webpagetest/client.rb', line 6

def response
  @response
end

Instance Method Details

#keyObject

Alias method for API key



23
24
25
# File 'lib/webpagetest/client.rb', line 23

def key
  params.k
end

#locationsObject

Gets all available test locations



56
57
58
59
60
61
62
# File 'lib/webpagetest/client.rb', line 56

def locations
  locations_params = Hashie::Mash.new( {f: params.f} )
  response = make_request(LOCATIONS_BASE, locations_params)
  return not_available (response) unless response.status == 200
  response_body = Hashie::Mash.new(JSON.parse(response.body))  
  response_body.data
end

#run_test {|params| ... } ⇒ Object

Runs a test bases on provided parameters

Yields:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/webpagetest/client.rb', line 28

def run_test
  # Make sure that params have been set
  raise_error('You need to pass some params to run the test. At least, an url or script') unless block_given?
  params = Hashie::Mash.new
  yield params
  raise_error('No params were passed to run_test method') if params.empty?

  response = connection.post do |req|
    req.url "#{TEST_BASE}"
    req.params['k'] = key
    req.params['f'] = @params.f
    params.each do |k, v|
      req.params[k] = v
    end
  end
  return not_available (response) unless response.status == 200
  @response = Response.new(Hashie::Mash.new(JSON.parse(response.body)))
end

#test_result(test_id) ⇒ Object

Gets the result of a test based on its id



48
49
50
51
52
53
# File 'lib/webpagetest/client.rb', line 48

def test_result(test_id)
  test_params = Hashie::Mash.new( {test: test_id, pagespeed: 1} )
  response = make_request(RESULT_BASE, test_params)
  return not_available (response) unless response.status == 200
  @response = Response.new(Hashie::Mash.new(JSON.parse(response.body)), false)
end