Module: Slack::API::RTM

Extended by:
RTM
Included in:
RTM
Defined in:
lib/slack-wrapper/api/rtm.rb

Instance Method Summary collapse

Instance Method Details

#connect(opts = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/slack-wrapper/api/rtm.rb', line 7

def connect(opts={})
  if Slack::API::Auth
    opts['token'] = Slack::Config.token
    uri = URI.parse('https://slack.com/api/rtm.connect')
    req = Net::HTTP::Post::Multipart.new(uri.path, opts)
    res = Net::HTTP::new(uri.host, uri.port)
    res.use_ssl = true
    res.verify_mode = OpenSSL::SSL::VERIFY_NONE
    resp = res.start do |http|
      http.request(req)
    end
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)['url']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#start(opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/slack-wrapper/api/rtm.rb', line 28

def start(opts={})
  opts['no_unreads'] = true unless opts.key? 'no_unreads'
  opts['simple_latest'] = true unless opts.key? 'simple_latest'
  opts['token'] = Slack::Config.token
  if Slack::API::Auth
    uri = URI.parse('https://slack.com/api/rtm.start')
    req = Net::HTTP::Post::Multipart.new(uri.path, opts)
    res = Net::HTTP::new(uri.host, uri.port)
    res.use_ssl = true
    res.verify_mode = OpenSSL::SSL::VERIFY_NONE
    resp = res.start do |http|
      http.request(req)
    end
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end