A Ruby gem for converting relative paths to absolute URIs.
- Uses the Addressable Ruby gem for improved URI parsing.
- Supports Ruby 2.6 and newer.
Before installing and using Absolutely, you'll want to have Ruby 2.6 (or newer) installed. It's recommended that you use a Ruby version managment tool like rbenv, chruby, or rvm.
Absolutely is developed using Ruby 2.6.10 and is additionally tested against Ruby 2.7, 3.0, and 3.1 using GitHub Actions.
If you're using Bundler, add Absolutely to your project's
source 'https://rubygems.org' gem 'absolutely'
…and hop over to your command prompt and run…
$ bundle install
With Absolutely added to your project's
Gemfile and installed, you may convert relative URIs to absolute URIs by doing:
require 'absolutely' uri = Absolutely.uri(base: 'https://example.com', relative: '/foo').to_abs puts uri # => String: 'https://example.com/foo'
This example combines the supplied
base value (
https://example.com) and combines it with the supplied
relative value (
/foo), returning the string
You may obtain the same results using this slightly shorter version:
require 'absolutely' uri = Absolutely.to_abs(base: 'https://example.com', relative: '/foo') puts uri # => 'https://example.com/foo'
Note that if the value passed as
relative is determined to be an absolute URI, Absolutely will return the value of
relative regardless of the value passed as
require 'absolutely' uri = Absolutely.to_abs(base: 'https://example.com', relative: 'https://example.com/foo') puts uri # => 'https://example.com/foo'
Should the need arise, you may work directly with the
require 'absolutely' uri = Absolutely::URI.new(base: 'https://example.com', relative: '/foo') puts uri # => #<Absolutely::URI> puts uri.base # => 'https://example.com' puts uri.relative # => '/foo' puts uri.base_uri # => #<Addressable::URI URI:https://example.com> puts uri.relative_uri # => #<Addressable::URI URI:/foo> puts uri.to_abs # => 'https://example.com/foo'
For convenience, the
relative_uri methods return instances of the
Addressable::URI class. For more on this class' available methods, see the Addressable Ruby gem's source code.
Interested in helping improve Absolutely? Awesome! Your help is greatly appreciated. See CONTRIBUTING.md for details.
Absolutely is written and maintained by Jason Garber.
Absolutely is freely available under the MIT License. Use it, learn from it, fork it, improve it, change it, tailor it to your needs.