nautilfer 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0345f886d7a3add841aab8d2ba2557232461dea43a39cbe383aef7841a380dbe
4
- data.tar.gz: 03d7133181c366fcbe8b38eb3c25e029c61bc65ab4f7c6657b122c8ee118e97e
3
+ metadata.gz: a67099350daf4531189ba0b87dc460a10cc26094677b7bcb218bd82e4f40f8e2
4
+ data.tar.gz: 33815f57747fc024a5d9035342e31a6766af2170a1067cc5aea2bd6b38b3eac5
5
5
  SHA512:
6
- metadata.gz: cd254576c3fa630717153bf93008f2534b27ac2384e87f37034fca3212f3bdb94f8c00b5bb0de0cff69bdbccd5d46711bd360dfeb2b4ae5ea8828a7a7c643e39
7
- data.tar.gz: ffe47507667a0bc83389db74d1599e61b8b276a6c8ca09fc67a680e3c458fe07beb460c95c598d8a97eaf5840575f1c81dc55bf0b2bf91af3bdc4f3dfd4a6c82
6
+ metadata.gz: '065369f4809851f0b8b2e3e592db61ff83287348a5269e75d202d38da311ce6533c4d1ce4e7afcf2742b3d4d2adb9e17c4de99d164b83eb1cdc5ba886726c8a2'
7
+ data.tar.gz: 05b2f7472395f241a2ff91a520abf10915ca548751282b11b1374e2437d8da6aec3d47e9eb9944c939d9f206d34ec2b149f54bab7999465830fb7191c7ca3ad6
data/README.md CHANGED
@@ -107,6 +107,27 @@ notifier = Nautilfer.new(endpoint: "#{workflow_endpoint}", adapter: :teams)
107
107
  notifier.notify("Deployment finished")
108
108
  ```
109
109
 
110
+ ### Configure message templates
111
+
112
+ Define reusable message templates in the global configuration and select them when instantiating a notifier. Templates are callables that receive the original message and return the formatted text.
113
+
114
+ ```ruby
115
+ Nautilfer.configure do |config|
116
+ config.message_templates = {
117
+ default: ->(message) { "[default] #{message}" },
118
+ headline: ->(message) { "## #{message}" }
119
+ }
120
+
121
+ config.default_message_template = :default
122
+ end
123
+
124
+ notifier = Nautilfer.new(endpoint: "#{workflow_endpoint}", adapter: :slack)
125
+ notifier.notify("Deployment finished") # => sends "[default] Deployment finished"
126
+
127
+ headline_notifier = Nautilfer.new(endpoint: "#{workflow_endpoint}", adapter: :slack, message_template: :headline)
128
+ headline_notifier.notify("Deployment finished") # => sends "## Deployment finished"
129
+ ```
130
+
110
131
  ## Chatwork Notification Integration
111
132
 
112
133
  To enable Chatwork notifications using the unified adapter interface, initialize `Nautilfer` with the Chatwork adapter and tar
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Nautilfer
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
5
5
  end
data/lib/nautilfer.rb CHANGED
@@ -13,11 +13,13 @@ class Nautilfer
13
13
  class Error < StandardError; end
14
14
 
15
15
  class Configuration
16
- attr_accessor :environment, :enabled_environments, :disabled_environments
16
+ attr_accessor :environment, :enabled_environments, :disabled_environments, :message_templates, :default_message_template
17
17
 
18
18
  def initialize
19
19
  @enabled_environments = []
20
20
  @disabled_environments = []
21
+ @message_templates = { default: ->(message) { message } }
22
+ @default_message_template = :default
21
23
  end
22
24
  end
23
25
 
@@ -33,7 +35,7 @@ class Nautilfer
33
35
  @configuration = Configuration.new
34
36
  end
35
37
 
36
- def initialize(endpoint:, adapter: Adapters::Teams.new, environment: nil, enabled_environments: nil, disabled_environments: nil)
38
+ def initialize(endpoint:, adapter: Adapters::Teams.new, environment: nil, enabled_environments: nil, disabled_environments: nil, message_template: nil)
37
39
  config = self.class.configuration
38
40
 
39
41
  @endpoint = URI.parse(endpoint)
@@ -41,6 +43,7 @@ class Nautilfer
41
43
  @environment = environment || config.environment || ENV['NAUTILFER_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV']
42
44
  @enabled_environments = normalize_env_list(enabled_environments, config.enabled_environments)
43
45
  @disabled_environments = normalize_env_list(disabled_environments, config.disabled_environments)
46
+ @message_template = resolve_message_template(message_template, config)
44
47
  end
45
48
 
46
49
  def notify(message)
@@ -52,7 +55,7 @@ class Nautilfer
52
55
 
53
56
  private
54
57
 
55
- attr_reader :endpoint, :adapter, :environment, :enabled_environments, :disabled_environments
58
+ attr_reader :endpoint, :adapter, :environment, :enabled_environments, :disabled_environments, :message_template
56
59
 
57
60
  def resolve_adapter(adapter)
58
61
  resolved_adapter =
@@ -99,7 +102,28 @@ class Nautilfer
99
102
  end
100
103
 
101
104
  def build_payload(message)
102
- [adapter.payload(message), adapter.headers]
105
+ [adapter.payload(apply_message_template(message)), adapter.headers]
106
+ end
107
+
108
+ def resolve_message_template(template_key, config)
109
+ return validate_message_template!(template_key) if template_key.respond_to?(:call)
110
+
111
+ key = template_key || config.default_message_template
112
+ resolved = config.message_templates[key]
113
+ resolved ||= config.message_templates[key.to_sym] if key.respond_to?(:to_sym)
114
+ raise Error, "Unknown message template: #{key}" unless resolved
115
+
116
+ validate_message_template!(resolved)
117
+ end
118
+
119
+ def validate_message_template!(template)
120
+ return template if template.respond_to?(:call)
121
+
122
+ raise Error, "Unsupported message template: #{template.inspect}"
123
+ end
124
+
125
+ def apply_message_template(message)
126
+ message_template.call(message)
103
127
  end
104
128
 
105
129
  def perform_request(payload, headers)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nautilfer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yusuke Abe