Class: HitCounter
- Inherits:
-
Object
- Object
- HitCounter
- Includes:
- Mongoid::Document, Mongoid::Timestamps
- Defined in:
- lib/version.rb,
lib/hit_counter.rb
Overview
Ruby version of that old 90s chestnut, <BLINK>the web-site hit counter</BLINK>
Defined Under Namespace
Classes: UrlValidator
Constant Summary collapse
- VERSION =
'0.1.4'
- STYLES =
%w[odometer scout celtic].freeze
Class Method Summary collapse
-
.get(url, hits = 0) ⇒ HitCounter
Returns a
HitCounter
matching the specified URL.
Instance Method Summary collapse
-
#hits=(value) ⇒ Integer
Sets the number of hits.
-
#image(style_number) ⇒ Magick::Image
Returns the hit count as a PNG image, using the specified style: 1 odometer 2 scout 3 Celtic.
-
#increment ⇒ true
Increments the hit count and saves the HitCounter.
-
#url=(value) ⇒ String
Sets the URL to be tracked.
Class Method Details
.get(url, hits = 0) ⇒ HitCounter
Returns a HitCounter
matching the specified URL. The HitCounter is created if no matching one is found. In the latter case, the hits argument specifies the starting count.
52 53 54 55 56 |
# File 'lib/hit_counter.rb', line 52 def self.get(url, hits = 0) args = { url: normalize_url(url) } args[:hits] = hits unless where(conditions: args).exists? find_or_create_by args end |
Instance Method Details
#hits=(value) ⇒ Integer
Sets the number of hits.
66 67 68 |
# File 'lib/hit_counter.rb', line 66 def hits=(value) self[:hits] = value.to_i end |
#image(style_number) ⇒ Magick::Image
Returns the hit count as a PNG image, using the specified style:
1 odometer
2 scout
3 Celtic
91 92 93 94 95 |
# File 'lib/hit_counter.rb', line 91 def image(style_number) image = HitCounter.send(:cat_image, hits.to_s, HitCounter.send(:normalize_style_number, style_number)) image.format = 'png' image end |
#increment ⇒ true
Increments the hit count and saves the HitCounter.
102 103 104 105 |
# File 'lib/hit_counter.rb', line 102 def increment self.hits += 1 save end |
#url=(value) ⇒ String
Sets the URL to be tracked. The http prefix is optional.
76 77 78 |
# File 'lib/hit_counter.rb', line 76 def url=(value) self[:url] = HitCounter.send(:normalize_url, value) end |