Class: Legion::Extensions::Github::Middleware::ScopeProbe

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/legion/extensions/github/middleware/scope_probe.rb

Constant Summary collapse

REPO_PATH_PATTERN =
%r{^/repos/([^/]+)/([^/]+)}

Instance Method Summary collapse

Constructor Details

#initialize(app, handler: nil) ⇒ ScopeProbe

Returns a new instance of ScopeProbe.



12
13
14
15
# File 'lib/legion/extensions/github/middleware/scope_probe.rb', line 12

def initialize(app, handler: nil)
  super(app)
  @handler = handler
end

Instance Method Details

#on_complete(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/github/middleware/scope_probe.rb', line 17

def on_complete(env)
  return unless @handler
  return unless env.url.path.match?(REPO_PATH_PATTERN)

  info = { status: env.status, url: env.url.to_s, path: env.url.path }

  if [403, 404].include?(env.status)
    @handler.on_scope_denied(info) if @handler.respond_to?(:on_scope_denied)
  elsif env.status >= 200 && env.status < 300
    @handler.on_scope_authorized(info) if @handler.respond_to?(:on_scope_authorized)
  end
end