Sha256: 295b58785376d63da8fd8e16e33bcb14dc3c954a4e580de4389ea70dc5b82134

Contents?: true

Size: 988 Bytes

Versions: 1

Compression:

Stored size: 988 Bytes

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 + relative_uri).to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
absolutely-1.0.0 lib/absolutely/uri.rb