Class: Salestation::Web::InputValidators::ContentTypeHeader

Inherits:
Object
  • Object
show all
Includes:
Deterministic::Prelude
Defined in:
lib/salestation/web/input_validators/content_type_header.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allowed_headers) ⇒ ContentTypeHeader

Returns a new instance of ContentTypeHeader.



13
14
15
# File 'lib/salestation/web/input_validators/content_type_header.rb', line 13

def initialize(allowed_headers)
  @allowed_headers = allowed_headers
end

Class Method Details

.[](*allowed_headers) ⇒ Object



9
10
11
# File 'lib/salestation/web/input_validators/content_type_header.rb', line 9

def self.[](*allowed_headers)
  new(allowed_headers)
end

Instance Method Details

#call(header_value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/salestation/web/input_validators/content_type_header.rb', line 17

def call(header_value)
  # Some headers can have additional information such as `multipart/form-data`
  parsed_header_value = header_value ? header_value.split(';').first : nil

  header_valid = @allowed_headers.empty? || @allowed_headers.include?(parsed_header_value)

  if header_valid
    Success(nil)
  else
    Failure(App::Errors::UnsupportedMediaType.new(
      message: "Unsupported Content-Type Header '#{header_value}'",
      debug_message: "Available Content-Type Headers are #{@allowed_headers.join(', ')}"
    ))
  end
end