Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Added send_file_content method.
Browse files Browse the repository at this point in the history
  • Loading branch information
boone committed Mar 4, 2016
1 parent 4902ef0 commit 7a068fa
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/lita/adapters/slack/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ def send_messages(channel_id, messages)
)
end

def send_file_content(content, filename, filetype = "text", title = nil,
initial_comment = nil, channels = nil)

call_api(
"files.upload",
content: content,
filetype: filetype,
filename: filename,
title: title,
initial_comment: initial_comment,
channels: channels
)
end

def set_topic(channel, topic)
call_api("channels.setTopic", channel: channel, topic: topic)
end
Expand Down
13 changes: 13 additions & 0 deletions lib/lita/adapters/slack/chat_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ def send_attachments(target, attachments)
api.send_attachments(target, Array(attachments))
end
alias_method :send_attachment, :send_attachments

# @param content [String] File contents.
# @param filename [String] File name.
# @param filetype [String] Slack file type. Default "text".
# @param title [String] File title. Default nil.
# @param initial_comment [String] Initial comment for file. Default nil.
# @param channels [String] Comma-separated list of channel ids. Default nil.
# @return [void]
def send_file_content(content, filename, filetype = "text", title = nil,
initial_comment = nil, channels = nil)
api.send_file_content(content, filename, filetype, title,
initial_comment, channels)
end
end
end
end
Expand Down
77 changes: 77 additions & 0 deletions spec/lita/adapters/slack/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,83 @@
end
end

describe "#send_file_content" do
let(:channels) { 'C1234567890' }
let(:comment) { 'First post!' }
let(:content) { 'lorem ipsum' }
let(:filename) { 'xyz.txt' }
let(:filetype) { 'text' }
let(:title) { 'XYZ Text File' }
let(:stubs) do
Faraday::Adapter::Test::Stubs.new do |stub|
stub.post(
'https://slack.com/api/files.upload',
token: token,
content: content,
filetype: filetype,
filename: filename,
title: title,
initial_comment: comment,
channels: channels
) do
[http_status, {}, http_response]
end
end
end

context "with a successful response" do
let(:http_response) do
MultiJson.dump({
ok: true,
file: {
id: "F12345678",
created: 1456523515,
timestamp: 1456523515,
name: "xyz.txt",
title: "XYZ Text File",
mimetype: "text/plain",
filetype: "text"
}
})
end

it "returns a response with the file id" do
response = subject.send_file_content(content, filename, filetype, title,
comment, channels)

expect(response['file']['id']).to eq("F12345678")
end
end

context "with a Slack error" do
let(:http_response) do
MultiJson.dump({
ok: false,
error: 'invalid_auth'
})
end

it "raises a RuntimeError" do
expect { subject.send_file_content(content, filename, filetype, title,
comment, channels) }.to raise_error(
"Slack API call to files.upload returned an error: invalid_auth."
)
end
end

context "with an HTTP error" do
let(:http_status) { 422 }
let(:http_response) { '' }

it "raises a RuntimeError" do
expect { subject.send_file_content(content, filename, filetype, title,
comment, channels) }.to raise_error(
"Slack API call to files.upload failed with status code 422."
)
end
end
end

describe "#set_topic" do
let(:channel) { 'C1234567890' }
let(:topic) { 'Topic' }
Expand Down
17 changes: 17 additions & 0 deletions spec/lita/adapters/slack/chat_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,21 @@
subject.send_attachment(room, attachment)
end
end

describe "#send_file_content" do
let(:channels) { 'C1234567890' }
let(:comment) { 'First post!' }
let(:content) { 'lorem ipsum' }
let(:filename) { 'xyz.txt' }
let(:filetype) { 'text' }
let(:title) { 'XYZ Text File' }

it "can send file content" do
expect(subject.api).to receive(:send_file_content).with(content, filename,
filetype, title, comment, channels)

subject.send_file_content(content, filename, filetype, title, comment,
channels)
end
end
end

0 comments on commit 7a068fa

Please sign in to comment.