Class: Stagehand::Rack::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
# File 'lib/stagehand/rack/middleware.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stagehand/rack/middleware.rb', line 7

def call(env)
  request = env['stagehand'] = Rack::Request.new(env)

  response = catch :stagehand do
    if request.path == '/callback'
      token_response = HTTParty.post(Stagehand.access_token_url, :body => {
                                 :client_id => Stagehand.config.client_id, 
                                 :client_secret => Stagehand.config.client_secret, 
                                 :redirect_uri => Stagehand.redirect_uri, 
                                 :code => request.params['code'],
                                 :grant_type => 'authorization_code'}
                               )
      env['rack.session'][:access_token] = token_response["access_token"]
      # redirect to root
      [302, {'Location'=>'/'}, []]
    else
      @app.call(env)
    end
  end
rescue InvalidRequest => e
  [400, {}, e.message]
end