Sha256: 6096aab3dd5acee3aa058875baa10840425f48d0aa35b89b79dbddb44a91621e

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

module Absolutely
  class URI
    attr_accessor :base, :relative

    def initialize(base:, relative:)
      raise ArgumentError, "base must be a String (given #{base.class.name})" unless base.is_a?(String)
      raise ArgumentError, "relative must be a String (given #{relative.class.name})" unless relative.is_a?(String)

      @base = base
      @relative = relative

      raise ArgumentError, 'base must be an absolute URI (e.g. https://example.com)' unless base_uri.absolute?

      relative_uri
    end

    def base_uri
      @base_uri ||= Addressable::URI.parse(base)
    rescue Addressable::URI::InvalidURIError => error
      raise InvalidURIError, error
    end

    def relative_uri
      @relative_uri ||= Addressable::URI.parse(relative)
    rescue Addressable::URI::InvalidURIError => error
      raise InvalidURIError, error
    end

    def to_absolute_uri
      return relative_uri.to_s if relative_uri.absolute?

      base_uri.path = '/' if base_uri.path.empty?

      (base_uri + relative_uri).to_s
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
absolutely-1.1.2 lib/absolutely/uri.rb
absolutely-1.1.1 lib/absolutely/uri.rb
absolutely-1.1.0 lib/absolutely/uri.rb