Sha256: 203e4a5d3d3cc174490c83632598d3b8bc89241655185de30fc007cbe5c9bfd5

Contents?: true

Size: 918 Bytes

Versions: 14

Compression:

Stored size: 918 Bytes

Contents

# frozen_string_literal: true

require 'http/accept'

module Salestation
  class Web < Module
    module InputValidators
      class AcceptHeader
        include Deterministic::Prelude

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

        def initialize(allowed_headers)
          @allowed_headers = allowed_headers
        end

        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
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
salestation-5.3.3 lib/salestation/web/input_validators/accept_header.rb
salestation-5.3.2 lib/salestation/web/input_validators/accept_header.rb
salestation-5.3.1 lib/salestation/web/input_validators/accept_header.rb
salestation-5.3.0 lib/salestation/web/input_validators/accept_header.rb
salestation-5.2.0 lib/salestation/web/input_validators/accept_header.rb
salestation-5.1.0 lib/salestation/web/input_validators/accept_header.rb
salestation-5.0.2 lib/salestation/web/input_validators/accept_header.rb
salestation-5.0.1 lib/salestation/web/input_validators/accept_header.rb
salestation-5.0.0 lib/salestation/web/input_validators/accept_header.rb
salestation-4.6.1 lib/salestation/web/input_validators/accept_header.rb
salestation-4.6.0 lib/salestation/web/input_validators/accept_header.rb
salestation-4.5.0 lib/salestation/web/input_validators/accept_header.rb
salestation-4.4.2 lib/salestation/web/input_validators/accept_header.rb
salestation-4.4.1 lib/salestation/web/input_validators/accept_header.rb