Sha256: 21ce2229376f7084078fb20e4506a447df49eb4954424954f24ececa01c82131

Contents?: true

Size: 1.87 KB

Versions: 22

Compression:

Stored size: 1.87 KB

Contents

require 'interpol'
require 'interpol/dynamic_struct'

module Interpol
  # Validates and parses a request body according to the endpoint
  # schema definitions.
  class RequestBodyValidator
    def initialize(app, &block)
      @config = Configuration.default.customized_duplicate(&block)
      @app = app
    end

    def call(env)
      if @config.validate_request?(env)
        handler = Handler.new(env, @config)

        handler.validate do |error_response|
          return error_response
        end

        env['interpol.parsed_body'] = handler.parse
      end

      @app.call(env)
    end

    # Handles request body validation for a single request.
    class Handler
      attr_reader :env, :config

      def initialize(env, config)
        @env = env
        @config = config
      end

      def parse
        DynamicStruct.new(parsed_body)
      end

      def validate(&block)
        endpoint_definition(&block).validate_data!(parsed_body)
      rescue Interpol::ValidationError => e
        yield @config.request_body_invalid(env, e)
      end

    private

      def request_method
        env.fetch('REQUEST_METHOD')
      end

      def path
        env.fetch('PATH_INFO')
      end

      def parsed_body
        @parsed_body ||= JSON.parse(unparsed_body)
      end

      def unparsed_body
        @unparsed_body ||= begin
          input = env.fetch('rack.input')
          input.read.tap { input.rewind }
        end
      end

      def endpoint_definition(&block)
        config.endpoints.find_definition(request_method, path, 'request', nil) do |endpoint|
          available = endpoint.available_request_versions

          @config.request_version_for(env, endpoint).tap do |requested|
            unless available.include?(requested)
              yield @config.request_version_unavailable(env, requested, available)
            end
          end
        end
      end

    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
interpol-0.11.0 lib/interpol/request_body_validator.rb
interpol-0.10.7 lib/interpol/request_body_validator.rb
interpol-0.10.6 lib/interpol/request_body_validator.rb
interpol-0.10.5 lib/interpol/request_body_validator.rb
interpol-0.10.4 lib/interpol/request_body_validator.rb
interpol-0.10.3 lib/interpol/request_body_validator.rb
interpol-0.10.2 lib/interpol/request_body_validator.rb
interpol-0.10.1 lib/interpol/request_body_validator.rb
interpol-0.10.0 lib/interpol/request_body_validator.rb
interpol-0.9.0 lib/interpol/request_body_validator.rb
interpol-0.8.1 lib/interpol/request_body_validator.rb
interpol-0.8.0 lib/interpol/request_body_validator.rb
interpol-0.7.3 lib/interpol/request_body_validator.rb
interpol-0.7.2 lib/interpol/request_body_validator.rb
interpol-0.7.1 lib/interpol/request_body_validator.rb
interpol-0.7.0 lib/interpol/request_body_validator.rb
interpol-0.6.0 lib/interpol/request_body_validator.rb
interpol-0.5.0 lib/interpol/request_body_validator.rb
interpol-0.4.3 lib/interpol/request_body_validator.rb
interpol-0.4.2 lib/interpol/request_body_validator.rb