Class: HitCounter
- Inherits:
-
Object
- Object
- HitCounter
- Includes:
- Mongoid::Document, Mongoid::Timestamps
- Defined in:
- lib/version.rb,
lib/hit_counter.rb
Overview
Self-hosted Ruby version of that old 90s chestnut, <BLINK>the web-site hit counter</BLINK>
Defined Under Namespace
Classes: UrlValidator
Constant Summary collapse
- VERSION =
This gem’s version
'1.0.0'
- STYLES =
Defines the available image styles as an array. Add yours to the end.
%w[odometer scout celtic].freeze
Class Method Summary collapse
-
.get(url, hits = 0) ⇒ HitCounter
Returns a
HitCounter
matching the specified URL. -
.install ⇒ Object
Installs the required Mongoid configuration and image files.
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.
54 55 56 57 58 |
# File 'lib/hit_counter.rb', line 54 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 |
.install ⇒ Object
Installs the required Mongoid configuration and image files.
65 66 67 68 69 70 |
# File 'lib/hit_counter.rb', line 65 def self.install puts 'Configuring Mongoid and installing image files...' full_gem_path = Gem::Specification.find_by_name('hit_counter').full_gem_path system "rsync -ruv #{full_gem_path}/config ." system "rsync -ruv #{full_gem_path}/public ." end |
Instance Method Details
#hits=(value) ⇒ Integer
Sets the number of hits.
80 81 82 |
# File 'lib/hit_counter.rb', line 80 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
105 106 107 108 109 |
# File 'lib/hit_counter.rb', line 105 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.
116 117 118 119 |
# File 'lib/hit_counter.rb', line 116 def increment self.hits += 1 save end |
#url=(value) ⇒ String
Sets the URL to be tracked. The http prefix is optional.
90 91 92 |
# File 'lib/hit_counter.rb', line 90 def url=(value) self[:url] = HitCounter.send(:normalize_url, value) end |