Module: Slack::API::IM

Extended by:
IM
Included in:
IM
Defined in:
lib/slack-wrapper/api/im.rb

Instance Method Summary collapse

Instance Method Details

#close(id, opts = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/slack-wrapper/api/im.rb', line 33

def close(id, opts={})
  if Slack::API::Auth
    opts['channel'] = id
    opts['token'] = Slack::Config.token
    uri = URI.parse('https://slack.com/api/im.close')
    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']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#history(id, opts = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/slack-wrapper/api/im.rb', line 55

def history(id, opts={})
  if Slack::API::Auth
    opts['channel'] = id
    opts['token'] = Slack::Config.token
    uri = URI.parse('https://slack.com/api/im.history')
    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)['messages']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#list(opts = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/slack-wrapper/api/im.rb', line 77

def list(opts={})
  if Slack::API::Auth
    opts['token'] = Slack::Config.token
    uri = URI.parse('https://slack.com/api/im.list')
    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)['ims']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#open(id, opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/slack-wrapper/api/im.rb', line 9

def open(id, opts={})
  if Slack::API::Auth
    opts['user'] = id
    opts['return_im'] = true
    opts['token'] = Slack::Config.token
    opts['include_locale'] = true
    uri = URI.parse('https://slack.com/api/im.open')
    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)['channel']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end