Class: Redd::Middleware

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

Overview

Rack middleware.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Middleware.

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create the object with

Options Hash (opts):

  • :user_agent (String)

    your app’s unique and descriptive user agent

  • :client_id (String)

    the client id of your app

  • :redirect_uri (String)

    the provided redirect URI

  • :secret (String) — default: ''

    the app secret (for the web type)

  • :scope (Array<String>) — default: ['identity']

    a list of scopes to request

  • :duration ('temporary', 'permanent') — default: 'permanent'

    the duration to request the code for.

  • :auto_refresh (Boolean) — default: true

    allow refreshing a permanent access automatically (only if duration is ‘permanent’)

  • :via (String) — default: '/auth/reddit'

    the relative path in the application that redirects a user to reddit



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/redd/middleware.rb', line 23

def initialize(app, opts = {})
  @app = app
  strategy_opts = opts.select { |k| %i(user_agent client_id secret redirect_uri).include?(k) }
  @strategy = Redd::AuthStrategies::Web.new(strategy_opts)

  @user_agent   = opts.fetch(:user_agent, "Redd:Web Application:v#{Redd::VERSION} (by unknown)")
  @client_id    = opts.fetch(:client_id)
  @redirect_uri = opts.fetch(:redirect_uri)
  @scope        = opts.fetch(:scope, ['identity'])
  @duration     = opts.fetch(:duration, 'permanent')
  @auto_refresh = opts.fetch(:auto_refresh, true) && @duration == 'permanent'
  @via          = opts.fetch(:via, '/auth/reddit')
end

Instance Method Details

#call(env) ⇒ Object



37
38
39
40
41
# File 'lib/redd/middleware.rb', line 37

def call(env)
  # This is done for thread safety so that each thread has its own copy
  # of the middleware logic.
  dup._call(env)
end