class Shellify::OauthCallbackHandler
Public Class Methods
new(config)
click to toggle source
# File lib/shellify/oauth_callback_handler.rb, line 13 def initialize(config) @config = config end
run(...)
click to toggle source
# File lib/shellify/oauth_callback_handler.rb, line 9 def self.run(...) new(...).run end
Public Instance Methods
run()
click to toggle source
# 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
Private Instance Methods
fetch_tokens(code)
click to toggle source
# File lib/shellify/oauth_callback_handler.rb, line 52 def fetch_tokens(code) headers = { 'Authorization': "Basic " + Base64.strict_encode64("#{@config.client_id}:#{@config.client_secret}"), } params = { client_id: @config.client_id, scope: Shellify::Config::SPOTIFY_AUTHORIZATION_SCOPES, redirect_uri: 'http://localhost:8888/callback', grant_type: 'authorization_code', code: code, } JSON.parse(RestClient.post("https://accounts.spotify.com/api/token", params, headers)) end
headers(content_length)
click to toggle source
# File lib/shellify/oauth_callback_handler.rb, line 41 def headers(content_length) [ 'HTTP/1.1 200 Ok', "date: #{Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S GMT")}", 'server: ruby', "Content-Length: #{content_length}", '', '', ].join("\r\n") end