pagespeed 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pagespeed.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Travis Dunn
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ a ruby wrapper around the google pagespeed api
2
+
3
+ ## Usage
4
+
5
+ > pagespeed add-key YOUR_API_KEY
6
+ > pagespeed blahed.com
7
+
8
+ 100 - Make landing page redirects cacheable
9
+ 100 - Put CSS in the document head
10
+ 100 - Avoid CSS @import
11
+ 95 - Specify a character set
12
+ 100 - Serve scaled images
13
+ 97 - Minify JavaScript
14
+ 0 - Leverage browser caching
15
+ 100 - Optimize the order of styles and scripts
16
+ 95 - Defer parsing of JavaScript
17
+ 100 - Prefer asynchronous resources
18
+ 100 - Minimize redirects
19
+ 48 - Minify CSS
20
+ 100 - Inline Small CSS
21
+ 100 - Combine images into CSS sprites
22
+ 100 - Specify a cache validator
23
+ 50 - Inline Small JavaScript
24
+ 0 - Specify a Vary: Accept-Encoding header
25
+ 100 - Serve resources from a consistent URL
26
+ 100 - Remove query strings from static resources
27
+ 100 - Specify image dimensions
28
+ 100 - Minimize request size
29
+ 100 - Avoid bad requests
30
+ 100 - Optimize images
31
+ 58 - Minify HTML
32
+
33
+ Total Score: 77
34
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/pagespeed ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
3
+ require 'pagespeed'
4
+
5
+ PageSpeed::CLI.run!(ARGV)
data/lib/pagespeed.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'pagespeed/version'
2
+ require 'pagespeed/cli'
3
+ require 'pagespeed/parser'
4
+ require 'pagespeed/request'
5
+
6
+ module PageSpeed
7
+
8
+ end
@@ -0,0 +1,85 @@
1
+ require 'optparse'
2
+
3
+ module PageSpeed
4
+ class CLI
5
+
6
+ class << self
7
+ KEY_PATH = File.join(ENV['HOME'], '.pagespeed_api_key')
8
+ BANNER = <<-USAGE
9
+ Usage:
10
+ pagespeed google.com
11
+
12
+ Description:
13
+ pagespeed pulls in results for a given website from the google pagespeed api
14
+
15
+ USAGE
16
+
17
+ # parse and set the options
18
+ def set_options
19
+ @opts = OptionParser.new do |opts|
20
+ opts.banner = BANNER.gsub(/^\s{4}/, '')
21
+
22
+ opts.separator ''
23
+
24
+ opts.on('-v', '--version', 'Show the pagespeed version and exit') do
25
+ puts "dsync v#{PageSpeed::VERSION}"
26
+ exit
27
+ end
28
+
29
+ opts.on( '-h', '--help', 'Display this help' ) do
30
+ puts opts
31
+ exit
32
+ end
33
+ end
34
+
35
+ @opts.parse!
36
+ end
37
+
38
+ # print out the options banner and exit
39
+ def print_usage_and_exit!
40
+ puts @opts
41
+ exit
42
+ end
43
+
44
+ # get the api key from ~/.pagespeed_api_key
45
+ # if we can't find it, show a user how to get one
46
+ def get_api_key
47
+ if File.exist?(KEY_PATH)
48
+ File.read(KEY_PATH).gsub(/\s/, '')
49
+ else
50
+ instructions = <<-INSTRUCTIONS
51
+ \033[31mLooks like you don't have an API key\033[0m
52
+ - visit the Google APIs Console. here: `https://code.google.com/apis/console'
53
+ - in the Services pane, activate the Page Speed Online API
54
+ - go to the API Access pane. The key is in the section titled "Simple API Access."
55
+ - paste the key into a file at ~/.pagespeed_api_key or add it with the pagespeed command: `pagespeed add-key YOUR_KEY'
56
+ INSTRUCTIONS
57
+ puts instructions.gsub(/^\s{10}/, '')
58
+ exit
59
+ end
60
+ end
61
+
62
+ # save the api key at ~/.pagespeed_api_key
63
+ def save_api_key(key)
64
+ File.open(KEY_PATH, 'w') { |f| f.write(key) }
65
+ end
66
+
67
+ # parse the options and make the pagespeed request
68
+ def run!(argv)
69
+ set_options
70
+
71
+ if argv.size == 1
72
+ api_key = get_api_key
73
+ request = PageSpeed::Request.new(argv[0], api_key)
74
+ request.pagespeed
75
+ elsif argv.size == 2 && argv[0] == 'add-key'
76
+ save_api_key(argv[1])
77
+ else
78
+ print_usage_and_exit!
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,67 @@
1
+ require 'json'
2
+
3
+ module PageSpeed
4
+ class Parser
5
+ class << self
6
+
7
+ # parse the response and print it real pretty
8
+ def parse(result)
9
+ result = JSON.parse(result)
10
+ code = result['responseCode']
11
+ total_score = result['score']
12
+
13
+ fail(code, result['title']) unless code == 200
14
+
15
+ result['formattedResults']['ruleResults'].each do |name, rule|
16
+ score = rule['ruleScore']
17
+ colorize(score)
18
+ puts " #{pad_score(score)} - #{rule['localizedRuleName']}"
19
+ end
20
+
21
+ colorize(total_score)
22
+ puts " \nTotal Score: #{total_score}"
23
+ decolorize
24
+ end
25
+
26
+ private
27
+
28
+ # fail with the appropriate code/message
29
+ def fail(code, title)
30
+ puts "\033[31m#{title}\033[0m"
31
+ exit
32
+ end
33
+
34
+ # pad the score to make it extra readable
35
+ def pad_score(score)
36
+ score = score.to_s
37
+
38
+ case score.length
39
+ when 3
40
+ score
41
+ when 2
42
+ ' ' + score
43
+ when 1
44
+ ' ' + score
45
+ end
46
+ end
47
+
48
+ # colorize the good the bad and the ugly
49
+ def colorize(score)
50
+ case score.to_i
51
+ when 90..100
52
+ print "\033[32m"
53
+ when 70..89
54
+ print "\033[33m"
55
+ else
56
+ print "\033[31m"
57
+ end
58
+ end
59
+
60
+ # reset colors
61
+ def decolorize
62
+ print "\033[0m"
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,51 @@
1
+ require 'net/https'
2
+ require 'uri'
3
+
4
+ module PageSpeed
5
+ class Request
6
+ PAGESPEED_API_URL = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed'
7
+
8
+ attr_accessor :url, :api_key
9
+
10
+ def initialize(url, api_key)
11
+ @url = url =~ /^https?:\/\// ? url : ('http://' + url )
12
+ @api_key = api_key
13
+ @uri = build_request_uri
14
+ end
15
+
16
+ def pagespeed
17
+ http = Net::HTTP.new(@uri.host, @uri.port)
18
+ http.use_ssl = true
19
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
20
+
21
+ request = Net::HTTP::Get.new(@uri.request_uri)
22
+ response = http.request(request)
23
+
24
+ if response.code.to_i == 200
25
+ PageSpeed::Parser.parse(response.body)
26
+ else
27
+ status_error(response)
28
+ end
29
+
30
+ rescue Exception => e
31
+ puts e.message
32
+ puts e.backtrace.join("\n")
33
+ puts "\033[31mUh oh, didn't work. Maybe the host is down or the url is wrong... or perhaps google is down :("
34
+ exit
35
+ end
36
+
37
+ def status_error(response)
38
+ puts "#{response.code}"
39
+ exit
40
+ end
41
+
42
+ private
43
+
44
+ def build_request_uri
45
+ uri = URI.parse(PAGESPEED_API_URL)
46
+ uri.query = "url=#{@url}&key=#{@api_key}"
47
+ uri
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module PageSpeed
2
+ VERSION = '0.0.1'
3
+ end
data/pagespeed.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'pagespeed/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'pagespeed'
7
+ s.version = PageSpeed::VERSION
8
+ s.authors = ['blahed']
9
+ s.email = ['tdunn13@gmail.com']
10
+ s.homepage = ''
11
+ s.summary = 'Pulls google pagespeed results for a given site'
12
+ s.description = ''
13
+
14
+ s.rubyforge_project = 'pagespeed'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pagespeed
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - blahed
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-28 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: ""
23
+ email:
24
+ - tdunn13@gmail.com
25
+ executables:
26
+ - pagespeed
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - bin/pagespeed
38
+ - lib/pagespeed.rb
39
+ - lib/pagespeed/cli.rb
40
+ - lib/pagespeed/parser.rb
41
+ - lib/pagespeed/request.rb
42
+ - lib/pagespeed/version.rb
43
+ - pagespeed.gemspec
44
+ has_rdoc: true
45
+ homepage: ""
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project: pagespeed
74
+ rubygems_version: 1.6.2
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Pulls google pagespeed results for a given site
78
+ test_files: []
79
+