Sha256: 1b9702719b36388d9895f038a01fd95d5b8c3ae261826cd1d846d688d1e64592

Contents?: true

Size: 953 Bytes

Versions: 6

Compression:

Stored size: 953 Bytes

Contents

require 'uri'

module BerkeleyLibrary
  module Util
    module URIs
      module Validator
        class << self

          # Returns the specified URL as a URI, or `nil` if the URI is `nil`.
          # @param url [String, URI, nil] the URL.
          # @return [URI] the URI, or `nil`.
          # @raise [URI::InvalidURIError] if `url` is not `nil` and cannot be
          #   parsed as a URI.
          def uri_or_nil(url)
            return unless url

            # noinspection RubyMismatchedReturnType
            url.is_a?(URI) ? url : URI.parse(url.to_s)
          end

          # Returns the specified URL as a string.
          # @param url [String, URI] the URL.
          # @return [String] the URL.
          # @raise [URI::InvalidURIError] if `url` cannot be parsed as a URI.
          def url_str_or_nil(url)
            uri = Validator.uri_or_nil(url)
            uri.to_s if uri
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
berkeley_library-util-0.1.9 lib/berkeley_library/util/uris/validator.rb
berkeley_library-util-0.1.8 lib/berkeley_library/util/uris/validator.rb
berkeley_library-util-0.1.7 lib/berkeley_library/util/uris/validator.rb
berkeley_library-util-0.1.6 lib/berkeley_library/util/uris/validator.rb
berkeley_library-util-0.1.5 lib/berkeley_library/util/uris/validator.rb
berkeley_library-util-0.1.4 lib/berkeley_library/util/uris/validator.rb