Class: OptParseValidator::OptURI

Inherits:
OptBase
  • Object
show all
Defined in:
lib/opt_parse_validator/opts/uri.rb

Overview

Implementation of the URI Option

Direct Known Subclasses

OptProxy, OptURL

Instance Attribute Summary

Attributes inherited from OptBase

#attrs, #option, #required

Instance Method Summary collapse

Methods inherited from OptBase

#choices, #default, #initialize, #normalize, #required?, #required_unless, #to_long, #to_s, #to_sym, #value_if_empty

Constructor Details

This class inherits a constructor from OptParseValidator::OptBase

Instance Method Details

#allowed_protocolsObject



4
5
6
# File 'lib/opt_parse_validator/opts/uri.rb', line 4

def allowed_protocols
  @allowed_protocols ||= [*attrs[:protocols]]
end

#default_protocolObject

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



9
10
11
# File 'lib/opt_parse_validator/opts/uri.rb', line 9

def default_protocol
  @default_protocol ||= attrs[:default_protocol]
end

#validate(value) ⇒ String

Parameters:

  • value (String)

Returns:

  • (String)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/opt_parse_validator/opts/uri.rb', line 16

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