An easy bit.ly toolkit with Redis as a caching layer.
Installation
Add this line to your application's Gemfile:
gem 'cached_bitly'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cached_bitly
Usage
To communicate with bit.ly you'll need your login and API key which you can get from the Advanced tab within your account settings.
If you set the bit.ly environment variables everything will just work:
BITLY_LOGIN=dewski
BITLY_API_KEY=Z_bf4b4fg16991dd72d276e7z9d94d1bc00b
You may also set the bit.ly client directly with your own configuration:
CachedBitly.bitly_client = Bitly.new('username', 'password')
There are 2 main methods to interface with bit.ly, the first being a way to retreive already generated URLs:
# First lookup will generate the URL from bit.ly
CachedBitly.fetch('https://github.com') # => http://bit.ly/WuNWHc
# Hits redis with no additional HTTP request
CachedBitly.fetch('https://github.com') # => http://bit.ly/WuNWHc
If you'd like to just pass in a large block of HTML you can cache multiple links at once:
content = "<a href='https://github.com'>GitHub</a> and <a href='https://github.com/dewski'>@dewski</a> join forces"
CachedBitly.clean(content) # => "<a href='http://bit.ly/WuNWHc'>GitHub</a> and <a href='http://bit.ly/10p297A'>@dewski</a> join forces"
Configuring CachedBitly
If you don't want to shorten all links within your HTML you can bypass bit.ly by setting your allowed hostnames:
CachedBitly.allowed_hostnames = ['github.com']
content = "<a href='https://github.com'>GitHub</a> and <a href='http://garrettbjerkhoel.com'>Garrett</a> join forces"
CachedBitly.clean(content) # => "<a href='https://github.com'>GitHub</a> and <a href='http://bit.ly/10p297A'>@dewski</a> join forces"
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request