Skip to content

Commit

Permalink
Let attachments pass single values to prevent typing [] in params
Browse files Browse the repository at this point in the history
I updated the ping function, so you can pass attachments with a single value.
I needed to implement a little helper function (#wrap_array) to prevent the hash being parsed as arrays like this:
```
a_ok_note = {
  fallback: "Everything looks peachy",
  text: "Everything looks peachy",
  color: "good"
}
Array(a_ok_note) # => [[:fallback, "Everything looks peachy"], [:text, "Everything looks peachy"], [:color, "good"]]
wrap_array(a_ok_note) # => [{:fallback=>"Everything looks peachy", :text=>"Everything looks peachy", :color=>"good"}]
```
  • Loading branch information
siegy22 committed Nov 30, 2015
1 parent 18da5f7 commit 54e1f78
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/slack-notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def ping message, options={}
end

if attachments = options[:attachments] || options["attachments"]
attachments.each do |attachment|
wrap_array(attachments).each do |attachment|
["text", :text].each do |key|
attachment[key] = LinkFormatter.format(attachment[key]) if attachment.has_key?(key)
end
Expand Down Expand Up @@ -67,5 +67,15 @@ def username= username
def escape(text)
text.gsub(HTML_ESCAPE_REGEXP, HTML_ESCAPE)
end

def wrap_array(object)
if object.nil?
[]
elsif object.respond_to?(:to_ary)
object.to_ary || [object]
else
[object]
end
end
end
end
13 changes: 13 additions & 0 deletions spec/lib/slack-notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@
}.not_to raise_error
end

it "passes attachment messages through LinkFormatter, even if a single value is passed" do
expect( Slack::Notifier::LinkFormatter ).to receive(:format)
.with("a random message")
expect( Slack::Notifier::LinkFormatter ).to receive(:format)
.with("attachment message")
attachment = {
color: "#000",
text: "attachment message",
fallback: "fallback message"
}
subject.ping "a random message", attachments: attachment
end

context "with a default channel set" do

before :each do
Expand Down

0 comments on commit 54e1f78

Please sign in to comment.