rack-chartbeat 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-chartbeat (0.0.3)
5
+ rack (>= 1.2.0, <= 2.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ minitest (2.11.4)
11
+ rack (1.4.1)
12
+ rack-test (0.6.2)
13
+ rack (>= 1.0)
14
+ rake (0.9.5)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ minitest (~> 2.11.4)
21
+ rack-chartbeat!
22
+ rack-test (~> 0.6.1)
23
+ rake (~> 0.9.2)
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Mattt Thompson (http://mattt.me)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # rack-chartbeat
2
+
3
+ Rack middleware to automatically include [Chartbeat](http://chartbeat.com) embed codes.
4
+
5
+ ## Example
6
+
7
+ ```ruby
8
+ require "rack/chartbeat"
9
+
10
+ use Rack::Chartbeat, uid: 12345, domain: "example.com"
11
+ ```
12
+
13
+ Including this in the `config.ru` file of your Rack application will automatically inject the corresponding JavaScript into the `<head>` and `<body>` of your HTML, respectively:
14
+
15
+ ```html
16
+ <script type="text/javascript">var _sf_startpt=(new Date()).getTime()</script>
17
+ ```
18
+
19
+ ```html
20
+ <script type="text/javascript">
21
+ var _sf_async_config={uid:12345,domain:"example.com"};
22
+ (function(){
23
+ function loadChartbeat() {
24
+ window._sf_endpt=(new Date()).getTime();
25
+ var e = document.createElement('script');
26
+ e.setAttribute('language', 'javascript');
27
+ e.setAttribute('type', 'text/javascript');
28
+ e.setAttribute('src',
29
+ (("https:" == document.location.protocol) ? "https://a248.e.akamai.net/chartbeat.download.akamai.com/102508/" : "http://static.chartbeat.com/") +
30
+ "js/chartbeat.js");
31
+ document.body.appendChild(e);
32
+ }
33
+ var oldonload = window.onload;
34
+ window.onload = (typeof window.onload != 'function') ?
35
+ loadChartbeat : function() { oldonload(); loadChartbeat(); };
36
+ })();
37
+ </script>
38
+ ```
39
+
40
+ ## License
41
+
42
+ rack-chartbeat is available under the MIT license. See the LICENSE file for more info.
@@ -0,0 +1,10 @@
1
+ require "bundler"
2
+ Bundler.setup
3
+
4
+ gemspec = eval(File.read("rack-chartbeat.gemspec"))
5
+
6
+ task :build => "#{gemspec.full_name}.gem"
7
+
8
+ file "#{gemspec.full_name}.gem" => gemspec.files + ["rack-chartbeat.gemspec"] do
9
+ system "gem build rack-chartbeat.gemspec"
10
+ end
@@ -0,0 +1,60 @@
1
+ module Rack #:nodoc:
2
+ class Chartbeat
3
+
4
+ VERSION = "0.0.3"
5
+
6
+ def initialize(app, options = {})
7
+ raise ArgumentError, "Chartbeat UID Required" unless options[:uid]
8
+ raise ArgumentError, "Domain Required" unless options[:domain] &&
9
+ !options[:domain].empty?
10
+ @app, @options = app, options
11
+ end
12
+
13
+ def call(env)
14
+ @status, @headers, @response = @app.call(env)
15
+ return [@status, @headers, @response] unless html?
16
+ response = Rack::Response.new([], @status, @headers)
17
+ if @response.respond_to?(:to_ary)
18
+ @response.each do |fragment|
19
+ response.write inject_chartbeat(fragment)
20
+ end
21
+ end
22
+ response.finish
23
+ end
24
+
25
+ private
26
+
27
+ def html?; @headers["Content-Type"] =~ /html/; end
28
+
29
+ def inject_chartbeat(response)
30
+ script = <<-EOF
31
+ <script type="text/javascript">var _sf_startpt=(new Date()).getTime()</script>
32
+ EOF
33
+
34
+ response = response.gsub(%r{</head>}, script + "</head>")
35
+
36
+ script = <<-EOF
37
+ <script type="text/javascript">
38
+ var _sf_async_config={uid:#{@options[:uid].to_i},domain:"#{@options[:domain]}"};
39
+ (function(){
40
+ function loadChartbeat() {
41
+ window._sf_endpt=(new Date()).getTime();
42
+ var e = document.createElement('script');
43
+ e.setAttribute('language', 'javascript');
44
+ e.setAttribute('type', 'text/javascript');
45
+ e.setAttribute('src',
46
+ (("https:" == document.location.protocol) ? "https://a248.e.akamai.net/chartbeat.download.akamai.com/102508/" : "http://static.chartbeat.com/") +
47
+ "js/chartbeat.js");
48
+ document.body.appendChild(e);
49
+ }
50
+ var oldonload = window.onload;
51
+ window.onload = (typeof window.onload != 'function') ?
52
+ loadChartbeat : function() { oldonload(); loadChartbeat(); };
53
+ })();
54
+ </script>
55
+ EOF
56
+
57
+ response = response.gsub(%r{</body>}, script + "</body>")
58
+ end
59
+ end
60
+ end
Binary file
Binary file
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rack/chartbeat"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rack-chartbeat"
7
+ s.authors = ["Mattt Thompson"]
8
+ s.email = "m@mattt.me"
9
+ s.homepage = "http://github.com/mattt/rack-chartbeat"
10
+ s.version = Rack::Chartbeat::VERSION
11
+ s.platform = Gem::Platform::RUBY
12
+ s.summary = "rack-chartbeat"
13
+ s.description = "Rack middleware to automatically include Chartbeat embed codes in your site."
14
+
15
+ s.add_runtime_dependency "rack", ">= 1.2.0", "<= 2.0.0"
16
+
17
+ s.add_development_dependency "minitest", "~> 2.11.4"
18
+ s.add_development_dependency "rack-test", "~> 0.6.1"
19
+ s.add_development_dependency "rake", "~> 0.9.2"
20
+
21
+ s.files = Dir["./**/*"].reject { |file| file =~ /\.\/(bin|log|pkg|script|spec|test|vendor)/ }
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,10 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Rack::Chartbeat Test</title>
5
+ </head>
6
+ <body>
7
+ <h1>Rack::Chartbeat</h1>
8
+ <p>Test file.</p>
9
+ </body>
10
+ </html>
@@ -0,0 +1,44 @@
1
+ require "minitest/autorun"
2
+ require "minitest/pride"
3
+ require "rack"
4
+ require "rack/lint"
5
+ require "rack/mock"
6
+ require "rack/chartbeat"
7
+
8
+ describe Rack::Chartbeat do
9
+ let(:html_file) do
10
+ File.read(File.expand_path("../fixtures/test.html", __FILE__))
11
+ end
12
+ let(:html_app) { proc{[200,{"Content-Type"=>"text/html"},[html_file]]} }
13
+ let(:text_app) { proc{[200,{"Content-Type"=>"text/plain"},["FOO"]]} }
14
+ let(:chartbeat_html) { Rack::Chartbeat.new(html_app, :uid => "12345", :domain => "example.com") }
15
+ let(:chartbeat_text) { Rack::Chartbeat.new(text_app, :uid => "12345", :domain => "example.com") }
16
+
17
+ it { proc{ Rack::Chartbeat.new(nil) }.must_raise(ArgumentError) }
18
+
19
+ it { chartbeat_html.must_be_kind_of(Rack::Chartbeat) }
20
+ it { chartbeat_text.must_be_kind_of(Rack::Chartbeat) }
21
+
22
+ describe "script injection" do
23
+ describe "html" do
24
+ let(:request) { Rack::MockRequest.new(Rack::Lint.new(chartbeat_html)) }
25
+ let(:response) { request.get("/") }
26
+
27
+ it { response.status.must_equal 200 }
28
+ it { response.body.must_match %(<script type="text/javascript">var _sf_startpt=(new Date()).getTime()</script>) }
29
+ it { response.body.must_match %(var _sf_async_config={uid:12345,domain:"example.com"};) }
30
+ it { response.body.must_match "<title>Rack::Chartbeat Test</title>" }
31
+ it { response.body.must_match "<p>Test file.</p>" }
32
+ end
33
+
34
+ describe "text" do
35
+ let(:request) { Rack::MockRequest.new(Rack::Lint.new(chartbeat_text)) }
36
+ let(:response) { request.get("/") }
37
+
38
+ it { response.status.must_equal 200 }
39
+ it { response.body.wont_match %(<script type="text/javascript">var _sf_startpt=(new Date()).getTime()</script>) }
40
+ it { response.body.wont_match "var _sf_async_config={uid:12345,domain:"example.com"};" }
41
+ it { response.body.must_match "FOO" }
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-chartbeat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mattt Thompson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.2.0
22
+ - - <=
23
+ - !ruby/object:Gem::Version
24
+ version: 2.0.0
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.2.0
33
+ - - <=
34
+ - !ruby/object:Gem::Version
35
+ version: 2.0.0
36
+ - !ruby/object:Gem::Dependency
37
+ name: minitest
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.11.4
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ~>
50
+ - !ruby/object:Gem::Version
51
+ version: 2.11.4
52
+ - !ruby/object:Gem::Dependency
53
+ name: rack-test
54
+ requirement: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: 0.6.1
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 0.6.1
68
+ - !ruby/object:Gem::Dependency
69
+ name: rake
70
+ requirement: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: 0.9.2
84
+ description: Rack middleware to automatically include Chartbeat embed codes in your
85
+ site.
86
+ email: m@mattt.me
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ./Gemfile
92
+ - ./Gemfile.lock
93
+ - ./lib/rack/chartbeat.rb
94
+ - ./LICENSE
95
+ - ./rack-chartbeat-0.0.1.gem
96
+ - ./rack-chartbeat-0.0.2.gem
97
+ - ./rack-chartbeat.gemspec
98
+ - ./Rakefile
99
+ - ./README.md
100
+ - spec/fixtures/test.html
101
+ - spec/rack_chartbeat_spec.rb
102
+ homepage: http://github.com/mattt/rack-chartbeat
103
+ licenses: []
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ segments:
115
+ - 0
116
+ hash: -480734142497803378
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ segments:
124
+ - 0
125
+ hash: -480734142497803378
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.24
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: rack-chartbeat
132
+ test_files:
133
+ - spec/fixtures/test.html
134
+ - spec/rack_chartbeat_spec.rb