Class: BerkeleyLibrary::Util::URIs::Appender

Inherits:
Object
  • Object
show all
Defined in:
lib/berkeley_library/util/uris/appender.rb

Overview

Appends the specified paths to the path of the specified URI, removing any extraneous slashes, and builds a new URI with that path and the same scheme, host, query, fragment, etc. as the original.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, *elements) ⇒ Appender

Creates and invokes a new BerkeleyLibrary::Util::URIs::Appender.

Parameters:

  • uri (URI, String)

    the original URI

  • elements (Array<String, Symbol>)

    the URI elements to join.

Raises:

  • URI::InvalidComponentError if appending the specified elements would create an invalid URI



20
21
22
23
24
25
26
27
# File 'lib/berkeley_library/util/uris/appender.rb', line 20

def initialize(uri, *elements)
  raise ArgumentError, 'uri cannot be nil' unless (@original_uri = URIs.uri_or_nil(uri))

  @elements = elements.map(&:to_s)
  @elements.each_with_index do |element, elem_index|
    handle_element(element, elem_index)
  end
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



13
14
15
# File 'lib/berkeley_library/util/uris/appender.rb', line 13

def elements
  @elements
end

#original_uriObject (readonly)

Returns the value of attribute original_uri.



13
14
15
# File 'lib/berkeley_library/util/uris/appender.rb', line 13

def original_uri
  @original_uri
end

Instance Method Details

#to_uriURI

Returns the new URI.

Returns:

  • (URI)

    a new URI appending the joined path elements.

Raises:

  • URI::InvalidComponentError if appending the specified elements would create an invalid URI



33
34
35
36
37
38
39
40
# File 'lib/berkeley_library/util/uris/appender.rb', line 33

def to_uri
  original_uri.dup.tap do |new_uri|
    new_path = Paths.join(new_uri.path, *path_elements)
    new_uri.path = Paths.ensure_abs(new_path)
    new_uri.query = query unless query_elements.empty?
    new_uri.fragment = fragment unless fragment_elements.empty?
  end
end

#to_url_strString

Returns the new URI as a string.

Returns:

  • (String)

    a new URI appending the joined path elements, as a string.

Raises:

  • URI::InvalidComponentError if appending the specified elements would create an invalid URI



46
47
48
# File 'lib/berkeley_library/util/uris/appender.rb', line 46

def to_url_str
  to_uri.to_s
end