page_speed 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/README.md CHANGED
@@ -17,7 +17,11 @@ Simply install the gem:
17
17
  Usage
18
18
  -----
19
19
 
20
- Just execute it against any website you can imagine:
20
+ You have to get your API key first through [here](http://code.google.com/intl/en-EN/apis/pagespeedonline/v1/getting_started.html), then set it to the gem:
21
+
22
+ page_speed -k YOUR_KEY
23
+
24
+ After that, just execute page_speed against any website you can imagine:
21
25
 
22
26
  page_speed URL
23
27
 
data/bin/page_speed CHANGED
@@ -4,9 +4,34 @@
4
4
  $:.unshift(File.dirname(__FILE__) + '/../lib')
5
5
  require 'page_speed'
6
6
 
7
- unless ARGV.length == 1 && URL = ARGV[0]
8
- puts "There has been an error. Please check your URL."
7
+ if ARGV.length == 2
8
+ arg1, api_key = ARGV[0..1]
9
+ if arg1 == "-k"
10
+ PageSpeed::set_api_key(api_key)
11
+ puts "API key set."
12
+ exit
13
+ end
14
+ end
15
+
16
+ unless PageSpeed::api_key_exists?
17
+ puts "API key not set. Use the -k option to set the key."
18
+ puts ""
19
+ PageSpeed::help
9
20
  exit
10
21
  end
11
22
 
12
- PageSpeed::analyze(URL)
23
+ case ARGV.length
24
+ when 1
25
+ arg1 = ARGV[0]
26
+ case arg1
27
+ when "-v", "--version"
28
+ PageSpeed::version
29
+ when "-h", "--help"
30
+ PageSpeed::help
31
+ else
32
+ PageSpeed::analyze(arg1)
33
+ end
34
+ else
35
+ puts "Please check your syntax.\n"
36
+ PageSpeed::help
37
+ end
@@ -1,3 +1,3 @@
1
1
  module PageSpeed
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/page_speed.rb CHANGED
@@ -3,7 +3,6 @@ require "page_speed/version"
3
3
  require 'json'
4
4
  require 'open-uri'
5
5
 
6
- API_KEY = "AIzaSyB-WW7oKAfD0T9cLGMufjiWA1mtLwDd4hg"
7
6
  API_URL = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed"
8
7
 
9
8
  module PageSpeed
@@ -18,8 +17,54 @@ module PageSpeed
18
17
 
19
18
  # Show results
20
19
  show_results(uri, desktop, mobile)
20
+
21
+ rescue URI::InvalidURIError
22
+ puts "Please check your URL and try again."
23
+
24
+ rescue OpenURI::HTTPError
25
+ puts "The API you used is not correct. Use the -k option to replace it."
26
+ puts ""
27
+ reset_api_key
28
+ help
29
+
30
+ rescue Exception => e
31
+ puts "There has been an unexpected error:"
32
+ puts e.message
33
+ puts e.backtrace.inspect
34
+ end
35
+ end
36
+
37
+ def help
38
+ version
39
+ usage
40
+ end
41
+
42
+ def version
43
+ puts "page_speed gem, version #{PageSpeed::VERSION}\n"
44
+ end
45
+
46
+ def usage
47
+ puts "usage: page_speed url | [ -k key | --key key ] | [ -v | --version ] | [ -h | --help ]"
48
+ puts "You can get your key at http://code.google.com/intl/en-EN/apis/pagespeedonline/v1/getting_started.html"
49
+ end
50
+
51
+ def set_api_key(key)
52
+ File.open(api_key_path, "w") {|f| f.write(key) }
53
+ end
54
+
55
+ def get_api_key
56
+ file = File.new(api_key_path, "r")
57
+ api_key = file.gets
58
+ file.close
59
+ api_key
60
+ end
61
+
62
+ def api_key_exists?
63
+ begin
64
+ get_api_key
65
+ true
21
66
  rescue Exception
22
- puts "There has been an error. Please check your URL."
67
+ false
23
68
  end
24
69
  end
25
70
 
@@ -30,30 +75,30 @@ module PageSpeed
30
75
  url = "http://#{url}"
31
76
  end
32
77
  uri = URI.parse(url)
33
- uri.open
78
+ uri.open # ping URL; will raise a URI::InvalidURIError when failing
34
79
  uri
35
80
  end
36
81
 
37
- def scores_for(uri, opts = {})
38
- desktop = score_for(uri, true)
39
- mobile = score_for(uri, false)
82
+ def scores_for(uri)
83
+ desktop = score_for(uri, :desktop => true)
84
+ mobile = score_for(uri, :desktop => false)
40
85
  [desktop, mobile]
41
86
  end
42
87
 
43
- def score_for(uri, is_desktop)
44
- json = api_request(uri, is_desktop)
88
+ def score_for(uri, opts)
89
+ json = api_request(uri, opts)
45
90
  parse_score(json)
46
91
  end
47
92
 
48
- def api_request(uri, is_desktop)
49
- # Get options
50
- strategy =
93
+ def api_request(uri, opts = {})
94
+ # Merge options
95
+ opts = {:desktop => true}.merge(opts)
51
96
 
52
97
  # Get URL for API
53
98
  params = {}
54
99
  params["url"] = uri
55
- params["key"] = API_KEY
56
- params["strategy"] = is_desktop ? "desktop" : "mobile"
100
+ params["key"] = get_api_key
101
+ params["strategy"] = opts[:desktop] ? "desktop" : "mobile"
57
102
  params_str = params.map{|k,v| "#{k}=#{v}" }.join("&")
58
103
 
59
104
  # Send request and return JSON
@@ -69,5 +114,18 @@ module PageSpeed
69
114
  def show_results(url, desktop, mobile)
70
115
  puts "Google Page Speed for #{url}: #{desktop} (Desktop) / #{mobile} (Mobile)"
71
116
  end
117
+
118
+ def reset_api_key
119
+ File.delete(api_key_path)
120
+ end
121
+
122
+ def api_key_path
123
+ dir = testing ? "/tmp" : File.expand_path("~")
124
+ dir + "/.page_speed"
125
+ end
126
+
127
+ def testing
128
+ ! defined?(ENV['page_speed_env']).nil? && ENV['page_speed_env'] == "test"
129
+ end
72
130
  end
73
131
  end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ ENV['page_speed_env'] = "test"
4
+ API_KEY = "AIzaSyCT0PgAW-FjLF0Nfx81DqU6laCSiXkIHFs"
5
+
6
+ describe PageSpeed do
7
+ before do
8
+ File.open("/tmp/.page_speed", 'w') {|f| f.write(API_KEY) } # Use a correct API KEY
9
+ end
10
+
11
+ it "should point to help when no parameter is provided" do
12
+ result = `bin/page_speed`
13
+ result.should include("Please check your syntax")
14
+ result.should include("usage: page_speed")
15
+ end
16
+
17
+ context "version" do
18
+ it "should be shown with -v" do
19
+ result = `bin/page_speed -v`
20
+ result.should include(PageSpeed::VERSION)
21
+ end
22
+
23
+ it "should be shown with --version" do
24
+ result = `bin/page_speed --version`
25
+ result.should include(PageSpeed::VERSION)
26
+ end
27
+ end
28
+
29
+ context "help" do
30
+ it "should be shown with -h" do
31
+ result = `bin/page_speed -h`
32
+ result.should include("usage: page_speed")
33
+ end
34
+
35
+ it "should be shown with --help" do
36
+ result = `bin/page_speed --help`
37
+ result.should include("usage: page_speed")
38
+ end
39
+ end
40
+
41
+ context "analyzer" do
42
+ it "should fail with a number as a parameter" do
43
+ result = `bin/page_speed 42`
44
+ result.should include("Please check your URL and try again")
45
+ end
46
+
47
+ it "should fail with a bad string as a parameter" do
48
+ result = `bin/page_speed do_the_cuca`
49
+ result.should include("Please check your URL and try again")
50
+ end
51
+
52
+ it "should success with a URL without www" do
53
+ result = `bin/page_speed google.com`
54
+ result.should include("Google Page Speed for http://google.com: 98 (Desktop) / 96 (Mobile)")
55
+ end
56
+
57
+ it "should success with a URL with www" do
58
+ result = `bin/page_speed www.facebook.com`
59
+ result.should include("Google Page Speed for http://www.facebook.com: 99 (Desktop) / 99 (Mobile)")
60
+ end
61
+ end
62
+
63
+ context "API key control" do
64
+ it "should prompt the API key when is not found" do
65
+ File.delete("/tmp/.page_speed")
66
+ result = `bin/page_speed google.com`
67
+ result.should include("PI key not set. Use the -k option to set the key.")
68
+ end
69
+
70
+ context "when API key is corrupted" do
71
+ before do
72
+ File.open("/tmp/.page_speed", 'w') {|f| f.write(API_KEY.chop) } # Use a corrupt API KEY
73
+ @result = `bin/page_speed google.com`
74
+ end
75
+
76
+ it "should prompt about it" do
77
+ @result.should include("The API you used is not correct.")
78
+ end
79
+
80
+ it "should have deleted the file where the API key is and ask for a new one" do
81
+ @result = `bin/page_speed google.com`
82
+ @result.should include("PI key not set. Use the -k option to set the key.")
83
+ end
84
+ end
85
+ end
86
+
87
+ context "key" do
88
+
89
+ end
90
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: page_speed
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.1.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Albert Bellonch
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-09-21 00:00:00 +02:00
13
+ date: 2011-09-26 00:00:00 +02:00
14
14
  default_executable: page_speed
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -46,6 +46,7 @@ extra_rdoc_files: []
46
46
 
47
47
  files:
48
48
  - .gitignore
49
+ - .rspec
49
50
  - .rvmrc
50
51
  - Gemfile
51
52
  - README.md
@@ -54,7 +55,7 @@ files:
54
55
  - lib/page_speed.rb
55
56
  - lib/page_speed/version.rb
56
57
  - page_speed.gemspec
57
- - spec/integration/pagespeed_spec.rb
58
+ - spec/integration/page_speed_spec.rb
58
59
  - spec/spec_helper.rb
59
60
  has_rdoc: true
60
61
  homepage: http://itnig.net
@@ -85,5 +86,5 @@ signing_key:
85
86
  specification_version: 3
86
87
  summary: A simple port for Google Page Speed's scores
87
88
  test_files:
88
- - spec/integration/pagespeed_spec.rb
89
+ - spec/integration/page_speed_spec.rb
89
90
  - spec/spec_helper.rb
@@ -1,4 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe PageSpeed do
4
- end