Absolutely

A Ruby gem for converting relative paths to absolute URIs.

Gem Downloads Build Dependencies Maintainability Coverage

Key Features

  • Uses the Addressable Ruby gem for improved URI parsing.
  • Supports Ruby 2.4 and newer.

Getting Started

Before installing and using Absolutely, you'll want to have Ruby 2.4 (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.4.6 and is additionally tested against Ruby 2.5.5 and 2.6.2 using Travis CI.

Installation

If you're using Bundler, add Absolutely to your project's Gemfile:

source 'https://rubygems.org'

gem 'absolutely'

…and hop over to your command prompt and run…

$ bundle install

Usage

Basic Usage

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_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 https://example.com/foo.

You may obtain the same results using this slightly shorter version:

require 'absolutely'

uri = Absolutely.to_absolute_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 base:

require 'absolutely'

uri = Absolute.to_absolute_uri(base: 'https://example.com', relative: 'https://example.com/foo')

puts uri # returns String: 'https://example.com/foo'

Advanced Usage

Should the need arise, you may work directly with the Absolutely::URI class:

require 'absolutely'

uri = Absolutely::URI.new(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'

The base_uri and 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.

Contributing

Interested in helping improve Absolutely? Awesome! Your help is greatly appreciated. See CONTRIBUTING.md for details.

Acknowledgments

Absolutely is written and maintained by Jason Garber.

License

Absolutely is freely available under the MIT License. Use it, learn from it, fork it, improve it, change it, tailor it to your needs.