Class: ChatNotifier::Chatter

Inherits:
Object
  • Object
show all
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

Slack

Defined Under Namespace

Classes: Debug, Slack

Constant Summary collapse

OPEN_TIMEOUT =
5
READ_TIMEOUT =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#environmentObject (readonly)

Returns the value of attribute environment.



21
22
23
# File 'lib/chat_notifier/chatter.rb', line 21

def environment
  @environment
end

#repositoryObject (readonly)

Returns the value of attribute repository.



21
22
23
# File 'lib/chat_notifier/chatter.rb', line 21

def repository
  @repository
end

#settingsObject (readonly)

Returns the value of attribute settings.



21
22
23
# File 'lib/chat_notifier/chatter.rb', line 21

def settings
  @settings
end

#thread_storeObject



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

.handlersObject



24
25
26
# File 'lib/chat_notifier/chatter.rb', line 24

def handlers
  @handlers ||= []
end

.handles?Boolean

Returns:

  • (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

#bodyObject



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_storeObject



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

Returns:

  • (Boolean)


79
80
81
# File 'lib/chat_notifier/chatter.rb', line 79

def verbose?
  !!settings.fetch("NOTIFIER_VERBOSE", false)
end

#webhook_urlObject



47
48
# File 'lib/chat_notifier/chatter.rb', line 47

def webhook_url
end