Class: ChatNotifier::Chatter
- Inherits:
-
Object
- Object
- ChatNotifier::Chatter
- Defined in:
- lib/chat_notifier/chatter.rb,
lib/chat_notifier/chatter/debug.rb,
lib/chat_notifier/chatter/slack.rb
Overview
All behavior for interacting with a notification platform
Direct Known Subclasses
Defined Under Namespace
Constant Summary collapse
- OPEN_TIMEOUT =
5- READ_TIMEOUT =
10
Instance Attribute Summary collapse
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
- #thread_store ⇒ Object
Class Method Summary collapse
- .handlers ⇒ Object
- .handles? ⇒ Boolean
- .handling(settings, repository:, environment:) ⇒ Object
- .register(klass) ⇒ Object
Instance Method Summary collapse
- #body ⇒ Object
- #conditional_post(messenger, process: method(:http_post)) ⇒ Object
- #default_thread_store ⇒ Object
- #http_client(uri) ⇒ Object
- #http_post(uri, body, headers = nil) ⇒ Object
-
#initialize(settings:, repository:, environment:) ⇒ Chatter
constructor
A new instance of Chatter.
- #payload(data) ⇒ Object
- #post(messenger, process: method(:http_post)) ⇒ Object
- #verbose? ⇒ Boolean
- #webhook_url ⇒ Object
Constructor Details
#initialize(settings:, repository:, environment:) ⇒ Chatter
Returns a new instance of Chatter.
15 16 17 18 19 |
# File 'lib/chat_notifier/chatter.rb', line 15 def initialize(settings:, repository:, environment:) @settings = settings @repository = repository @environment = environment end |
Instance Attribute Details
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
21 22 23 |
# File 'lib/chat_notifier/chatter.rb', line 21 def environment @environment end |
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
21 22 23 |
# File 'lib/chat_notifier/chatter.rb', line 21 def repository @repository end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
21 22 23 |
# File 'lib/chat_notifier/chatter.rb', line 21 def settings @settings end |
#thread_store ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/chat_notifier/chatter.rb', line 86 def thread_store @thread_store ||= if settings.fetch("NOTIFY_THREAD_STORE", nil) == "none" ThreadStore::Null.new else default_thread_store end end |
Class Method Details
.handlers ⇒ Object
24 25 26 |
# File 'lib/chat_notifier/chatter.rb', line 24 def handlers @handlers ||= [] end |
.handles? ⇒ Boolean
28 29 30 |
# File 'lib/chat_notifier/chatter.rb', line 28 def handles? false end |
.handling(settings, repository:, environment:) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/chat_notifier/chatter.rb', line 36 def handling(settings, repository:, environment:) handlers.select { |handler| handler.handles?(settings) }.map do |klass| klass.new( settings: settings, repository: repository, environment: environment ) end end |
.register(klass) ⇒ Object
32 33 34 |
# File 'lib/chat_notifier/chatter.rb', line 32 def register(klass) handlers << klass end |
Instance Method Details
#body ⇒ Object
50 51 |
# File 'lib/chat_notifier/chatter.rb', line 50 def body end |
#conditional_post(messenger, process: method(:http_post)) ⇒ Object
59 60 61 62 63 |
# File 'lib/chat_notifier/chatter.rb', line 59 def conditional_post(messenger, process: method(:http_post)) return if messenger.success? && !verbose? post(messenger, process:) end |
#default_thread_store ⇒ Object
94 95 96 |
# File 'lib/chat_notifier/chatter.rb', line 94 def default_thread_store ThreadStore::Null.new end |
#http_client(uri) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/chat_notifier/chatter.rb', line 65 def http_client(uri) Net::HTTP.new(uri.host, uri.port).tap do |http| http.use_ssl = uri.scheme == "https" http.open_timeout = OPEN_TIMEOUT http.read_timeout = READ_TIMEOUT end end |
#http_post(uri, body, headers = nil) ⇒ Object
73 74 75 76 77 |
# File 'lib/chat_notifier/chatter.rb', line 73 def http_post(uri, body, headers = nil) http_client(uri).start do |http| http.request_post(uri.request_uri, body, headers) end end |
#payload(data) ⇒ Object
98 99 100 |
# File 'lib/chat_notifier/chatter.rb', line 98 def payload(data) data.to_json end |
#post(messenger, process: method(:http_post)) ⇒ Object
53 54 55 56 57 |
# File 'lib/chat_notifier/chatter.rb', line 53 def post(messenger, process: method(:http_post)) uri = URI(webhook_url) process.call(uri, payload(messenger)) end |
#verbose? ⇒ Boolean
79 80 81 |
# File 'lib/chat_notifier/chatter.rb', line 79 def verbose? !!settings.fetch("NOTIFIER_VERBOSE", false) end |
#webhook_url ⇒ Object
47 48 |
# File 'lib/chat_notifier/chatter.rb', line 47 def webhook_url end |