From 33f414024407b33f94d8cd3a51f1dc543e254d4f Mon Sep 17 00:00:00 2001 From: Mike Boone Date: Thu, 3 Mar 2016 15:26:40 -0500 Subject: [PATCH] Added send_file_content method. --- lib/lita/adapters/slack/api.rb | 14 ++++ lib/lita/adapters/slack/chat_service.rb | 13 ++++ spec/lita/adapters/slack/api_spec.rb | 77 +++++++++++++++++++ spec/lita/adapters/slack/chat_service_spec.rb | 17 ++++ 4 files changed, 121 insertions(+) diff --git a/lib/lita/adapters/slack/api.rb b/lib/lita/adapters/slack/api.rb index 3e90fa0..f1148b4 100644 --- a/lib/lita/adapters/slack/api.rb +++ b/lib/lita/adapters/slack/api.rb @@ -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 diff --git a/lib/lita/adapters/slack/chat_service.rb b/lib/lita/adapters/slack/chat_service.rb index 317997b..8358f1d 100644 --- a/lib/lita/adapters/slack/chat_service.rb +++ b/lib/lita/adapters/slack/chat_service.rb @@ -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 diff --git a/spec/lita/adapters/slack/api_spec.rb b/spec/lita/adapters/slack/api_spec.rb index cfac3f0..3e4b93c 100644 --- a/spec/lita/adapters/slack/api_spec.rb +++ b/spec/lita/adapters/slack/api_spec.rb @@ -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' } diff --git a/spec/lita/adapters/slack/chat_service_spec.rb b/spec/lita/adapters/slack/chat_service_spec.rb index 3161745..8621bb5 100644 --- a/spec/lita/adapters/slack/chat_service_spec.rb +++ b/spec/lita/adapters/slack/chat_service_spec.rb @@ -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