Class: Podio::Middleware::OAuth2

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/podio/middleware/oauth2.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ OAuth2

Returns a new instance of OAuth2.



25
26
27
28
# File 'lib/podio/middleware/oauth2.rb', line 25

def initialize(app, options={})
  super(app)
  @podio_client = options[:podio_client]
end

Instance Method Details

#call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/podio/middleware/oauth2.rb', line 6

def call(env)
  orig_env = env.dup

  begin
    @app.call(env)
  rescue TokenExpired
    @podio_client.refresh_access_token

    # new access token needs to be put into the header
    orig_env[:request_headers].merge!(@podio_client.configured_headers)

    # rewind the body if this was a file upload
    orig_env[:body].rewind if orig_env[:body].respond_to?(:rewind)

    # redo the request with the new access token
    @app.call(orig_env)
  end
end