Class: Salestation::Web::InputValidators::AcceptHeader

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allowed_headers) ⇒ AcceptHeader

Returns a new instance of AcceptHeader.



15
16
17
# File 'lib/salestation/web/input_validators/accept_header.rb', line 15

def initialize(allowed_headers)
  @allowed_headers = allowed_headers
end

Class Method Details

.[](*allowed_headers) ⇒ Object



11
12
13
# File 'lib/salestation/web/input_validators/accept_header.rb', line 11

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

Instance Method Details

#call(header_value) ⇒ Object



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

def call(header_value)
  return Success(nil) if @allowed_headers.empty?

  mime_types = HTTP::Accept::MediaTypes.parse(header_value.to_s).map(&:mime_type)

  if (@allowed_headers & mime_types).any?
    Success(nil)
  else
    Failure(App::Errors::NotAcceptable.new(
      message: "Unsupported Accept Header '#{header_value}'",
      debug_message: "Available Accept Headers are #{@allowed_headers.join(', ')}"
    ))
  end
end