HitCounter

<img src=“https://badge.fury.io/rb/hit_counter.svg” alt=“Gem Version” /> <img src=“https://codeclimate.com/github/ivanoblomov/hit_counter.png” /> <img src=“https://secure.travis-ci.org/ivanoblomov/hit_counter.png”/> <img src=“https://gemnasium.com/ivanoblomov/hit_counter.png” alt=“Dependency Status” />

Ruby version of that old 90s chestnut, <BLINK>the web-site hit counter</BLINK>.

Installation

  1. Install the gem and supporting files.

    Gemfile

    gem 'hit_counter'
    

    Run:

    bundle
    rake hit_counter:install
    
  2. Add a controller action to your app.

    application_controller.rb

    def 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
    end
    

    routes.rb

    get 'hit-counter' => 'application#hit_counter' # technically should be POST/PUT, but GET makes integration simpler
    
  3. Add the hit-counter image tag to your site’s HTML:

    <img alt="Hit Counter" border="0" src="/hit-counter?url=http://cnn.com&style=1" />
    

Customizing the Hit-Counter Image

Use an Existing Style

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" />

The included styles are:

odometer scout Celtic

Create Your Own Style

  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.

  2. In your controller, declare a new STYLES constant and add the folder name to it:

    HitCounter::STYLES = ['odometer', 'scout', 'celtic', '(folder name)']
    
  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):

    <img alt="Hit Counter" border="0" src="/hit-counter?url=cnn.com&style=4" />
    

Contributing to hit_counter

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 Roderick Monje. See LICENSE.txt for further details.