slack-notifier 1.5.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/lib/slack-notifier.rb +35 -60
  3. data/lib/slack-notifier/config.rb +37 -0
  4. data/lib/slack-notifier/payload_middleware.rb +21 -0
  5. data/lib/slack-notifier/payload_middleware/base.rb +35 -0
  6. data/lib/slack-notifier/payload_middleware/format_attachments.rb +37 -0
  7. data/lib/slack-notifier/payload_middleware/format_message.rb +19 -0
  8. data/lib/slack-notifier/payload_middleware/stack.rb +35 -0
  9. data/lib/slack-notifier/util/escape.rb +15 -0
  10. data/lib/slack-notifier/util/http_client.rb +53 -0
  11. data/lib/slack-notifier/util/link_formatter.rb +63 -0
  12. data/lib/slack-notifier/version.rb +2 -1
  13. data/spec/end_to_end_spec.rb +84 -0
  14. data/spec/integration/ping_integration_test.rb +12 -5
  15. data/spec/lib/slack-notifier/config_spec.rb +71 -0
  16. data/spec/lib/slack-notifier/payload_middleware/base_spec.rb +75 -0
  17. data/spec/lib/slack-notifier/payload_middleware/format_attachments_spec.rb +35 -0
  18. data/spec/lib/slack-notifier/payload_middleware/format_message_spec.rb +26 -0
  19. data/spec/lib/slack-notifier/payload_middleware/stack_spec.rb +93 -0
  20. data/spec/lib/slack-notifier/payload_middleware_spec.rb +32 -0
  21. data/spec/lib/slack-notifier/util/http_client_spec.rb +34 -0
  22. data/spec/lib/slack-notifier/{link_formatter_spec.rb → util/link_formatter_spec.rb} +30 -19
  23. data/spec/lib/slack-notifier_spec.rb +62 -128
  24. data/spec/spec_helper.rb +20 -5
  25. metadata +30 -9
  26. data/lib/slack-notifier/default_http_client.rb +0 -51
  27. data/lib/slack-notifier/link_formatter.rb +0 -62
  28. data/spec/lib/slack-notifier/default_http_client_spec.rb +0 -37
@@ -1,51 +0,0 @@
1
- module Slack
2
- class Notifier
3
-
4
- class DefaultHTTPClient
5
-
6
- class << self
7
- def post uri, params
8
- DefaultHTTPClient.new( uri, params ).call
9
- end
10
- end
11
-
12
- attr_reader :uri, :params, :http_options
13
-
14
- def initialize uri, params
15
- @uri = uri
16
- @http_options = params.delete(:http_options) || {}
17
- @params = params
18
- end
19
-
20
- def call
21
- http_obj.request request_obj
22
- end
23
-
24
- private
25
-
26
- def request_obj
27
- req = Net::HTTP::Post.new uri.request_uri
28
- req.set_form_data params
29
-
30
- return req
31
- end
32
-
33
- def http_obj
34
- http = Net::HTTP.new uri.host, uri.port
35
- http.use_ssl = (uri.scheme == "https")
36
-
37
- http_options.each do |opt, val|
38
- if http.respond_to? "#{opt}="
39
- http.send "#{opt}=", val
40
- else
41
- warn "Net::HTTP doesn't respond to `#{opt}=`, ignoring that option"
42
- end
43
- end
44
-
45
- return http
46
- end
47
-
48
- end
49
-
50
- end
51
- end
@@ -1,62 +0,0 @@
1
- module Slack
2
- class Notifier
3
- class LinkFormatter
4
-
5
- class << self
6
-
7
- def format string
8
- LinkFormatter.new(string).formatted
9
- end
10
-
11
- end
12
-
13
- def initialize string
14
- @orig = if string.respond_to? :scrub
15
- string.scrub
16
- else
17
- string
18
- end
19
- end
20
-
21
- def formatted
22
- @orig.gsub( html_pattern ) do |match|
23
- link = Regexp.last_match[1]
24
- text = Regexp.last_match[2]
25
- slack_link link, text
26
- end.gsub( markdown_pattern ) do |match|
27
- link = Regexp.last_match[2]
28
- text = Regexp.last_match[1]
29
- slack_link link, text
30
- end
31
-
32
- rescue => e
33
- if RUBY_VERSION < '2.1' && e.message.include?('invalid byte sequence')
34
- raise e, "#{e.message}. Consider including the 'string-scrub' gem to strip invalid characters"
35
- else
36
- raise e
37
- end
38
- end
39
-
40
- private
41
-
42
- def slack_link link, text=nil
43
- out = "<#{link}"
44
- out << "|#{text}" if text && !text.empty?
45
- out << ">"
46
-
47
- return out
48
- end
49
-
50
- # http://rubular.com/r/19cNXW5qbH
51
- def html_pattern
52
- / <a (?:.*?) href=['"](.+?)['"] (?:.*?)> (.+?) <\/a> /x
53
- end
54
-
55
- # http://rubular.com/r/guJbTK6x1f
56
- def markdown_pattern
57
- /\[ ([^\[\]]*?) \] \( ((https?:\/\/.*?) | (mailto:.*?)) \) /x
58
- end
59
-
60
- end
61
- end
62
- end
@@ -1,37 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Slack::Notifier::DefaultHTTPClient do
4
-
5
- describe "::post" do
6
- it "initializes DefaultHTTPClient with the given uri and params then calls" do
7
- http_post_double = instance_double("Slack::Notifier::DefaultHTTPClient")
8
-
9
- expect( described_class ).to receive(:new)
10
- .with( 'uri', 'params' )
11
- .and_return( http_post_double )
12
- expect( http_post_double ).to receive(:call)
13
-
14
- described_class.post 'uri', 'params'
15
- end
16
-
17
- # http_post is really tested in the integration spec,
18
- # where the internals are run through
19
- end
20
-
21
- describe "#initialize" do
22
- it "allows setting of options for Net::HTTP" do
23
- net_http_double = instance_double("Net::HTTP")
24
- http_client = described_class.new( URI.parse('http://example.com'), http_options: { open_timeout: 5 })
25
-
26
- allow( Net::HTTP ).to receive(:new)
27
- .and_return(net_http_double)
28
- allow( net_http_double ).to receive(:use_ssl=)
29
- allow( net_http_double ).to receive(:request)
30
-
31
- expect( net_http_double ).to receive(:open_timeout=).with(5)
32
-
33
- http_client.call
34
- end
35
- end
36
-
37
- end