pagespeedhelper 0.3.1 → 0.4.1

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 +4 -4
  2. data/lib/pagespeedhelper.rb +36 -7
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5110042bea6a93493b44a485133f4e57ce5dee5d
4
- data.tar.gz: 688de312a32498747b26ecfdc5a82395287e31aa
3
+ metadata.gz: 8e2d33727ae481ad8eea2d8bf9ab79e758ea20ca
4
+ data.tar.gz: 5385ef3013641f3b4382cf4982a56e49435d8d5b
5
5
  SHA512:
6
- metadata.gz: 7abc65dcdab8d4b290330eb6d305170a28257ac91d20d693082474763bf549a96f042694f9f647c9a5760a327bba60a327e638237a909c5364201777a32ff71f
7
- data.tar.gz: 0dc2a47cdaba059686b7f7634f5efc8f88417fc949ff0da7826e9fac589facd6c976c5248da8414049f481a78702fe0395b51f3241fb8c368b49b4efc131d296
6
+ metadata.gz: 28f738beae2467d992cf97e74613de0bd947560129814d4bd658e23b59692fe6a7405c2df4d389ed2dd6ad6e6fe1ee39bddd39bdf1e4d4e616ca5fecc3ea4725
7
+ data.tar.gz: 6e325e6c392825a2c4746e4f735d9a833e0e8379fd6b21f894247711a72a9af8bac944cba5d221e4e9852bee336f5e712a5e2d9ad9c7edf184e7e572525ef08e
@@ -6,9 +6,10 @@ class PagespeedHelper
6
6
 
7
7
  Pagespeedonline = Google::Apis::PagespeedonlineV2
8
8
 
9
- def initialize(key, debug=false)
9
+ def initialize(key, limit=32, debug=false)
10
10
  @psservice = Pagespeedonline::PagespeedonlineService.new
11
11
  @psservice.key = key
12
+ @wait_limit = limit
12
13
 
13
14
  if debug
14
15
  Google::Apis.logger = Logger.new(STDERR)
@@ -16,20 +17,40 @@ class PagespeedHelper
16
17
  end
17
18
  end
18
19
 
19
- def query(urls, secure=false, strategy="desktop")
20
+ def query(urls, strategy="desktop", secure="false")
21
+ @wait_time = 1
20
22
  data = Array.new
21
23
 
22
24
  urls = [urls] if !urls.is_a?(Array)
23
25
  urls = urls.each { |url| add_protocol_if_absent!(url, secure) }
24
26
 
25
27
  urls.each_slice(20).to_a.each do |url_list|
26
- data.concat send_request(url_list, strategy)
27
- sleep(0.5)
28
- end
28
+
29
+ begin
30
+ results = send_request(url_list, strategy)
31
+ end while rate_error?(results)
29
32
 
33
+ data.concat results
34
+ sleep(3)
35
+ end
36
+
30
37
  data
31
38
  end
32
39
 
40
+ def rate_error?(data)
41
+ data.each do |d|
42
+ if d.is_a?(Hash)
43
+ if d["error"].include?("rateLimitExceeded") or d["error"].include?("userRateLimitExceeded")
44
+ puts "RATE ERROR OCCURRED: Waiting #{@wait_time} seconds"
45
+ sleep(@wait_time)
46
+ @wait_time < @wait_limit ? @wait_time *= 2 : @wait_limit
47
+ return true
48
+ end
49
+ end
50
+ end
51
+ return false
52
+ end
53
+
33
54
  def self.parse(data)
34
55
  results = Array.new
35
56
 
@@ -85,17 +106,25 @@ class PagespeedHelper
85
106
 
86
107
  private
87
108
 
109
+ def time_throttle
110
+ finish = Time.now
111
+ puts finish - @start
112
+ if finish - @start < 100
113
+ sleep(100 - (finish - @start))
114
+ end
115
+ @start = Time.now
116
+ end
117
+
88
118
  def send_request(urls, strategy="desktop")
89
119
  data = Array.new
90
120
 
91
121
  @psservice.batch do |ps|
92
122
  urls.each do |url|
93
123
  ps.run_pagespeed(url, strategy: strategy) do |result, err|
94
- err.nil? ? data.push(result) : data.push({ "url" => url, "error" => err })
124
+ err.nil? ? data.push(result) : data.push({ "url" => url, "error" => err.to_s })
95
125
  end
96
126
  end
97
127
  end
98
-
99
128
  data
100
129
  end
101
130
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagespeedhelper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirk Stennett