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

#advanced?, #alias?, #choices, #default, #help_message_for_default, #help_messages, #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_protocolsArray<String>



13
14
15
# File 'lib/opt_parse_validator/opts/uri.rb', line 13

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

#append_help_messagesObject

return [ Void ]



5
6
7
8
9
10
# File 'lib/opt_parse_validator/opts/uri.rb', line 5

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_protocolObject

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



18
19
20
# File 'lib/opt_parse_validator/opts/uri.rb', line 18

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

#validate(value) ⇒ String



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/opt_parse_validator/opts/uri.rb', line 25

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