pagespeed 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d7ab645c3e17ee15826119bd35b8d4fd47049b96
4
+ data.tar.gz: 5b7892fc13b1a78e24dcf3cd98643ed39fd1f9d4
5
+ SHA512:
6
+ metadata.gz: 80eefaf2ff3eea612a6e7c11ad327ed1f5531b18e59a7aa769e50da78ac12575990f6f2a23bcfc091b27880349a19aae733971753fdef62e43e5e02c57a4d33c
7
+ data.tar.gz: bf5eec7059b177e0a297e378b1bc423ebbe79c95891fa2868691c71107a14a709afc971ef36ac7a7d0efc068125795342f61af14ef72f4c4a86d23f980bb74a2
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in pagespeed.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ end
data/README.md CHANGED
@@ -1,34 +1,39 @@
1
- a ruby wrapper around the google pagespeed api
1
+ A Ruby library for the Google PageSpeed API.
2
2
 
3
3
  ## Usage
4
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
-
5
+ ### Command Line
6
+
7
+ ```bash
8
+ $ pagespeed add-key YOUR_API_KEY
9
+ $ pagespeed -u blahed.com
10
+ - Avoid landing page redirects
11
+ - Enable compression
12
+ - Leverage browser caching
13
+ - Reduce server response time
14
+ - Minify CSS
15
+ - Minify HTML
16
+ - Minify JavaScript
17
+ - Eliminate render-blocking JavaScript and CSS in above-the-fold content
18
+ - Optimize images
19
+ - Prioritize visible content
20
+
21
+ Total Score: 87/100 (desktop)
22
+ ```
23
+
24
+ ### Ruby API
25
+
26
+ ```ruby
27
+ require 'pagespeed'
28
+
29
+ request = PageSpeed::Request.new("google.com", 'YOUR_API_KEY', 'desktop')
30
+ request.pagespeed
31
+ ```
32
+
33
+ ## Contributers
34
+
35
+ - [blahed][1]
36
+ - [danriti][2]
37
+
38
+ [1]: https://github.com/blahed
39
+ [2]: https://github.com/danriti
data/Rakefile CHANGED
@@ -1 +1,10 @@
1
1
  require 'bundler/gem_tasks'
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ puts "rspec no available"
8
+ end
9
+
10
+ task :default => ['spec']
@@ -2,27 +2,30 @@ require 'optparse'
2
2
 
3
3
  module PageSpeed
4
4
  class CLI
5
-
5
+
6
6
  class << self
7
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
-
8
+
17
9
  # parse and set the options
18
- def set_options
10
+ def set_options(args)
11
+ # set default options
12
+ options = {}
13
+ options['strategy'] = 'desktop'
14
+
19
15
  @opts = OptionParser.new do |opts|
20
- opts.banner = BANNER.gsub(/^\s{4}/, '')
16
+ opts.banner = "Usage: pagespeed [url] [options]"
21
17
 
22
18
  opts.separator ''
23
19
 
20
+ opts.on( '-s', '--strategy [STRATEGY]', 'The strategy to use when analyzing the page. Valid values are \'desktop\' and \'mobile\'.') do |s|
21
+ if s == nil
22
+ print_usage_and_exit!
23
+ end
24
+ options['strategy'] = s
25
+ end
26
+
24
27
  opts.on('-v', '--version', 'Show the pagespeed version and exit') do
25
- puts "dsync v#{PageSpeed::VERSION}"
28
+ puts "pagespeed v#{PageSpeed::VERSION}"
26
29
  exit
27
30
  end
28
31
 
@@ -30,17 +33,19 @@ module PageSpeed
30
33
  puts opts
31
34
  exit
32
35
  end
36
+
33
37
  end
34
38
 
35
- @opts.parse!
39
+ @opts.parse!(args)
40
+ options
36
41
  end
37
-
42
+
38
43
  # print out the options banner and exit
39
44
  def print_usage_and_exit!
40
45
  puts @opts
41
46
  exit
42
47
  end
43
-
48
+
44
49
  # get the api key from ~/.pagespeed_api_key
45
50
  # if we can't find it, show a user how to get one
46
51
  def get_api_key
@@ -58,28 +63,27 @@ module PageSpeed
58
63
  exit
