Class: Legion::Extensions::Github::Helpers::CallbackServer

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/github/helpers/callback_server.rb

Constant Summary collapse

RESPONSE_HTML =
<<~HTML
  <html><body style="font-family:sans-serif;text-align:center;padding:40px;">
  <h2>GitHub authentication complete</h2><p>You can close this window.</p></body></html>
HTML

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCallbackServer

Returns a new instance of CallbackServer.



18
19
20
21
22
23
24
# File 'lib/legion/extensions/github/helpers/callback_server.rb', line 18

def initialize
  @server = nil
  @port = nil
  @result = nil
  @mutex = Mutex.new
  @cv = ConditionVariable.new
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



16
17
18
# File 'lib/legion/extensions/github/helpers/callback_server.rb', line 16

def port
  @port
end

Instance Method Details

#redirect_uriObject



48
49
50
# File 'lib/legion/extensions/github/helpers/callback_server.rb', line 48

def redirect_uri
  "http://127.0.0.1:#{@port}/callback"
end

#shutdownObject



39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/github/helpers/callback_server.rb', line 39

def shutdown
  @server&.close
rescue StandardError => _e
  nil
ensure
  @thread&.join(2)
  @thread&.kill
end

#startObject



26
27
28
29
30
# File 'lib/legion/extensions/github/helpers/callback_server.rb', line 26

def start
  @server = TCPServer.new('127.0.0.1', 0)
  @port = @server.addr[1]
  @thread = Thread.new { listen } # rubocop:disable ThreadSafety/NewThread
end

#wait_for_callback(timeout: 120) ⇒ Object



32
33
34
35
36
37
# File 'lib/legion/extensions/github/helpers/callback_server.rb', line 32

def wait_for_callback(timeout: 120)
  @mutex.synchronize do
    @cv.wait(@mutex, timeout) unless @result
    @result
  end
end