rack-counter 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/.yardoc +0 -0
- data/LICENSE +20 -0
- data/README.rdoc +30 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/rack-counter.rb +1 -0
- data/lib/rack_counter.rb +70 -0
- data/rack-counter.gemspec +69 -0
- data/spec/rack_counter_spec.rb +32 -0
- data/spec/rcov.opts +2 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +20 -0
- metadata +119 -0
data/.document
ADDED
data/.yardoc
ADDED
Binary file
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 David Dollar
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
= rack-counter
|
2
|
+
|
3
|
+
Hit counter Rack Middleware using Memcached
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
$ gem install rack-counter --source http://gemcutter.org
|
8
|
+
|
9
|
+
== Setup
|
10
|
+
|
11
|
+
use Rack::Counter
|
12
|
+
use Rack::Counter, :namespace => 'mynamespace'
|
13
|
+
|
14
|
+
== Usage
|
15
|
+
|
16
|
+
GET /_stats.json
|
17
|
+
200 OK {"hits":415,"avg_per_sec":"0.5773"}
|
18
|
+
|
19
|
+
== Note on Patches/Pull Requests
|
20
|
+
|
21
|
+
* Fork the project.
|
22
|
+
* Make your feature addition or bug fix.
|
23
|
+
* Add tests for it. This is important so I don't break it in a
|
24
|
+
future version unintentionally.
|
25
|
+
* Commit, do not mess with Rakefile or VERSION.
|
26
|
+
* Send me a pull request. Bonus points for topic branches.
|
27
|
+
|
28
|
+
== Copyright
|
29
|
+
|
30
|
+
Copyright (c) 2009 David Dollar. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "rack-counter"
|
8
|
+
gem.summary = %Q{Hit counter Rack Middleware using Memcached}
|
9
|
+
gem.description = gem.summary
|
10
|
+
gem.email = "<ddollar@gmail.com>"
|
11
|
+
gem.homepage = "http://github.com/ddollar/rack-counter"
|
12
|
+
gem.authors = ["David Dollar"]
|
13
|
+
|
14
|
+
gem.add_development_dependency "rack-test"
|
15
|
+
gem.add_development_dependency "rspec"
|
16
|
+
|
17
|
+
gem.add_dependency "memcache-client"
|
18
|
+
gem.add_dependency "rack"
|
19
|
+
gem.add_dependency "system_timer"
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
rescue LoadError
|
23
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'spec/rake/spectask'
|
27
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
30
|
+
end
|
31
|
+
|
32
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
33
|
+
spec.libs << 'lib' << 'spec'
|
34
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
35
|
+
spec.rcov = true
|
36
|
+
end
|
37
|
+
|
38
|
+
task :spec => :check_dependencies
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
begin
|
43
|
+
require 'yard'
|
44
|
+
YARD::Rake::YardocTask.new
|
45
|
+
rescue LoadError
|
46
|
+
task :yardoc do
|
47
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
48
|
+
end
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/rack-counter.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rack_counter'
|
data/lib/rack_counter.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'memcache'
|
2
|
+
require 'rack'
|
3
|
+
|
4
|
+
class Rack::Counter
|
5
|
+
|
6
|
+
attr_reader :app, :options
|
7
|
+
|
8
|
+
def initialize(app, options={})
|
9
|
+
@app = app
|
10
|
+
@options = options
|
11
|
+
|
12
|
+
reset_stats! unless initialized?
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
case env['PATH_INFO']
|
17
|
+
when '/_stats.json' then [200, {}, [stats.to_json]]
|
18
|
+
when '/_stats/reset' then reset_stats!
|
19
|
+
else record_hit!; app.call(env)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def namespace
|
26
|
+
options[:namespace] || 'rack-counter'
|
27
|
+
end
|
28
|
+
|
29
|
+
def memcache_host
|
30
|
+
options[:memcache_host] || 'localhost:11211'
|
31
|
+
end
|
32
|
+
|
33
|
+
## memcached #################################################################
|
34
|
+
|
35
|
+
def memcache
|
36
|
+
@memcache ||= MemCache.new(memcache_host, :namespace => namespace)
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialized?
|
40
|
+
memcache.get('hits', true)
|
41
|
+
end
|
42
|
+
|
43
|
+
def record_hit!
|
44
|
+
memcache.incr('hits')
|
45
|
+
end
|
46
|
+
|
47
|
+
def reset_stats!
|
48
|
+
memcache.set 'start', Time.now.utc
|
49
|
+
memcache.set 'hits', 0, 0, true
|
50
|
+
end
|
51
|
+
|
52
|
+
## stats #####################################################################
|
53
|
+
|
54
|
+
def hits
|
55
|
+
memcache.get('hits', true).to_i
|
56
|
+
end
|
57
|
+
|
58
|
+
def start
|
59
|
+
memcache.get('start')
|
60
|
+
end
|
61
|
+
|
62
|
+
def average_hits_per_second
|
63
|
+
'%0.4f' % (hits.to_f / (Time.now.utc - start))
|
64
|
+
end
|
65
|
+
|
66
|
+
def stats
|
67
|
+
{ 'hits' => hits, 'avg_per_sec' => average_hits_per_second }
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rack-counter}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["David Dollar"]
|
12
|
+
s.date = %q{2009-11-12}
|
13
|
+
s.description = %q{Hit counter Rack Middleware using Memcached}
|
14
|
+
s.email = %q{<ddollar@gmail.com>}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
".yardoc",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/rack-counter.rb",
|
28
|
+
"lib/rack_counter.rb",
|
29
|
+
"rack-counter.gemspec",
|
30
|
+
"spec/rack_counter_spec.rb",
|
31
|
+
"spec/rcov.opts",
|
32
|
+
"spec/spec.opts",
|
33
|
+
"spec/spec_helper.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/ddollar/rack-counter}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.5}
|
39
|
+
s.summary = %q{Hit counter Rack Middleware using Memcached}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/rack_counter_spec.rb",
|
42
|
+
"spec/spec_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<rack-test>, [">= 0"])
|
51
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
52
|
+
s.add_runtime_dependency(%q<memcache-client>, [">= 0"])
|
53
|
+
s.add_runtime_dependency(%q<rack>, [">= 0"])
|
54
|
+
s.add_runtime_dependency(%q<system_timer>, [">= 0"])
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<rack-test>, [">= 0"])
|
57
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
58
|
+
s.add_dependency(%q<memcache-client>, [">= 0"])
|
59
|
+
s.add_dependency(%q<rack>, [">= 0"])
|
60
|
+
s.add_dependency(%q<system_timer>, [">= 0"])
|
61
|
+
end
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<rack-test>, [">= 0"])
|
64
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
65
|
+
s.add_dependency(%q<memcache-client>, [">= 0"])
|
66
|
+
s.add_dependency(%q<rack>, [">= 0"])
|
67
|
+
s.add_dependency(%q<system_timer>, [">= 0"])
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Rack::Counter" do
|
4
|
+
|
5
|
+
include Rack::Test::Methods
|
6
|
+
|
7
|
+
describe "with no options specified" do
|
8
|
+
def app
|
9
|
+
Rack::Builder.new do
|
10
|
+
use Rack::Counter
|
11
|
+
run BlankApplication.new
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "when hitting the app several times" do
|
16
|
+
before(:each) do
|
17
|
+
@stats_before = JSON.parse(get('/_stats.json').body)
|
18
|
+
50.times { get '/' }
|
19
|
+
@stats_after = JSON.parse(get('/_stats.json').body)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "records the proper number of hits" do
|
23
|
+
(@stats_after['hits'] - @stats_before['hits']).should == 50
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should change the hits/sec" do
|
27
|
+
@stats_after['avg_per_sec'].should_not == @stats_before['avg_per_sec']
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/spec/rcov.opts
ADDED
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
|
5
|
+
require 'rack-counter'
|
6
|
+
require 'spec'
|
7
|
+
require 'spec/autorun'
|
8
|
+
|
9
|
+
require 'json'
|
10
|
+
require 'rack/test'
|
11
|
+
|
12
|
+
class BlankApplication
|
13
|
+
def call(env)
|
14
|
+
return 200, {}, ['']
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Spec::Runner.configure do |config|
|
19
|
+
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-counter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Dollar
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-12 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rack-test
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: memcache-client
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: rack
|
47
|
+
type: :runtime
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: system_timer
|
57
|
+
type: :runtime
|
58
|
+
version_requirement:
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
description: Hit counter Rack Middleware using Memcached
|
66
|
+
email: <ddollar@gmail.com>
|
67
|
+
executables: []
|
68
|
+
|
69
|
+
extensions: []
|
70
|
+
|
71
|
+
extra_rdoc_files:
|
72
|
+
- LICENSE
|
73
|
+
- README.rdoc
|
74
|
+
files:
|
75
|
+
- .document
|
76
|
+
- .gitignore
|
77
|
+
- .yardoc
|
78
|
+
- LICENSE
|
79
|
+
- README.rdoc
|
80
|
+
- Rakefile
|
81
|
+
- VERSION
|
82
|
+
- lib/rack-counter.rb
|
83
|
+
- lib/rack_counter.rb
|
84
|
+
- rack-counter.gemspec
|
85
|
+
- spec/rack_counter_spec.rb
|
86
|
+
- spec/rcov.opts
|
87
|
+
- spec/spec.opts
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
has_rdoc: true
|
90
|
+
homepage: http://github.com/ddollar/rack-counter
|
91
|
+
licenses: []
|
92
|
+
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options:
|
95
|
+
- --charset=UTF-8
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: "0"
|
103
|
+
version:
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: "0"
|
109
|
+
version:
|
110
|
+
requirements: []
|
111
|
+
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 1.3.5
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: Hit counter Rack Middleware using Memcached
|
117
|
+
test_files:
|
118
|
+
- spec/rack_counter_spec.rb
|
119
|
+
- spec/spec_helper.rb
|