Sha256: a66c0edc7a2dc9226381b5f4a53e38008fb1b8019ab113fccfab1bae778a87dc

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

require 'tomograph'
require 'esplanade/request'
require 'esplanade/response'

module Esplanade
  class SafeMiddleware
    def initialize(app, **params)
      @app = app
      @documentation = Tomograph::Tomogram.new(Esplanade.configuration.params.merge(params))
    end

    def call(env)
      request = Esplanade::Request.new(@documentation, env)
      check_request(request)

      status, headers, body = @app.call(env)

      response = Esplanade::Response.new(request, status, body)
      check_response(response)

      [status, headers, body]
    end

    def check_request(request)
      request.validation.valid!
      Rails.logger.debug 'ESPLANADE SAYS THAT THE REQUEST IS VALID'
    rescue Esplanade::Request::Error => e
      Rails.logger.debug "ESPLANADE SKIP: #{e.inspect}"
    end

    def check_response(response)
      response.validation.valid!
      Rails.logger.debug 'ESPLANADE SAYS THAT THE RESPONSE IS VALID'
    rescue Esplanade::Response::Error => e
      Rails.logger.debug "ESPLANADE SKIP: #{e.inspect}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
esplanade-1.8.2 lib/esplanade/middlewares/safe_middleware.rb
esplanade-1.8.1 lib/esplanade/middlewares/safe_middleware.rb
esplanade-1.8.0 lib/esplanade/middlewares/safe_middleware.rb
esplanade-1.7.1 lib/esplanade/middlewares/safe_middleware.rb
esplanade-1.7.0 lib/esplanade/middlewares/safe_middleware.rb