visit-counter 0.0.1
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/lib/visit-counter.rb +7 -0
- data/lib/visit-counter/key.rb +7 -0
- data/lib/visit-counter/store.rb +18 -0
- data/lib/visit-counter/version.rb +3 -0
- data/lib/visit-counter/visit_counter.rb +16 -0
- data/spec/lib/store_spec.rb +19 -0
- data/spec/lib/visit_counter_spec.rb +46 -0
- data/spec/spec_helper.rb +9 -0
- data/visit-counter.gemspec +21 -0
- metadata +96 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Tom Caspy
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Visit::Counter
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'visit-counter'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install visit-counter
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
require "visit-counter/version"
|
2
|
+
require "visit-counter/key"
|
3
|
+
require "visit-counter/visit_counter"
|
4
|
+
require "visit-counter/store/abstract_store"
|
5
|
+
require "visit-counter/store/redis_store"
|
6
|
+
require "visit-counter/store/rails_store"
|
7
|
+
require "visit-counter/store"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module VisitCounter
|
2
|
+
def incr_counter(name)
|
3
|
+
key = VisitCounter::Key.key(self, name)
|
4
|
+
count = VisitCounter::Store.engine.incr(key)
|
5
|
+
current_count = self.send(name).to_i
|
6
|
+
if current_count / (current_count + count).to_f < 0.7
|
7
|
+
self.update_attribute(name, current_count + count)
|
8
|
+
nullify_counter_cache(name)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def nullify_counter_cache(name)
|
13
|
+
key = VisitCounter::Key.key(self, name)
|
14
|
+
VisitCounter::Store.engine.nullify(key)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe VisitCounter::Store do
|
4
|
+
|
5
|
+
describe "engine" do
|
6
|
+
it "should have the redis store by default" do
|
7
|
+
VisitCounter::Store.engine.should == VisitCounter::Store::RedisStore
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be able to change the storage engine" do
|
11
|
+
VisitCounter::Store.set_engine(VisitCounter::Store::RailsStore)
|
12
|
+
VisitCounter::Store.engine.should == VisitCounter::Store::RailsStore
|
13
|
+
|
14
|
+
#switching back
|
15
|
+
VisitCounter::Store.set_engine(VisitCounter::Store::RedisStore)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
class DummyObject
|
4
|
+
include VisitCounter
|
5
|
+
|
6
|
+
attr_accessor :counter
|
7
|
+
|
8
|
+
def update_attribute(attribute, value)
|
9
|
+
self.send("#{attribute}=", value)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
VisitCounter::Store::RedisStore.redis = {host: "localhost"}
|
14
|
+
|
15
|
+
describe VisitCounter do
|
16
|
+
describe "incrementing counters" do
|
17
|
+
before :each do
|
18
|
+
@d = DummyObject.new
|
19
|
+
@d.stub!(:id).and_return(1)
|
20
|
+
@d.nullify_counter_cache(:counter)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should increase the counter from nil / zero using the incr_counter method" do
|
24
|
+
@d.counter.should be_nil
|
25
|
+
@d.incr_counter(:counter)
|
26
|
+
@d.counter.should == 1
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should not increase the counter if not passing the threshold" do
|
30
|
+
@d.counter = 100
|
31
|
+
@d.incr_counter(:counter)
|
32
|
+
@d.counter.should == 100
|
33
|
+
43.times do
|
34
|
+
@d.incr_counter(:counter)
|
35
|
+
end
|
36
|
+
@d.counter.should == 143
|
37
|
+
|
38
|
+
#should still be 143, because of the percentage thingie
|
39
|
+
43.times do
|
40
|
+
@d.incr_counter(:counter)
|
41
|
+
end
|
42
|
+
@d.counter.should == 143
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/visit-counter/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Tom Caspy"]
|
6
|
+
gem.email = ["tcaspy@gmail.com"]
|
7
|
+
gem.description = %q{Simple counter increment which only writes to DB once in a while}
|
8
|
+
gem.summary = %q{No need to write to db each visit, save the visits to a quick DB like redis or memcached, and write to the SQL db once reads exeeded a certain threshold}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "visit-counter"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = VisitCounter::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency(%q<rspec>, [">= 0"])
|
19
|
+
gem.add_development_dependency(%q<rake>, ["~> 0.9.2"])
|
20
|
+
gem.add_dependency(%q<redis>, [">= 0"])
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: visit-counter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tom Caspy
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-25 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70131008040560 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70131008040560
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70131008040040 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.9.2
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70131008040040
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: redis
|
38
|
+
requirement: &70131008039560 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70131008039560
|
47
|
+
description: Simple counter increment which only writes to DB once in a while
|
48
|
+
email:
|
49
|
+
- tcaspy@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/visit-counter.rb
|
60
|
+
- lib/visit-counter/key.rb
|
61
|
+
- lib/visit-counter/store.rb
|
62
|
+
- lib/visit-counter/version.rb
|
63
|
+
- lib/visit-counter/visit_counter.rb
|
64
|
+
- spec/lib/store_spec.rb
|
65
|
+
- spec/lib/visit_counter_spec.rb
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
- visit-counter.gemspec
|
68
|
+
homepage: ''
|
69
|
+
licenses: []
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.10
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: No need to write to db each visit, save the visits to a quick DB like redis
|
92
|
+
or memcached, and write to the SQL db once reads exeeded a certain threshold
|
93
|
+
test_files:
|
94
|
+
- spec/lib/store_spec.rb
|
95
|
+
- spec/lib/visit_counter_spec.rb
|
96
|
+
- spec/spec_helper.rb
|