Sha256: b18a28c5dad92fdbda550e36cee5e31e058593beff37e3990929beac3c687c49

Contents?: true

Size: 958 Bytes

Versions: 5

Compression:

Stored size: 958 Bytes

Contents

# frozen_string_literal: true
module URLCanonicalize
  # Manage the URL into a URI with local exception handling
  class URI
    class << self
      def parse(url)
        uri = ::URI.parse decorate(url)
        uri if valid? uri
      rescue ::URI::InvalidURIError => e
        new_exception = URLCanonicalize::Exception::URI.new("#{e.class}: #{e.message}")
        new_exception.set_backtrace e.backtrace
        raise new_exception
      end

      private

      def valid?(uri)
        raise URLCanonicalize::Exception::URI, "#{uri} must be http or https" unless VALID_CLASSES.include?(uri.class)
        raise URLCanonicalize::Exception::URI, "Missing host name in #{uri}" unless uri.host
        true
      end

      def decorate(url)
        return url if url.include? COLON
        "http://#{url}" # Add protocol if we just receive a host name
      end

      VALID_CLASSES = [::URI::HTTP, ::URI::HTTPS].freeze
      COLON = ':'
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
url_canonicalize-0.1.15 lib/url_canonicalize/uri.rb
url_canonicalize-0.1.14 lib/url_canonicalize/uri.rb
url_canonicalize-0.1.13 lib/url_canonicalize/uri.rb
url_canonicalize-0.1.12 lib/url_canonicalize/uri.rb
url_canonicalize-0.1.11 lib/url_canonicalize/uri.rb