59
64
  end
60
65
  end
61
-
66
+
62
67
  # save the api key at ~/.pagespeed_api_key
63
68
  def save_api_key(key)
64
69
  File.open(KEY_PATH, 'w') { |f| f.write(key) }
65
70
  end
66
-
71
+
67
72
  # parse the options and make the pagespeed request
68
73
  def run!(argv)
69
- set_options
70
-
74
+ opts = set_options(argv)
75
+
71
76
  if argv.size == 1
72
77
  api_key = get_api_key
73
- request = PageSpeed::Request.new(argv[0], api_key)
78
+ request = PageSpeed::Request.new(argv[0], api_key, opts['strategy'])
74
79
  request.pagespeed
75
80
  elsif argv.size == 2 && argv[0] == 'add-key'
76
81
  save_api_key(argv[1])
77
82
  else
78
83
  print_usage_and_exit!
79
84
  end
80
-
81
85
  end
82
-
83
- end
86
+
87
+ end
84
88
  end
85
89
  end
@@ -3,38 +3,39 @@ require 'json'
3
3
  module PageSpeed
4
4
  class Parser
5
5
  class << self
6
-
6
+
7
7
  # parse the response and print it real pretty
8
8
  def parse(result)
9
9
  result = JSON.parse(result)
10
10
  code = result['responseCode']
11
11
  total_score = result['score']
12
-
12
+ strategy = result['request']['strategy']
13
+
13
14
  fail(code, result['title']) unless code == 200
14
-
15
+
15
16
  result['formattedResults']['ruleResults'].each do |name, rule|
16
17
  score = rule['ruleScore']
17
18
  colorize(score)
18
19
  puts " #{pad_score(score)} - #{rule['localizedRuleName']}"
19
20
  end
20
-
21
+
21
22
  colorize(total_score)
22
- puts " \nTotal Score: #{total_score}"
23
+ puts " \nTotal Score: #{total_score}/100 (#{strategy})"
23
24
  decolorize
24
25
  end
25
-
26
+
26
27
  private
27
-
28
+
28
29
  # fail with the appropriate code/message
29
30
  def fail(code, title)
30
31
  puts "\033[31m#{title}\033[0m"
31
32
  exit
32
33
  end
33
-
34
+
34
35
  # pad the score to make it extra readable
35
36
  def pad_score(score)
36
37
  score = score.to_s
37
-
38
+
38
39
  case score.length
39
40
  when 3
40
41
  score
@@ -44,7 +45,7 @@ module PageSpeed
44
45
  ' ' + score
45
46
  end
46
47
  end
47
-
48
+
48
49
  # colorize the good the bad and the ugly
49
50
  def colorize(score)
50
51
  case score.to_i
@@ -56,12 +57,12 @@ module PageSpeed
56
57
  print "\033[31m"
57
58
  end
58
59
  end
59
-
60
+
60
61
  # reset colors
61
62
  def decolorize
62
63
  print "\033[0m"
63
64
  end
64
-
65
+
65
66
  end
66
67
  end
67
- end
68
+ end
@@ -4,48 +4,49 @@ require 'uri'
4
4
  module PageSpeed
5
5
  class Request
6
6
  PAGESPEED_API_URL = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed'
7
-
7
+
8
8
  attr_accessor :url, :api_key
9
-
10
- def initialize(url, api_key)
9
+
10
+ def initialize(url, api_key, strategy)
11
11
  @url = url =~ /^https?:\/\// ? url : ('http://' + url )
12
12
  @api_key = api_key
13
+ @strategy = strategy
13
14
  @uri = build_request_uri
14
15
  end
15
-
16
+
16
17
  def pagespeed
17
18
  http = Net::HTTP.new(@uri.host, @uri.port)
18
19
  http.use_ssl = true
19
20
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
20
-
21
+
21
22
  request = Net::HTTP::Get.new(@uri.request_uri)
22
23
  response = http.request(request)
23
-
24
+
24
25
  if response.code.to_i == 200
25
26
  PageSpeed::Parser.parse(response.body)
26
27
  else
27
28
  status_error(response)
28
29
  end
29
-
30
+
30
31
  rescue Exception => e
31
32
  puts e.message
