Module: Slack::API::Files

Extended by:
Files
Included in:
Files
Defined in:
lib/slack-wrapper/api/files.rb

Instance Method Summary collapse

Instance Method Details

#add_comment(text, file) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/slack-wrapper/api/files.rb', line 91

def add_comment(text, file)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/files.comments.add')
    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}&file=#{file}&comment=#{text}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)['files']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#delete(id) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/slack-wrapper/api/files.rb', line 48

def delete(id)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/files.delete')
    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}&file=#{id}")
    resp = http.request(req)
    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

#delete_comment(file, id) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/slack-wrapper/api/files.rb', line 109

def delete_comment(file, id)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/files.comments.delete')
    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}&file=#{file}&id=#{id}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)['files']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#edit_comment(text, file, id) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/slack-wrapper/api/files.rb', line 127

def edit_comment(text, file, id)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/files.comments.edit')
    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}&comment=#{text}&file=#{file}&id=#{id}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)['files']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#enable_URL(file) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/slack-wrapper/api/files.rb', line 163

def enable_URL(file)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/files.revokePublicURL')
    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}&file=#{file}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)['files']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#get_files(type = 'all') ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/slack-wrapper/api/files.rb', line 13

def get_files(type='all')
  if Slack::API::Auth
    type.split(',').each do |t|
      case t
        when 'all', 'spaces', 'snippets', 'images', 'gdocs', 'zips', 'pdfs'
          next
        else
          Slack::Errors.new({'error'  => 'invalid file type',
                             'detail' => 'Only all, spaces, snippets, images, gdocs, zips, pdfs supported'})
      end
    end
    uri  = URI.parse('https://slack.com/api/files.list')
    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}&types=#{type}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)['files']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#revoke_URL(file) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/slack-wrapper/api/files.rb', line 145

def revoke_URL(file)
  if Slack::API::Auth
    uri  = URI.parse('https://slack.com/api/files.sharedPublicURL')
    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}&file=#{file}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      JSON.parse(resp.body)['file']['url']
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#search(term, regex = false) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/slack-wrapper/api/files.rb', line 40

def search(term, regex=false)
  files = get_files
  if regex
    files.select{|f| f['name'] =~ /#{term}/}
  else
    files.select{|f| f['name'] == term}.first
  end
end

#upload(file, channel, opts = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/slack-wrapper/api/files.rb', line 66

def upload(file, channel, opts={})
  Slack::Errors.new({'error'  => 'file_invalid', 'detail' => 'Object is missing or is not a file'}) unless File.file?(file)
  opts['channels']        = channel
  opts['title']           = File.basename(file) unless opts.has_key? :title
  opts['filename']        = File.basename(file) unless opts.has_key? :filename
  opts['filetype']        = 'auto' unless opts.has_key? :filetype
  opts['initial_comment'] = '' unless opts.has_key? :initial_comment
  if Slack::API::Auth
    uri = URI.parse('https://slack.com/api/files.upload')
    opts['token'] = Slack::Config.token
    File.open(file) do |f|
      opts['file'] = UploadIO.new(f, `file --brief --mime-type #{file}`.strip, opts['filename'])
      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
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end