HitCounter
Ruby version of that old 90s chestnut, <BLINK>the web-site hit counter</BLINK>.
Installation
Install the gem and supporting files.
Gemfilegem 'hit_counter'Run:
bundle rake hit_counter:installSecurity note
HitCounteris cryptographically signed. To insure the gem you install hasn’t been tampered with, add my public key as a trusted certificate and then install:gem cert --add <(curl -Ls https://raw.github.com/ivanoblomov/hit_counter/master/certs/ivanoblomov.pem) gem install hit_counter -P HighSecurityAdd a controller action to your app.
application_controller.rbdef hit_counter return if params[:url].blank? # find or create a hit counter for this URL hc = HitCounter.get params[:url] # increase the tally by one hc.increment # get the image as a blob and stream it to the browser send_data hc.image(params[:style]).to_blob, disposition: 'inline', filename: "#{hc.hits}.png", type: :png endroutes.rbget 'hit-counter' => 'application#hit_counter' # technically should be POST/PUT, but GET makes integration simpler
Or try it out in irb with:
require './spec/spec_helper'
hc = HitCounter.get('cnn.com')
Add the hit-counter image tag to your site's HTML:
<img alt="Hit Counter" border="0" src="/hit-counter?url=https://cnn.com&style=1" />
Customizing the Hit-Counter Image
Use an Existing Style
| Name | Image |
|---|---|
| Celtic | ![]() |
| Odometer | ![]() |
| Scout | ![]() |
You can use one of the three included styles by specifying a different style param in the HTML:
<img alt="Hit Counter" border="0" src="/hit-counter?url=cnn.com&style=1" />
Create Your Own Style
To add your own style, create a PNG for each of the digits,
0.pngthrough9.png. Save the images in a folder named after your new style inpublic/images/digits.In your controller, declare a new
STYLESconstant and add the folder name to it:HitCounter::STYLES = %w(odometer scout celtic folder_name)In your HTML, specify the new style with a
styleparam equal to its index in the array plus one (unlike arrays, thestyleparam's index is one-based):<img alt="Hit Counter" border="0" src="/hit-counter?url=cnn.com&style=4" />
Documentation
Complete RDoc documentation is available at RubyDoc.info.
Contributing to HitCounter
Cheers!
Copyright
Copyright © 2011-2021 Roderick Monje. See LICENSE.txt for further details.



