A Ruby gem for converting relative paths to absolute URIs.
- Uses the Addressable Ruby gem for improved URI parsing.
- Supports Ruby 2.4 and newer.
Absolutely is developed using Ruby 2.4.4 and is additionally tested against Ruby 2.5.1 using Travis CI.
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 = .(base: 'https://example.com', relative: '/foo').to_absolute_uri puts uri # returns 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 = .(base: 'https://example.com', relative: '/foo') puts uri # returns String: '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 = Absolute.to_absolute_uri(base: 'https://example.com', relative: 'https://example.com/foo') puts uri # returns String: 'https://example.com/foo'
Should the need arise, you may work directly with the
require 'absolutely' uri = ::.(base: 'https://example.com', relative: '/foo') puts uri # returns Absolutely::URI puts uri.base # returns String: 'https://example.com' puts uri.relative # returns String: '/foo' puts uri.base_uri # returns Addressable::URI puts uri.relative_uri # returns Addressable::URI puts uri.to_absolute_uri # returns String: 'https://example.com/foo'
relative_uri methods return instances of
Addressable::URI for convenience. 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.