Class: BerkeleyLibrary::Util::URIs::Appender
- Inherits:
-
Object
- Object
- BerkeleyLibrary::Util::URIs::Appender
- 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
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
-
#original_uri ⇒ Object
readonly
Returns the value of attribute original_uri.
Instance Method Summary collapse
-
#initialize(uri, *elements) ⇒ Appender
constructor
Creates and invokes a new Appender.
-
#to_uri ⇒ URI
Returns the new URI.
-
#to_url_str ⇒ String
Returns the new URI as a string.
Constructor Details
#initialize(uri, *elements) ⇒ Appender
Creates and invokes a new BerkeleyLibrary::Util::URIs::Appender.
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
#elements ⇒ Object (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_uri ⇒ Object (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_uri ⇒ URI
Returns the new 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_str ⇒ String
Returns the new URI as a string.
46 47 48 |
# File 'lib/berkeley_library/util/uris/appender.rb', line 46 def to_url_str to_uri.to_s end |