Class: Ak4r::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/ak4r/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, config = {}) ⇒ Middleware

Options

  • :salt - Salt to generate API keys.

  • :header_key - A way to override the header’s name used to store the API key.

    The value given here should reflect how Rack interprets the
    header. For example if the client passes "X-API-KEY" Rack
    transforms interprets it as "HTTP_X_API_KEY". The default
    value is "HTTP_X_API_KEY".
    
  • :url_restriction - A way to restrict specific URLs that should pass through

    the rack-api-key middleware. In order to use pass an Array of Regex patterns.
    If left unspecified all requests will pass through the rack-api-key
    middleware.
    
  • :url_exclusion - A way to exclude specific URLs that should not pass through the

    the rack-api-middleware. In order to use, pass an Array of Regex patterns.
    

Example

use Ak4r,
    :salt => "API_KEY_SALT"
    :header_key => "HTTP_X_API_KEY",
    :url_restriction => [/api/],
    :url_exclusion => [/api\/status/]


34
35
36
37
# File 'lib/ak4r/middleware.rb', line 34

def initialize(app, config = {})
  @app = app
  Ak4r.config.update config
end

Instance Method Details

#call(env) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/ak4r/middleware.rb', line 39

def call(env)
  if constraint?(:url_exclusion) && url_matches(:url_exclusion, env)
    @app.call(env)
  elsif constraint?(:url_restriction)
    url_matches(:url_restriction, env) ? process_request(env) : @app.call(env)
  else
    process_request(env)
  end
end