Bitmarkable

Generate and persist a bitly URL to an ActiveRecord object using Redis.

Installation

Add this line to your application's Gemfile:

gem 'bitmarkable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install bitmarkable

Usage

Bitmarkable.configure do |c|
  c. = "username" # bitly username
  c.api_key = "12345" # https://bitly.com/a/your_api_key
  c.base_url = Rails.application.config.base_url # base URL to prepend, if you're using relative paths
end

Then, include it in your model.

include Bitmarkable::Bitmarked
bitmark :site_url # optional method/attribute containing the URL to shorten, defaults to :url

The short URL can then be fetched using instance.bitmark. The value is stored in Redis with the key class_name:id:bitmark.

If the bitmarkable class responds to .delay (e.g. you're using DelayedJob or Sidekiq) then bitmarks will be processed asynchronously.

If running in Rails and in development or test environments, the bitmark will be set to the full localhost URL and the bitly API will not be called (because it will not shorten localhost URLs). The URL will still be set asynchronously (if applicable) to mimic production behavior.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request