Class: SlackCLI::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_cli/server.rb

Overview

Main Rack App for Callbacks

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#webbrickObject

Returns the value of attribute webbrick.



5
6
7
# File 'lib/slack_cli/server.rb', line 5

def webbrick
  @webbrick
end

Instance Method Details

#authorization_pathObject



64
65
66
# File 'lib/slack_cli/server.rb', line 64

def authorization_path
  File.join(config_path, 'authorization.json')
end

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/slack_cli/server.rb', line 7

def call(env)
  if env['REQUEST_PATH'] == '/callback/'
    callback(env)
  elsif env['REQUEST_PATH'] == '/token/'
    save_token(env)
  elsif env['REQUEST_PATH'] == '/'
    index(env)
  else
    [404, {}, ['File not found']]
  end
end

#callback(env) ⇒ Object



23
24
25
26
27
# File 'lib/slack_cli/server.rb', line 23

def callback(env)
  request = Rack::Request.new(env)
  slack_resp = fetch_token(code: request.params['code'])
  [302, { 'Location' => "http://localhost:65187/token/?json=#{CGI.escape(slack_resp)}" }, ['redirecting...']]
end

#client_idObject



76
77
78
# File 'lib/slack_cli/server.rb', line 76

def client_id
  ENV['SLACK_CLIENT_ID'] || raise('SLACK_CLIENT_ID not found in ENV!')
end

#client_secretObject



80
81
82
# File 'lib/slack_cli/server.rb', line 80

def client_secret
  ENV['SLACK_SECRET'] || raise('SLACK_SECRET not found in ENV!')
end

#config_pathObject



68
69
70
# File 'lib/slack_cli/server.rb', line 68

def config_path
  File.expand_path(File.join('~', '.config', 'slack_cli'))
end

#fetch_token(code:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/slack_cli/server.rb', line 29

def fetch_token(code:)
  base_url = 'https://slack.com/api/oauth.access'
  query = {
    client_id: client_id,
    client_secret: client_secret,
    code: code
  }
  query_uri = query.map { |k, v| "#{k}=#{v}" }.join('&')
  open("#{base_url}?#{query_uri}").read
end

#index(env) ⇒ Object



19
20
21
# File 'lib/slack_cli/server.rb', line 19

def index(env)
  render_view(src: 'index.erb', binding: binding)
end

#render_view(src:, binding:, status: 200) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/slack_cli/server.rb', line 56

def render_view(src:, binding:, status: 200)
  full_src_path = File.expand_path(File.join(__dir__, '..', '..', 'views', src))
  erb = ERB.new(File.read(full_src_path))
  erb.filename = src
  erb.result(binding)
  [status, {'Content Type' => 'text/html'}, [erb.result(binding)]]
end

#save_token(env) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/slack_cli/server.rb', line 40

def save_token(env)
  request = Rack::Request.new(env)
  json = JSON.parse(request['json'])
  if json['error']
    $stderr.puts "ERROR #{json['error']}"
    FileUtils.rm_rf(authorization_path)
    webbrick.shutdown if webbrick
    raise Error, json['error']
  end
  FileUtils.mkdir_p(config_path)
  File.open(authorization_path, 'w'){|f| f << request['json']}
  render_view(src: 'save_token.erb', binding: binding).tap do
    webbrick.shutdown if webbrick
  end
end

#save_token_url(token:) ⇒ Object



72
73
74
# File 'lib/slack_cli/server.rb', line 72

def save_token_url(token:)
  "http://localhost:65187/token/#{token}"
end