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.
GPL-3.0 License
RailsUrlShortener is a small rails engine that provide your app with short URLs functionalities. Like a Bitly on your app. By default, RailsUrlShortener save the visits to your link for future interesting things that you may want to do.
Why give your data to a third party app if you can do it by yourself?
You can see a demo project of what you can do with this engine HERE.
A few of the things you can do with RailsUrlShortener:
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: "/"
And generate the short links like you want:
short_url("https://www.github.com/a-chacon/rails-url-shortener")
RailsUrlShortener::Url.generate("https://www.github.com/a-chacon/rails-url-shortener")
Then share the short link to your users or wherever you want.
Full params for the short_url helper:
short_url(url, owner: nil, key: nil, expires_at: nil, category: nil, url_options: {})
Where:
And the same for the generate model method except for url_options:
RailsUrlShortener::Url.generate(url, owner: nil, key: nil, expires_at: nil, category: nil)
By default, this engine save all request made on your short url, you can use that data for some analytics or simple IP logger. So for get the data in a controller or do wherever you want, you can use the Visit model related to an Url:
RailsUrlShortener::Url.find_by_key("key").visits # all visits
Or using the model class:
RailsUrlShortener::Visit.all # all in database
And a Visit is related to a Ipgeo model that contain information about the ip, so you can view this using the active record relation:
RailsUrlShortener::Visit.first.ipgeo # Ipgeo object that contain information of the ip
When a Visit record is created, a job is enqueue for get Ip data from this service and create the Ipgeo record. It is integrated to the free endpoint, so if you think that you have more than 45 different IPS querying in a minute to your app, we need to think in a new solution.
Add this line to your application's Gemfile:
gem "rails_url_shortener"
Or install it yourself as:
gem install rails_url_shortener
Then execute:
bundle
And finally install & run the migrations on your project and migrate:
bin/rails rails_url_shortener:install:migrations db:migrate
For the configurations generate the initializer whith this:
rails generate RailsUrlShortener:initializer
Here is important to configure the host at least if your are not running your app in localhost
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!
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)The gem is available as open source under the terms of the GPL-3.0 License.
by: a-chacon