ShortRails

URL shortener for Rails 6.

Installation

Add this line to your application's Gemfile:

gem 'short_rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install short_rails

Configuration

Mount engine in config/routes.rb:

Rails.application.routes.draw do
  mount ShortRails::Engine => "/s"
end

Copy engine migrations to application:

$ bin/rails short_rails:install:migrations
$ bin/rails db:migrate

Configure optional host whitelist (default: nil, disabled) in initializer (e.g. config/initializers/short_rails.rb):

ShortRails.setup do |config|
  config.host_whitelist = ['example.com', '127.0.0.1']
end

Usage

  • POST /s: Create short URL

Submit data:

  • base_url: base URL without parameters (e.g. http://example.com/path)
  • base_url_short_param: optional parameter for short id on redirect (e.g. s)
  • URL parameters

Example request:

  curl -F base_url=http://example.com/path -F base_url_short_param=s -F x=1 -F y=2 http://localhost:3000/s

Returns short id as text (e.g. abc123)

  • GET /s/abc123: Open short URL

If base_url_short_param has been set, redirect to <base_url>?<base_url_short_param>=<short id> (e.g. http://example.com/path?s=abc123), otherwise redirect to full URL.

  • GET /s/abc123/params: Get full URL parameters as JSON

  • GET /s/abc123/qrcode: Get QR code as PNG image

Parameters:

  • width (optional): image width (max 2048), will be clamped to minimum QR code width
  • height (optional): image height (max 2048), will be clamped to minimum QR code height

Development

Run standalone dummy app at http://localhost:3000/s:

$ cd test/dummy/
$ bin/rails s

License

The gem is available as open source under the terms of the MIT License.