Last updated

Announcing Firefly, a ruby URL shortener

Here it is! Firefly! The easiest URL shortener there is in Ruby land!

How easy? Install the gem, copy and paste a config.ru and you’re good to go!

Read the documentation or the source for more details. I’ve included the README in this post as well for your convenience.

FYI: I’m currently running aj.gs on Firefly 0.1 with MySQL.

FireFly

FireFly is a simple URL shortener for personal use.

Installation

1sudo gem install firefly

After you have installed the Firefly gem you should create a config.ru file that tells your webserver what to do. Here is a sample config.ru:

 1require 'rubygems'
 2require 'firefly'
 3
 4disable :run
 5
 6app = Firefly::Server.new do
 7    set :hostname,    "localhost:3000"
 8    set :api_key,     "test"
 9
10    # Use Sqlite3 by default
11    set :database,    "sqlite3://#{Dir.pwd}/firefly.sqlite3"
12
13    # You can use MySQL as well.
14    # Make sure to install the do_mysql gem:
15    #    sudo gem install do_mysql
16    # set :database,    "mysql://root@localhost/firefly"
17end
18
19run app

Next you can start your web server. You may try thin:

1thin start -R config.ru

Configuration

All configuration is done in config.ru.

  • :hostname sets the hostname to use for shortened URLs.
  • :api_key sets the API key. This key is required when posting new URLs
  • :database a database URI that DataMapper can understand.

It’s possible to use all kinds of backends with DataMapper. Sqlite3 and MySQL have been tested, but others like MongoDB may work as well.

Usage

Adding a URL is done by doing a simple POST request that includes the URL and your API key.

1curl -d "url=http://ariejan.net" -d "api_key=test" http://localhost:3000/api/add

If you’re on a MacOSX you could add the following function to your ~/.profile to automate URL shortening:

1shorten(){
2    URL=$1
3    SHORT_URL=`curl -s -d "url=$URL&api_key=test" http://localhost:3000/api/add`
4    echo $SHORT_URL | pbcopy
5
6    echo "-- $URL => $SHORT_URL"
7    echo "Short URL copied to clipboard."
8}

After you restart Terminal.app (or at least reload the .profile file) you can use it like this:

1$ shorten http://ariejan.net
2-- http://ariejan.net => http://aj.gs/1
3Short URL copied to clipboard.

Bugs, Feature Requests, etc.

Feel free to fork Firefly and create patches for it. Here are some basic instructions:

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Commit, do not mess with Rakefile, VERSION, or HISTORY. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

License

Copyright (c) 2009 Ariejan de Vroom

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Tags: