
Security News
CISA’s 2025 SBOM Guidance Adds Hashes, Licenses, Tool Metadata, and Context
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
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
# New configuration added in PR #88
config.enable_fallback_mode = false # When true, falls back to Firebase URL if a short link is not found
config.firebase_host = "https://example.app.goo.gl" # Firebase host URL for fallbacks
end
This project supports two development environment options: GitHub Codespaces and local Docker Compose.
This project is configured to work with GitHub development containers, providing a consistent development environment.
Once the development container is created and set up:
cd test/dummy && bin/rails test
cd test/dummy && bin/rails server
For local development, we use Docker Compose with VS Code's Remote - Containers extension.
For more details on the Docker Compose setup, refer to the Docker Compose documentation. 4. Access the application at the forwarded port (usually port 3000)
To shorten a URL, simply call:
shortened_url = DynamicLinks.shorten_url("https://example.com")
To find an existing short link for a URL:
short_link_data = DynamicLinks.find_short_link("https://example.com", client)
if short_link_data
puts short_link_data[:short_url] # e.g., "https://client.com/abc123"
puts short_link_data[:full_url] # e.g., "https://example.com"
else
puts "No existing short link found"
end
DynamicLinks provides a REST API for URL shortening operations when enable_rest_api
is set to true
in the configuration.
All API endpoints require an api_key
parameter that corresponds to a registered client.
Creates a new short link for a URL.
Endpoint: POST /v1/shortLinks
Parameters:
url
(required): The URL to shortenapi_key
(required): Client API keyExample Request:
curl -X POST "http://localhost:3000/v1/shortLinks" \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com/long-url",
"api_key": "your-api-key"
}'
Example Response:
{
"shortLink": "https://your-domain.com/abc123",
"previewLink": "https://your-domain.com/abc123?preview=true",
"warning": []
}
Finds an existing short link for a URL, or creates a new one if none exists. This prevents duplicate short links for the same URL and client.
Endpoint: POST /v1/shortLinks/findOrCreate
Parameters:
url
(required): The URL to find or shortenapi_key
(required): Client API keyExample Request:
curl -X POST "http://localhost:3000/v1/shortLinks/findOrCreate" \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com/long-url",
"api_key": "your-api-key"
}'
Example Response (existing link found):
{
"shortLink": "https://your-domain.com/abc123",
"previewLink": "https://your-domain.com/abc123?preview=true",
"warning": []
}
Example Response (new link created):
{
"shortLink": "https://your-domain.com/def456",
"previewLink": "https://your-domain.com/def456?preview=true",
"warning": []
}
Retrieves the original URL from a short link.
Endpoint: GET /v1/shortLinks/{short_url}
Parameters:
short_url
(in URL): The short URL code to expandapi_key
(required): Client API keyExample Request:
curl "http://localhost:3000/v1/shortLinks/abc123?api_key=your-api-key"
Example Response:
{
"url": "https://example.com/long-url"
}
The API returns appropriate HTTP status codes and error messages:
400 Bad Request
: Invalid URL format401 Unauthorized
: Invalid or missing API key403 Forbidden
: REST API feature is disabled404 Not Found
: Short link not found (expand endpoint)500 Internal Server Error
: Server errorExample Error Response:
{
"error": "Invalid URL"
}
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.
NanoIdStrategy
, add gem 'nanoid', '~> 2.0'
to your Gemfile.RedisCounterStrategy
, ensure Redis is available and configured. Redis strategy requires connection_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
Benchmarking scripts are available in the benchmarks/
directory to measure performance:
ruby_api.rb
: Benchmarks Ruby API URL shortening performancerest_api.py
: Benchmarks REST API URL shortening performancecreate_or_find.rb
: Compares performance of different create_or_find
methodsYou can run these benchmarks to measure performance in your specific environment.
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.
FAQs
Unknown package
We found that dynamic_links demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.