32
33
  puts e.backtrace.join("\n")
33
34
  puts "\033[31mUh oh, didn't work. Maybe the host is down or the url is wrong... or perhaps google is down :("
34
35
  exit
35
36
  end
36
-
37
+
37
38
  def status_error(response)
38
39
  puts "#{response.code}"
39
40
  exit
40
41
  end
41
-
42
+
42
43
  private
43
-
44
+
44
45
  def build_request_uri
45
46
  uri = URI.parse(PAGESPEED_API_URL)
46
- uri.query = "url=#{@url}&key=#{@api_key}"
47
+ uri.query = "url=#{@url}&strategy=#{@strategy}&key=#{@api_key}"
47
48
  uri
48
49
  end
49
-
50
+
50
51
  end
51
- end
52
+ end
@@ -1,3 +1,3 @@
1
1
  module PageSpeed
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -0,0 +1,37 @@
1
+ require 'pagespeed'
2
+
3
+ describe PageSpeed::CLI do
4
+ subject(:cli) { described_class }
5
+
6
+ before do
7
+ IO.any_instance.stub(:puts) # globally
8
+ end
9
+
10
+ it "has no parameters" do
11
+ actual = cli.set_options([])
12
+ expect(actual).to eq({"strategy" => "desktop"})
13
+ end
14
+
15
+ it "has help parameter" do
16
+ lambda { cli.set_options(["-h"]) }.should raise_error SystemExit
17
+ end
18
+
19
+ it "has version parameter" do
20
+ lambda { cli.set_options(["-v"]) }.should raise_error SystemExit
21
+ end
22
+
23
+ it "has incomplete strategy parameter" do
24
+ lambda { cli.set_options(["-s"]) }.should raise_error SystemExit
25
+ end
26
+
27
+ it "has complete strategy parameter" do
28
+ actual = cli.set_options(["-s", "mobile"])
29
+ expect(actual).to eq({"strategy" => "mobile"})
30
+ end
31
+
32
+ it "has positional url argument" do
33
+ actual = cli.set_options(["google.com"])
34
+ expect(actual).to eq({"strategy" => "desktop"})
35
+ end
36
+
37
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata CHANGED
@@ -1,35 +1,25 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
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
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - blahed
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-07-28 00:00:00 -04:00
19
- default_executable:
11
+ date: 2014-03-14 00:00:00.000000000 Z
20
12
  dependencies: []
21
-
22
- description: ""
23
- email:
13
+ description: ''
14
+ email:
24
15
  - tdunn13@gmail.com
25
- executables:
16
+ executables:
26
17
  - pagespeed
27
18
  extensions: []
28
-
29
19
  extra_rdoc_files: []
30
-
31
- files:
32
- - .gitignore
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
33
23
  - Gemfile
34
24
  - LICENSE
35
25
  - README.md
@@ -41,39 +31,31 @@ files:
41
31
  - lib/pagespeed/request.rb
42
32
  - lib/pagespeed/version.rb
43
33
  - pagespeed.gemspec
44
- has_rdoc: true
45
- homepage: ""
34
+ - spec/pagespeed/cli_spec.rb
35
+ - spec/spec_helper.rb
36
+ homepage: ''
46
37
  licenses: []
47
-
38
+ metadata: {}
48
39
  post_install_message:
49
40
  rdoc_options: []
50
-
51
- require_paths:
41
+ require_paths:
52
42
  - lib
53
- required_ruby_version: !ruby/object:Gem::Requirement
54
- none: false
55
- requirements:
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
56
45
  - - ">="
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:
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
65
50
  - - ">="
66
- - !ruby/object:Gem::Version
67
- hash: 3
68
- segments:
69
- - 0
70
- version: "0"
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
71
53
  requirements: []
72
-
73
54
  rubyforge_project: pagespeed
74
- rubygems_version: 1.6.2
55
+ rubygems_version: 2.2.0
75
56
  signing_key:
76
- specification_version: 3
57
+ specification_version: 4
77
58
  summary: Pulls google pagespeed results for a given site
78
- test_files: []
79
-
59
+ test_files:
60
+ - spec/pagespeed/cli_spec.rb
61
+ - spec/spec_helper.rb