Sha256: 03c856ed67005256832e22c406eafcc4fd58e0f129e1337462e7a1e1579f9eaf

Contents?: true

Size: 1.13 KB

Versions: 11

Compression:

Stored size: 1.13 KB

Contents

module OptParseValidator
  # Implementation of the URI Option
  class OptURI < OptBase
    # return [ Void ]
    def append_help_messages
      option << "Allowed Protocols: #{allowed_protocols.join(', ')}" unless allowed_protocols.empty?
      option << "Default Protocol if none provided: #{default_protocol}" if default_protocol

      super
    end

    # @return [ Array<String> ]
    def allowed_protocols
      @allowed_protocols ||= [*attrs[:protocols]]
    end

    # The default protocol (or scheme) to use if none was given
    def default_protocol
      @default_protocol ||= attrs[:default_protocol]
    end

    # @param [ String ] value
    #
    # @return [ String ]
    def validate(value)
      uri = Addressable::URI.parse(value)

      uri = Addressable::URI.parse("#{default_protocol}://#{value}") if !uri.scheme && default_protocol

      unless allowed_protocols.empty? || allowed_protocols.include?(uri.scheme)
        # For future refs: will have to check if the uri.scheme exists,
        # otherwise it means that the value was empty
        raise Addressable::URI::InvalidURIError
      end

      uri.to_s
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
opt_parse_validator-0.0.17.0 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.16.6 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.16.5 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.16.4 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.16.3 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.16.2 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.16.1 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.16.0 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.15.2 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.15.1 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.15.0 lib/opt_parse_validator/opts/uri.rb