diff --git a/lib/logstash/outputs/slack.rb b/lib/logstash/outputs/slack.rb index 047f404..dfca9d8 100644 --- a/lib/logstash/outputs/slack.rb +++ b/lib/logstash/outputs/slack.rb @@ -36,6 +36,24 @@ def register @content_type = "application/x-www-form-urlencoded" end # def register + private + def recursive_sprintf(event, node) + case node + when String + ret = event.sprintf(node) + when Array + ret = node.map do |obj| + recursive_sprintf( event, obj ) + end + when Hash + ret = Hash.new + node.each_pair do |key, value| + ret[key] = recursive_sprintf( event, value ) + end + end + ret + end + public def receive(event) return unless output?(event) @@ -61,7 +79,7 @@ def receive(event) end if @attachments and @attachments.any? - payload_json['attachments'] = @attachments.map { |x| JSON.parse(event.sprintf(JSON.dump(x))) } + payload_json['attachments'] = recursive_sprintf(event , @attachments) end if event.include?('attachments') and event.get('attachments').is_a?(Array) if event.get('attachments').any? diff --git a/logstash-output-slack.gemspec b/logstash-output-slack.gemspec index 1e62382..222a588 100644 --- a/logstash-output-slack.gemspec +++ b/logstash-output-slack.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'logstash-output-slack' - s.version = '2.1.1' + s.version = '7.0.0' s.licenses = ['MIT','Apache License (2.0)'] s.summary = "Write events to Slack" s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program" @@ -21,7 +21,7 @@ Gem::Specification.new do |s| # Gem dependencies s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99" - s.add_runtime_dependency "public_suffix", "< 1.5.0" + s.add_runtime_dependency "public_suffix" s.add_runtime_dependency "logstash-codec-plain" s.add_runtime_dependency "rest-client", '~> 1.8', ">= 1.8.0" diff --git a/spec/outputs/slack_spec.rb b/spec/outputs/slack_spec.rb index fe1cbad..f90fb35 100644 --- a/spec/outputs/slack_spec.rb +++ b/spec/outputs/slack_spec.rb @@ -228,6 +228,35 @@ def test_one_event(event, expected_json, expected_url = "http://requestb.in/r9lk test_one_event(event, expected_json) end end + + context "when attachements contain interpolations" do + let(:data) { { + "message" => "This message should show in slack", + "x" => "3", + "image" => "http://example.com/image.png", + "textquot" => "Text with \"quotation marks\"", + } } + + let(:config) { { + "url" => "http://requestb.in/r9lkbzr9", + "attachments" => [ + {"image_url" => "%{image}", + "textquot" => "%{textquot}" + } + ] + } } + + it "uses and formats all provided values" do + expected_json = { + :text => "This message should show in slack", + :attachments => [{ + :image_url => "http://example.com/image.png", + :textquot => "Text with \"quotation marks\"" + }] + } + test_one_event(event, expected_json) + end + end end describe "interpolation in url field" do