class Escher::RackMiddleware

Constants

VERSION

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/escher/rack_middleware.rb, line 19
def initialize(app, options = {})
  @app = app
  @options = options
end

Protected Class Methods

config(&block) click to toggle source
# File lib/escher/rack_middleware.rb, line 41
def self.config(&block)
  block.call(self)
end

Public Instance Methods

call(request_env) click to toggle source
# File lib/escher/rack_middleware.rb, line 24
def call(request_env)
  if authorize_path?(::Rack::Utils.clean_path_info(request_env[::Rack::PATH_INFO]))
    return unauthorized_response unless authorized?(request_env)
  end

  @app.call(request_env)
end

Protected Instance Methods

authorize_path?(path) click to toggle source
# File lib/escher/rack_middleware.rb, line 45
def authorize_path?(path)
  case true

  when paths_of(:included_paths, include: path)
    true

  when paths_of(:excluded_paths, include: path)
    false

  else
    true

  end
end
final_options() click to toggle source
# File lib/escher/rack_middleware.rb, line 71
def final_options
  @final_options ||= default_options.merge(@options)
end
paths_of(option_key, h) click to toggle source
# File lib/escher/rack_middleware.rb, line 60
def paths_of(option_key, h)
  path = h[:include]
  final_options[option_key].any? do |matcher|
    if matcher.is_a?(Regexp)
      !!(path =~ matcher)
    else
      path == matcher.to_s
    end
  end
end
unauthorized_response() click to toggle source
# File lib/escher/rack_middleware.rb, line 34
def unauthorized_response
  response = Rack::Response.new
  response.write 'Unauthorized'
  response.status = 401
  response.finish
end