class OptParseValidator::OptURI

Implementation of the URI Option

Public Instance Methods

allowed_protocols() click to toggle source

@return [ Array<String> ]

# File lib/opt_parse_validator/opts/uri.rb, line 15
def allowed_protocols
  @allowed_protocols ||= Array(attrs[:protocols]).map(&:downcase)
end
append_help_messages() click to toggle source

return [ Void ]

# File lib/opt_parse_validator/opts/uri.rb, line 7
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
default_protocol() click to toggle source

The default protocol (or scheme) to use if none was given

# File lib/opt_parse_validator/opts/uri.rb, line 20
def default_protocol
  @default_protocol ||= attrs[:default_protocol]&.downcase
end
validate(value) click to toggle source

@param [ String ] value

@return [ String ]

# File lib/opt_parse_validator/opts/uri.rb, line 27
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&.downcase)
    # 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