DynamicLinks is a flexible URL shortening Ruby gem, designed to provide various strategies for URL shortening, similar to Firebase Dynamic Links.
By default, encoding strategies such as MD5 will generate the same short URL for the same input URL. This behavior ensures consistency and prevents the creation of multiple records for identical URLs. For scenarios requiring unique short URLs for each request, strategies like RedisCounterStrategy can be used, which generate a new short URL every time, regardless of the input URL.
To use DynamicLinks, you need to configure the shortening strategy and other settings in an initializer or before you start shortening URLs.
In your Rails initializer or similar setup code, configure DynamicLinks like this:
DynamicLinks.configure do |config|
config.shortening_strategy = :MD5 # Default strategy
config.redis_config = { host: 'localhost', port: 6379 } # Redis configuration
config.redis_pool_size = 10 # Redis connection pool size
config.redis_pool_timeout = 3 # Redis connection pool timeout in seconds
config.enable_rest_api = true # Enable or disable REST API feature
end
To shorten a URL, simply call:
shortened_url = DynamicLinks.shorten_url("https://example.com")
DynamicLinks supports various shortening strategies. The default strategy is MD5
, but you can choose among several others, including NanoIdStrategy
, RedisCounterStrategy
, Sha256Strategy
, and more.
Depending on the strategy you choose, you may need to install additional dependencies.
- For
NanoIdStrategy
, addgem 'nanoid', '~> 2.0'
to your Gemfile. - For
RedisCounterStrategy
, ensure Redis is available and configured. Redis strategy requiresconnection_pool
gem too.
Ensure you bundle these dependencies along with the DynamicLinks gem if you plan to use these strategies.
Add this line to your application's Gemfile:
gem "dynamic_links"
And then execute:
$ bundle
Or install it yourself as:
$ gem install dynamic_links
Shorten an URL using Ruby: Shorten an URL using API:
rails db:setup
rails db:test:prepare
rails test
export CITUS_ENABLED=true
rails db:setup
rails db:test:prepare
rails test
Note: Make sure the Citus extension already enabled on the installed PostgreSQL We don't manage it on Rails.
The gem is available as open source under the terms of the MIT License.