Files
cookbook-openstack-common/libraries/uri.rb
Lance Albertson c354f2a406 Workaround issue with >= 16.5 when requiring addressable/uri
A change made in 16.5 causes an issue when trying to require `addressable/uri`.
This has been filed upstream with this issue [1]. As a workaround, it seems
using `autoload` fixes the problem.

[1] https://github.com/chef/chef/issues/10452

Change-Id: I8cb5de8aed76f3530e4b4b475024e554c8451790
Depends-On: https://review.opendev.org/753415
Signed-off-by: Lance Albertson <lance@osuosl.org>
2020-09-22 10:38:00 -07:00

56 lines
2.0 KiB
Ruby

# encoding: UTF-8
#
# Cookbook:: openstack-common
# library:: uri
#
# Copyright:: 2012-2013, AT&T Services, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'uri'
# TODO(ramereth): Workaround for this issue https://github.com/chef/chef/issues/10452
# require 'addressable/uri'
autoload :Addressable, 'addressable/uri'
# URI methods
module ::Openstack
# Returns a uri::URI from a hash. If the hash has a 'uri' key, the value
# of that is returned. If not, then the routine attempts to construct
# the URI from other parts of the hash. The values of the 'port' and 'path'
# keys are used directly from the hash.
def uri_from_hash(hash)
if hash['uri']
::URI.parse Addressable::URI.encode(hash['uri'])
else
host = hash['host']
scheme = hash['scheme'] ? hash['scheme'] : 'http'
port = hash['port'] # Returns nil if missing, which is fine.
path = hash['path'] # Returns nil if missing, which is fine.
::URI::Generic.new scheme, nil, host, port, nil, path, nil, nil, nil
end
end
# Helper for joining URI paths. The standard URI::join method is not
# intended for joining URI relative path segments. This function merely
# helps to accurately join supplied paths.
def uri_join_paths(*paths)
return nil if paths.empty?
leadingslash = paths[0][0] == '/' ? '/' : ''
trailingslash = paths[-1][-1] == '/' ? '/' : ''
paths.map! { |path| path.sub(%r{^\/+}, '').sub(%r{\/+$}, '') }
leadingslash + paths.join('/') + trailingslash
end
end