Class: OptParseValidator::OptHeaders

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

Overview

Implementation of the Headers Option

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

#append_help_messagesVoid

Returns:

  • (Void)


7
8
9
10
11
12
# File 'lib/opt_parse_validator/opts/headers.rb', line 7

def append_help_messages
  super

  option << "Separator to use between the headers: '; '"
  option << "Examples: 'X-Forwarded-For: 127.0.0.1', 'X-Forwarded-For: 127.0.0.1; Another: aaa'"
end

#validate(value) ⇒ Hash

Returns The parsed headers in a hash, with { ‘key’ => ‘value’ } format.

Parameters:

  • value (String)

Returns:

  • (Hash)

    The parsed headers in a hash, with { ‘key’ => ‘value’ } format



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

def validate(value)
  values = super(value).chomp(';').split('; ')

  headers = {}

  values.each do |header|
    raise Error, "Malformed header: '#{header}'" unless header.index(':')

    val = header.split(':', 2)

    headers[val[0].strip] = val[1].strip
  end

  headers
end