Created
May 28, 2012 18:18
-
-
Save igrigorik/2820414 to your computer and use it in GitHub Desktop.
pagespeed insights API ruby example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/https' | |
require 'json' | |
require 'uri' | |
# | |
# Use your project key to query the API | |
# 1) go to Google API Console: https://code.google.com/apis/console/ | |
# 2) go to "Services" tab and enable "Page Speed Online API" | |
# 3) go to "API Key" tab and copy your "API Key" | |
# | |
KEY = 'YOUR_KEY' | |
strategy = 'desktop' # alternatively: 'mobile' | |
# hostname or page to run the test on | |
domain = 'http://www.igvita.com/' | |
url = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=" + \ | |
URI.encode(domain) + \ | |
"&key=#{KEY}&strategy=#{strategy}" | |
uri = URI.parse(url) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
request = Net::HTTP::Get.new(uri.request_uri) | |
response = http.request(request) | |
pss = JSON.parse(response.body) | |
jj pss |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi igrigorik. i have a question.
I want to use the page speed api in my project but it isnt a direct page. Its more of a home page->login(authenticate)->go to x page->click on this->go to y page. It is the y page that I need to get the performance statistics for. also its an internal test environment that I need to test this on. I am unable to understand how to specify all of this with a single "url" parameter. Please suggest.