The project is in a healthy, maintained state
RailsUrlShortener is a lightweight Rails engine that enables easy creation and management of short URLs within your project. Similar to bitly.com, it condenses long links into short, user-friendly addresses. Enhance your app's functionality with this simple yet powerful URL shortening solution.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

>= 5.3.0
>= 5.1.0
 Project Readme

Ruby on Rails CI Gem Version

GitHub License

RailsUrlShortener

RailsUrlShortener is a small Rails engine that provides your app with short URL functionality and IP logging capabilities - like having your own Bitly service. By default, RailsUrlShortener saves all visits to your links for future analysis or other interesting uses.

Why give your data to a third-party app when you can manage it yourself?

You can see a demo project of what you can do with this engine HERE.

Key Features

Here are some of the things you can do with RailsUrlShortener:

  • Generate unique keys for links
  • Provide a controller method that finds, saves request information, and performs a 301 redirect to the original URL
  • Associate short links with models in your app
  • Save browser, system, and IP data from each request
  • Create temporary short links using the expires_at option
  • Get IP data from a third-party service

Installation

Follow these steps to install and configure RailsUrlShortener:

  1. Add this line to your application's Gemfile:
gem "rails_url_shortener"
  1. Install the gem by running:
bundle install
  1. Install and run the migrations:
bin/rails rails_url_shortener:install:migrations db:migrate
  1. Generate the initializer for configuration:
rails generate rails_url_shortener

Usage

  1. Mount the engine

Mount the engine on your app adding the next code on your config/routes.rb:

If you want to mount this on the root of your app, this should be on the bottom of your routes file.

mount RailsUrlShortener::Engine, at: "/"
  1. Generate the short link

And generate the short links like you want:

  • Using the helper method, this return the ready short link.
short_url("https://www.github.com/a-chacon/rails-url-shortener")
  • Or model method, this return the object built. So you can save this on a variable, extract the key and build the short link by your own:
RailsUrlShortener::Url.generate("https://www.github.com/a-chacon/rails-url-shortener")
  1. Share the short link

Then share the short link to your users or wherever you want.

Deeper

Full params for the short_url helper:

short_url(url, owner: nil, key: nil, expires_at: nil, category: nil, url_options: {})

Where:

  • url: The long URL to be shortened
  • owner: A model from your app to associate with the URL
  • key: A custom key for the short URL (optional)
  • expires_at: Expiration datetime (after which the redirect won't work)
  • category: A tag for categorizing the link
  • url_options: Options for the URL generator (e.g., subdomain or protocol)

The generate model method accepts the same parameters except for url_options:

RailsUrlShortener::Url.generate(url, owner: nil, key: nil, expires_at: nil, category: nil)

Data Collection

By default, the engine saves all requests made to your short URLs. You can use this data for analytics or IP logging. To access the data:

  1. Get visits for a specific URL:
RailsUrlShortener::Url.find_by_key("key").visits
  1. Get all visits:
RailsUrlShortener::Visit.all

Each Visit is associated with an Ipgeo model that contains information about the IP address:

RailsUrlShortener::Visit.first.ipgeo

IP Data Collection

When a Visit record is created, a background job is enqueued to fetch IP data from the ip-api.com service and create an Ipgeo record. This uses the free endpoint, which has a limit of 45 different IPs per minute. If you expect higher traffic, you'll need to implement an alternative solution.

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

The gem is available as open source under the terms of the GPL-3.0 License.

by: a-chacon