hit_counter 0.0.1 → 0.0.2
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/README.rdoc +38 -9
- data/VERSION +1 -1
- data/hit_counter.gemspec +2 -2
- data/lib/hit_counter.rb +14 -3
- data/spec/lib/hit_counter_spec.rb +45 -1
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -4,38 +4,67 @@ Ruby version of that old 90s chestnut, the web-site hit counter.
|
|
4
4
|
|
5
5
|
== How to Install
|
6
6
|
|
7
|
-
1. Install the gem.
|
7
|
+
1. Install the gem and supporting files.
|
8
8
|
|
9
|
-
Gemfile
|
9
|
+
+Gemfile+:
|
10
10
|
|
11
11
|
gem 'hit_counter'
|
12
12
|
|
13
|
+
Run:
|
14
|
+
|
15
|
+
bundle
|
16
|
+
rake hit_counter:install
|
17
|
+
|
13
18
|
2. Add a controller action to your app.
|
14
19
|
|
15
|
-
|
20
|
+
+application_controller.rb+:
|
16
21
|
|
17
22
|
def hit_counter
|
18
23
|
return if params[:url].blank?
|
24
|
+
|
25
|
+
# find or create a hit counter for this URL
|
19
26
|
hc = HitCounter.get params[:url]
|
27
|
+
|
28
|
+
# increase the tally by one
|
20
29
|
hc.increment
|
30
|
+
|
31
|
+
# get the image as a blog and stream it to the browser
|
21
32
|
send_data hc.image(params[:style]).to_blob, :disposition => 'inline', :filename => "#{hc.hits}.png", :type => :png
|
22
33
|
end
|
23
34
|
|
24
|
-
routes.rb
|
35
|
+
+routes.rb+:
|
25
36
|
|
26
|
-
get 'hit-counter' => '
|
37
|
+
get 'hit-counter' => 'application#hit_counter' # technically should be POST/PUT, but GET makes integration simpler
|
27
38
|
|
28
39
|
3. Add the hit-counter image tag to your site's HTML:
|
29
40
|
|
30
|
-
|
41
|
+
<img alt="Hit Counter" border="0" src="/hit-counter?url=http://cnn.com&style=1" />
|
31
42
|
|
32
43
|
== How to Customize the Hit-Counter Image
|
33
44
|
|
34
|
-
|
45
|
+
=== Use an Existing Style
|
46
|
+
|
47
|
+
* You can use one of the three included styles by specifying a different +style+ param in the HTML:
|
48
|
+
|
49
|
+
<img alt="Hit Counter" border="0" src="/hit-counter?url=cnn.com&style=1" />
|
50
|
+
|
51
|
+
The included styles are:
|
52
|
+
|
53
|
+
http://github.com/ivanoblomov/hit_counter/raw/master/public/images/digits/odometer/1.gif odometer
|
54
|
+
http://github.com/ivanoblomov/hit_counter/raw/master/public/images/digits/scout/2.gif scout
|
55
|
+
http://github.com/ivanoblomov/hit_counter/raw/master/public/images/digits/celtic/3.gif Celtic
|
56
|
+
|
57
|
+
=== Create Your Own Style
|
58
|
+
|
59
|
+
1. To add your own style, create a GIF for each of the digits, +0.gif+ through +9.gif+. Save the images in a folder named after your new style in +public/images/digits+.
|
60
|
+
|
61
|
+
2. In your controller, declare a new +STYLES+ constant and add the folder name to it:
|
62
|
+
|
63
|
+
HitCounter::STYLES = ['odometer', 'scout', 'celtic', '(folder name)']
|
35
64
|
|
36
|
-
|
65
|
+
3. In your HTML, specify the new style with a +style+ param equal to its index in the array plus one (unlike arrays, the +style+ param's index is one-based):
|
37
66
|
|
38
|
-
|
67
|
+
<img alt="Hit Counter" border="0" src="/hit-counter?url=cnn.com&style=4" />
|
39
68
|
|
40
69
|
== Contributing to hit_counter
|
41
70
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/hit_counter.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "hit_counter"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Roderick Monje"]
|
12
|
-
s.date = "2011-09-
|
12
|
+
s.date = "2011-09-18"
|
13
13
|
s.email = "rod@seologic.com"
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
data/lib/hit_counter.rb
CHANGED
@@ -79,8 +79,8 @@ class HitCounter
|
|
79
79
|
# - +style_number+ -> the image style
|
80
80
|
# * *Returns*
|
81
81
|
# - the current hit count as a PNG image
|
82
|
-
def image style_number
|
83
|
-
image = HitCounter.cat_image self.hits.to_s, style_number
|
82
|
+
def image style_number
|
83
|
+
image = HitCounter.cat_image self.hits.to_s, HitCounter.normalize_style_number(style_number)
|
84
84
|
image.format = 'png'
|
85
85
|
image
|
86
86
|
end
|
@@ -98,11 +98,22 @@ class HitCounter
|
|
98
98
|
|
99
99
|
STYLES = ['odometer', 'scout', 'celtic']
|
100
100
|
|
101
|
-
def self.cat_image number
|
101
|
+
def self.cat_image number, style_index, images = Magick::ImageList.new
|
102
102
|
return images.append(false) if number.blank?
|
103
103
|
HitCounter.cat_image number[1..-1], style_index, images << Magick::Image.read("#{Rails.root}/public/images/digits/#{STYLES[style_index]}/#{number[0..0]}.gif").first
|
104
104
|
end
|
105
105
|
|
106
|
+
def self.normalize_style_number value
|
107
|
+
value = value.to_i
|
108
|
+
value -= 1 if value > 0
|
109
|
+
|
110
|
+
if value >= 0 && value < STYLES.size
|
111
|
+
return value
|
112
|
+
else
|
113
|
+
value % 3
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
106
117
|
def self.normalize_url value
|
107
118
|
value !~ %r{^http://} ? "http://#{value}" : value
|
108
119
|
end
|
@@ -35,12 +35,56 @@ describe HitCounter do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
describe '#image' do
|
38
|
-
subject { @hit_counter.image }
|
38
|
+
subject { @hit_counter.image '1' }
|
39
39
|
|
40
40
|
it { should be_a Magick::Image }
|
41
41
|
pending 'should reflect hits'
|
42
42
|
end
|
43
43
|
|
44
|
+
describe '#normalize_style_number' do
|
45
|
+
context 'when undefined' do
|
46
|
+
it 'should return 0' do
|
47
|
+
HitCounter.normalize_style_number(nil).should == 0
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'when within range' do
|
52
|
+
it 'should return 0 for 1' do
|
53
|
+
HitCounter.normalize_style_number('1').should == 0
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should return 2 for 3' do
|
57
|
+
HitCounter.normalize_style_number('3').should == 2
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when outside range' do
|
62
|
+
context 'and negative' do
|
63
|
+
context 'should return 2 for' do
|
64
|
+
it -1 do
|
65
|
+
HitCounter.normalize_style_number('-1').should == 2
|
66
|
+
end
|
67
|
+
|
68
|
+
it -31 do
|
69
|
+
HitCounter.normalize_style_number('-31').should == 2
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'and positive' do
|
75
|
+
context 'should return 0 for' do
|
76
|
+
it 4 do
|
77
|
+
HitCounter.normalize_style_number('4').should == 0
|
78
|
+
end
|
79
|
+
|
80
|
+
it 34 do
|
81
|
+
HitCounter.normalize_style_number('34').should == 0
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
44
88
|
describe '#normalize_url' do
|
45
89
|
context 'with "cnn.com"' do
|
46
90
|
specify { HitCounter.normalize_url('cnn.com').should == 'http://cnn.com' }
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hit_counter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Roderick Monje
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-09-
|
18
|
+
date: 2011-09-18 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: addressable
|