Class: HitCounter

Inherits:
Object
  • Object
show all
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>

Examples:

hc = HitCounter.get 'cnn.com'
hc.increment

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

Instance Method Summary collapse

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.

Examples:

hc = HitCounter.get 'cnn.com'
hc = HitCounter.get 'cnn.com', 100

Parameters:

  • url (String)

    the URL for the site being counted

  • hits (Integer) (defaults to: 0)

    the number of hits to start with

Returns:



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

.installObject

Installs the required Mongoid configuration and image files.

Examples:

HitCounter.install

Returns:

  • true



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.

Examples:

hc.hits = 100

Parameters:

  • value (Integer)

    the initial number of hits

Returns:

  • (Integer)

    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

Examples:

hc.image = 1

Parameters:

  • style_number (String)

    the image style

Returns:

  • (Magick::Image)

    the current hit count as a PNG image



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

#incrementtrue

Increments the hit count and saves the HitCounter.

Examples:

hc.increment

Returns:

  • (true)

    if the save was successful



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.

Examples:

hc.url = 'cnn.com'

Parameters:

  • value (String)

    the URL for the site being counted

Returns:

  • (String)

    the normalized URL



90
91
92
# File 'lib/hit_counter.rb', line 90

def url=(value)
  self[:url] = HitCounter.send(:normalize_url, value)
end