Sha256: af8c1c15313945532939bf177c93713aaec40bc4ad1aa7cf36f036b3821e92a3

Contents?: true

Size: 1.13 KB

Versions: 10

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
    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)

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

      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
        fail Addressable::URI::InvalidURIError
      end

      uri.to_s
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
opt_parse_validator-0.0.13.8 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.13.7 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.13.6 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.13.5 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.13.4 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.13.3 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.13.2 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.13 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.12.1 lib/opt_parse_validator/opts/uri.rb
opt_parse_validator-0.0.12 lib/opt_parse_validator/opts/uri.rb