Module: Slack::API::Auth

Extended by:
Auth
Included in:
Auth
Defined in:
lib/slack-wrapper/api/auth.rb

Instance Method Summary collapse

Instance Method Details

#is_valid?Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/slack-wrapper/api/auth.rb', line 5

def is_valid?
  case Slack::Config.token
    when nil
      false
    when String
      uri  = URI.parse('https://slack.com/api/auth.test')
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}")
      resp = http.request(req)
      false unless resp.code == 200
      JSON.parse(resp.body)['ok']
    else
      false
  end
end

#whoamiObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/slack-wrapper/api/auth.rb', line 22

def whoami
  uri  = URI.parse('https://slack.com/api/auth.test')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}")
  resp = http.request(req)
  false unless resp.code == 200
  data = JSON.parse(resp.body)
  return {"name" => data['user'], 'id' => data['user_id']}
end