Skip to content

Commit 26f1cc6

Browse files
committed
Use Open3.capture3.
1 parent c58f8fb commit 26f1cc6

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

slack-api-explorer/commands/slack.rb

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,19 @@ def self.call(client, data, match)
1111
expression.gsub! '—', '--'
1212
logger.info "SLACK: #{client.owner} - #{expression}"
1313
args, pipe = Shellwords.parse(expression)
14-
execute(client, args) do |output, error|
15-
if error && !error.blank?
16-
client.say(channel: data.channel, text: "```\n#{error}```")
17-
else
18-
output = pipe ? JsonPath.on(output, pipe) : JSON.parse(output)
19-
output = JSON.pretty_generate(output)
20-
client.say(channel: data.channel, text: "```\n#{output}```")
21-
end
14+
output, error, _ = Open3.capture3(* ['slack', '--slack-api-token', client.owner.token, args].flatten)
15+
error&.strip!
16+
output&.strip!
17+
if error && !error.blank?
18+
client.say(channel: data.channel, text: "```\n#{error}```")
19+
else
20+
output = pipe ? JsonPath.on(output, pipe) : JSON.parse(output)
21+
output = JSON.pretty_generate(output)
22+
client.say(channel: data.channel, text: "```\n#{output}```")
2223
end
2324
rescue SyntaxError => e
2425
client.say(channel: data.channel, text: e.message)
2526
end
26-
27-
def self.execute(client, args)
28-
Open3.popen3(* [
29-
'slack',
30-
'--slack-api-token',
31-
client.owner.token,
32-
args
33-
].flatten) do |_, stdout, stderr, _|
34-
yield stdout.read&.strip, stderr.read&.strip
35-
end
36-
end
3727
end
3828
end
3929
end

spec/slack-api-explorer/commands/slack_spec.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
end
2424

2525
before do
26-
allow(described_class).to receive(:execute).and_yield(JSON.dump(json), nil)
26+
allow(Open3).to receive(:capture3).and_return(JSON.dump(json), nil)
2727
end
2828

2929
it 'returns raw json' do
@@ -73,7 +73,7 @@
7373
end
7474

7575
before do
76-
allow(described_class).to receive(:execute).and_yield(JSON.dump(json), nil)
76+
allow(Open3).to receive(:capture3).and_return(JSON.dump(json), nil)
7777
end
7878

7979
it 'returns multiple json values' do
@@ -92,9 +92,8 @@
9292

9393
context 'chat postMessage' do
9494
it 'unescapes channel' do
95-
expect(described_class).to receive(:execute).with(client,
96-
['chat', 'postMessage', '--text',
97-
'Hello World', '--channel', '#C04KB5X4D']).and_yield(JSON.dump(ok: true), nil)
95+
allow(Open3).to receive(:capture3).and_return(JSON.dump(ok: true), nil)
96+
expect(Open3).to receive(:capture3).with('slack', '--slack-api-token', client.token, 'chat', 'postMessage', '--text', 'Hello World', '--channel', '#C04KB5X4D')
9897
expect(message: "#{SlackRubyBot.config.user} chat postMessage --text 'Hello World' --channel <#C04KB5X4D>").to respond_with_slack_message("```\n{\n \"ok\": true\n}```")
9998
end
10099
end

0 commit comments

Comments
 (0)