Sha256: 2e05e1f87736a775ce37a965ed0fb6bfb8b9ddecb3a4e0f2d8318fc6886b1df3

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require "addressable/uri"

module Berkshelf
  class SourceURI < Addressable::URI
    class << self
      # Returns a URI object based on the parsed string.
      #
      # @param [String, Addressable::URI, #to_str] uri
      #   The URI string to parse.
      #   No parsing is performed if the object is already an
      #   <code>Addressable::URI</code>.
      #
      # @raise [Berkshelf::InvalidSourceURI]
      #
      # @return [Berkshelf::SourceURI]
      def parse(uri)
        parsed_uri = super(uri)
        parsed_uri.send(:validate)
        parsed_uri
      rescue TypeError, ArgumentError => ex
        raise InvalidSourceURI.new(uri, ex)
      end
    end

    VALID_SCHEMES = %w{http https file}.freeze

    # @raise [Berkshelf::InvalidSourceURI]
    def validate
      super

      unless VALID_SCHEMES.include?(self.scheme)
        raise InvalidSourceURI.new(self, "invalid URI scheme '#{self.scheme}'. Valid schemes: #{VALID_SCHEMES}")
      end
    rescue Addressable::URI::InvalidURIError => ex
      raise InvalidSourceURI.new(self, ex)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
berkshelf-6.1.0 lib/berkshelf/source_uri.rb