pagespeedhelper 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pagespeedhelper.rb +108 -0
  3. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a03af15de632e608fbad8e143536a99b73355350
4
+ data.tar.gz: 20f30894f6f46a37438785daa075d04547eb2fdf
5
+ SHA512:
6
+ metadata.gz: c47b205f5b056994d71ddc5f9edc51865f4885e1ebcd009ddc5164fd3450cf5970a035145199b959cf5265b54828d6c9c341b88f812fc5bde903f1460a5daca2
7
+ data.tar.gz: 69d64f18015b3515c357549b8b7e2efbd0beb1a69b808d829085787d375de1e52854210b56385bf17be01909ea97e8fe248f1da40c72a4cbf45f5c02e3006b68
@@ -0,0 +1,108 @@
1
+ require 'google/apis/pagespeedonline_v2'
2
+ require 'google/apis'
3
+ require 'logger'
4
+
5
+ class PagespeedHelper
6
+
7
+ Pagespeedonline = Google::Apis::PagespeedonlineV2
8
+
9
+ def initialize(key, debug=false)
10
+ @psservice = Pagespeedonline::PagespeedonlineService.new
11
+ @psservice.key = key
12
+
13
+ if debug
14
+ Google::Apis.logger = Logger.new(STDERR)
15
+ Google::Apis.logger.level = Logger::DEBUG
16
+ end
17
+ end
18
+
19
+ def query(urls, secure=false, strategy="desktop")
20
+ data = Array.new
21
+
22
+ urls = [urls] if !urls.is_a?(Array)
23
+ urls = urls.each { |url| add_protocol_if_absent!(url, secure) }
24
+
25
+ urls.each_slice(20).to_a.each do |url_list|
26
+ data.concat send_request(url_list, strategy)
27
+ sleep(0.5)
28
+ end
29
+
30
+ data
31
+ end
32
+
33
+ def self.parse(data)
34
+ results = Array.new
35
+
36
+ data.each do |result|
37
+ if !result.is_a?(Hash)
38
+ result_hash = {
39
+ "url" => result.id,
40
+ "score" => result.rule_groups["SPEED"].score,
41
+ "stats" => Hash[result.page_stats.to_h.map{ |k, v| [k.to_s, {
42
+ "name" => k.to_s.capitalize.gsub!('_', ' '),
43
+ "value" => v } ] }],
44
+ "results" => build_rule_hash(result.formatted_results.rule_results)
45
+ }
46
+ else
47
+ result_hash = result
48
+ end
49
+
50
+ results.push(result_hash)
51
+ end
52
+
53
+ results
54
+ end
55
+
56
+ def self.build_rule_hash(rule_res)
57
+ rule_hash = Hash.new
58
+
59
+ rule_res.each do |rule, info|
60
+ rule_hash[rule] = {
61
+ "name" => info.localized_rule_name,
62
+ "impact" => info.rule_impact.round(2),
63
+ "summary" => build_summary_string!(info.summary)
64
+ }
65
+ end
66
+
67
+ rule_hash
68
+ end
69
+
70
+ def self.build_summary_string!(summary)
71
+ return nil if summary.nil?
72
+
73
+ if summary.to_h.key?(:args)
74
+ summary.args.each do |arg|
75
+ summary.format.sub!(arg.key, arg.value)
76
+ summary.format.gsub!(/\{{2}|\}{2}/,'')
77
+ end
78
+ end
79
+
80
+ summary.format.split('.')[0]
81
+ end
82
+
83
+
84
+ private_class_method :build_rule_hash, :build_summary_string!
85
+
86
+ private
87
+
88
+ def send_request(urls, strategy="desktop")
89
+ data = Array.new
90
+
91
+ @psservice.batch do |ps|
92
+ urls.each do |url|
93
+ ps.run_pagespeed(url, strategy: strategy) do |result, err|
94
+ err.nil? ? data.push(result) : data.push({ "url" => url, "error" => err })
95
+ end
96
+ end
97
+ end
98
+
99
+ data
100
+ end
101
+
102
+ def add_protocol_if_absent!(url, secure=false)
103
+ if !url.include? "http://" and !url.include? "https://"
104
+ secure ? url.replace("https://" + url) : url.replace("http://" + url)
105
+ end
106
+ end
107
+
108
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pagespeedhelper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Kirk Stennett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-api-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.5'
41
+ description: Utilizes Google's Api for ruby to batch query 20 websites at a time,
42
+ and format the results.
43
+ email: kirk.stennett@getg5.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/pagespeedhelper.rb
49
+ homepage: http://rubygems.org/gems/pagespeedhelper
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.4.8
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A wrapper for Google's PageSpeed API
73
+ test_files: []