Skip to content

Commit

Permalink
feat(ping): allow sending with an attachment only
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenosloan committed Nov 30, 2015
1 parent 6f93937 commit 21e6ce3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- allow sending with attachments only [#48]

# 1.4.0
- Format attachment messages with the LinkFormatter [@bhuga #37]
- Add support for mailto links in markdown formatted links [@keithpitty #43]
Expand Down
12 changes: 10 additions & 2 deletions lib/slack-notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@ def initialize webhook_url, options={}
end

def ping message, options={}
message = LinkFormatter.format(message)
if message.is_a?(Hash)
message, options = nil, message
end

if attachments = options[:attachments] || options["attachments"]
attachments.each do |attachment|
["text", :text].each do |key|
attachment[key] = LinkFormatter.format(attachment[key]) if attachment.has_key?(key)
end
end
end
payload = default_payload.merge(options).merge(text: message)

payload = default_payload.merge(options)
client = payload.delete(:http_client) || http_client
http_options = payload.delete(:http_options)

unless message.nil?
payload.merge!(text: LinkFormatter.format(message))
end

params = { payload: payload.to_json }
params[:http_options] = http_options if http_options

Expand Down
1 change: 1 addition & 0 deletions spec/integration/ping_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
notifier = Slack::Notifier.new ENV['SLACK_WEBHOOK_URL'], username: 'notifier'
puts "testing with ruby #{RUBY_VERSION}"
notifier.ping "hello/こんにちは from notifier test script on ruby: #{RUBY_VERSION}\225"
notifier.ping attachments: [{color:"#1BF5AF",fallback:"fallback",text:"attachment"}]
16 changes: 16 additions & 0 deletions spec/lib/slack-notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@
}]
end

it "allows sending only an attachment" do
expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post).with(
URI.parse('http://example.com'),
payload: '{"channel":"foo","attachments":[{"text":"attachment","fallback":"fallback"}]}'
)

expect{
described_class.new('http://example.com')
.ping channel: 'foo',
attachments: [{
text: 'attachment',
fallback: 'fallback'
}]
}.not_to raise_error
end

context "with a default channel set" do

before :each do
Expand Down

0 comments on commit 21e6ce3

Please sign in to comment.