Class: Shellify::OauthCallbackHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/shellify/oauth_callback_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ OauthCallbackHandler

Returns a new instance of OauthCallbackHandler.



13
14
15
# File 'lib/shellify/oauth_callback_handler.rb', line 13

def initialize(config)
  @config = config
end

Class Method Details

.runObject



9
10
11
# File 'lib/shellify/oauth_callback_handler.rb', line 9

def self.run(...)
  new(...).run
end

Instance Method Details

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shellify/oauth_callback_handler.rb', line 17

def run
  @server = TCPServer.open(8888)
  @client = @server.accept

  path = @client.gets.split[1]
  params = CGI.parse(path.split('?').last).transform_values(&:first)
  body = 'Success! (You can close this now)'

  begin
    tokens = fetch_tokens(params['code'])
  rescue RestClient::Exception => e
    body = "Spotify didn't like that\n#{e.response}"
  end

  @client.puts headers(body.length)
  @client.puts body
  tokens
ensure
  @client.close if @client
  @server.close
end