newshound 0.1.1 → 0.2.0
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 +4 -4
- data/README.md +152 -155
- data/lib/generators/newshound/install/install_generator.rb +9 -50
- data/lib/generators/newshound/install/templates/newshound.rb +27 -33
- data/lib/newshound/authorization.rb +46 -0
- data/lib/newshound/configuration.rb +12 -18
- data/lib/newshound/middleware/banner_injector.rb +288 -0
- data/lib/newshound/railtie.rb +32 -36
- data/lib/newshound/version.rb +1 -1
- data/lib/newshound.rb +5 -43
- data/newshound.gemspec +2 -7
- metadata +6 -52
- data/lib/newshound/daily_report_job.rb +0 -31
- data/lib/newshound/scheduler.rb +0 -42
- data/lib/newshound/slack_notifier.rb +0 -44
- data/lib/newshound/transport/base.rb +0 -28
- data/lib/newshound/transport/slack.rb +0 -67
- data/lib/newshound/transport/sns.rb +0 -115
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Newshound
|
4
|
-
module Transport
|
5
|
-
class Base
|
6
|
-
attr_reader :configuration, :logger
|
7
|
-
|
8
|
-
def initialize(configuration: nil, logger: nil)
|
9
|
-
@configuration = configuration || Newshound.configuration
|
10
|
-
@logger = logger || default_logger
|
11
|
-
end
|
12
|
-
|
13
|
-
def deliver(message)
|
14
|
-
raise NotImplementedError, "Subclasses must implement the #deliver method"
|
15
|
-
end
|
16
|
-
|
17
|
-
protected
|
18
|
-
|
19
|
-
def default_logger
|
20
|
-
if defined?(Rails)
|
21
|
-
Rails.logger
|
22
|
-
else
|
23
|
-
Logger.new(STDOUT)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "slack-ruby-client"
|
4
|
-
require_relative "base"
|
5
|
-
|
6
|
-
module Newshound
|
7
|
-
module Transport
|
8
|
-
class Slack < Base
|
9
|
-
attr_reader :webhook_client, :web_api_client
|
10
|
-
|
11
|
-
def initialize(configuration: nil, logger: nil, webhook_client: nil, web_api_client: nil)
|
12
|
-
super(configuration: configuration, logger: logger)
|
13
|
-
@webhook_client = webhook_client
|
14
|
-
@web_api_client = web_api_client
|
15
|
-
configure_slack_client
|
16
|
-
end
|
17
|
-
|
18
|
-
def deliver(message)
|
19
|
-
return unless configuration.valid?
|
20
|
-
|
21
|
-
if webhook_configured?
|
22
|
-
deliver_via_webhook(message)
|
23
|
-
elsif web_api_configured?
|
24
|
-
deliver_via_web_api(message)
|
25
|
-
else
|
26
|
-
logger.error "Newshound: No valid Slack configuration found"
|
27
|
-
false
|
28
|
-
end
|
29
|
-
rescue StandardError => e
|
30
|
-
logger.error "Newshound: Failed to send Slack notification: #{e.message}"
|
31
|
-
false
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def configure_slack_client
|
37
|
-
::Slack.configure do |config|
|
38
|
-
config.token = ENV["SLACK_API_TOKEN"] if ENV["SLACK_API_TOKEN"]
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def webhook_configured?
|
43
|
-
!configuration.slack_webhook_url.nil? && !configuration.slack_webhook_url.empty?
|
44
|
-
end
|
45
|
-
|
46
|
-
def web_api_configured?
|
47
|
-
!ENV["SLACK_API_TOKEN"].nil? && !ENV["SLACK_API_TOKEN"].empty?
|
48
|
-
end
|
49
|
-
|
50
|
-
def deliver_via_webhook(message)
|
51
|
-
client = webhook_client || ::Slack::Incoming::Webhook.new(configuration.slack_webhook_url)
|
52
|
-
client.post(message)
|
53
|
-
true
|
54
|
-
end
|
55
|
-
|
56
|
-
def deliver_via_web_api(message)
|
57
|
-
client = web_api_client || ::Slack::Web::Client.new
|
58
|
-
client.chat_postMessage(
|
59
|
-
channel: configuration.slack_channel,
|
60
|
-
blocks: message[:blocks],
|
61
|
-
text: "Daily Newshound Report"
|
62
|
-
)
|
63
|
-
true
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,115 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "base"
|
4
|
-
|
5
|
-
module Newshound
|
6
|
-
module Transport
|
7
|
-
class Sns < Base
|
8
|
-
attr_reader :sns_client
|
9
|
-
|
10
|
-
def initialize(configuration: nil, logger: nil, sns_client: nil)
|
11
|
-
super(configuration: configuration, logger: logger)
|
12
|
-
@sns_client = sns_client || build_sns_client
|
13
|
-
end
|
14
|
-
|
15
|
-
def deliver(message)
|
16
|
-
return false unless valid_sns_configuration?
|
17
|
-
|
18
|
-
formatted_message = format_message(message)
|
19
|
-
|
20
|
-
response = sns_client.publish(
|
21
|
-
topic_arn: configuration.sns_topic_arn,
|
22
|
-
message: formatted_message,
|
23
|
-
subject: extract_subject(message)
|
24
|
-
)
|
25
|
-
|
26
|
-
logger.info "Newshound: Message sent to SNS, MessageId: #{response.message_id}"
|
27
|
-
true
|
28
|
-
rescue StandardError => e
|
29
|
-
logger.error "Newshound: Failed to send SNS notification: #{e.message}"
|
30
|
-
false
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
def build_sns_client
|
36
|
-
require "aws-sdk-sns"
|
37
|
-
|
38
|
-
options = {
|
39
|
-
region: configuration.aws_region || ENV["AWS_REGION"] || "us-east-1"
|
40
|
-
}
|
41
|
-
|
42
|
-
if configuration.aws_access_key_id && configuration.aws_secret_access_key
|
43
|
-
options[:credentials] = Aws::Credentials.new(
|
44
|
-
configuration.aws_access_key_id,
|
45
|
-
configuration.aws_secret_access_key
|
46
|
-
)
|
47
|
-
end
|
48
|
-
|
49
|
-
Aws::SNS::Client.new(options)
|
50
|
-
end
|
51
|
-
|
52
|
-
def valid_sns_configuration?
|
53
|
-
if configuration.sns_topic_arn.nil? || configuration.sns_topic_arn.empty?
|
54
|
-
logger.error "Newshound: SNS topic ARN not configured"
|
55
|
-
false
|
56
|
-
else
|
57
|
-
true
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def format_message(message)
|
62
|
-
case message
|
63
|
-
when Hash
|
64
|
-
if message[:blocks]
|
65
|
-
format_slack_blocks_for_sns(message[:blocks])
|
66
|
-
else
|
67
|
-
JSON.pretty_generate(message)
|
68
|
-
end
|
69
|
-
when String
|
70
|
-
message
|
71
|
-
else
|
72
|
-
message.to_s
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def format_slack_blocks_for_sns(blocks)
|
77
|
-
lines = []
|
78
|
-
|
79
|
-
blocks.each do |block|
|
80
|
-
case block[:type]
|
81
|
-
when "section"
|
82
|
-
if block[:text]
|
83
|
-
lines << format_text_element(block[:text])
|
84
|
-
end
|
85
|
-
when "header"
|
86
|
-
if block[:text]
|
87
|
-
lines << "=== #{format_text_element(block[:text])} ==="
|
88
|
-
end
|
89
|
-
when "divider"
|
90
|
-
lines << "---"
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
lines.join("\n\n")
|
95
|
-
end
|
96
|
-
|
97
|
-
def format_text_element(text_element)
|
98
|
-
return "" unless text_element
|
99
|
-
|
100
|
-
text = text_element[:text] || ""
|
101
|
-
text.gsub(/:([a-z_]+):/, '')
|
102
|
-
.gsub(/\*(.+?)\*/, '\1')
|
103
|
-
.gsub(/_(.+?)_/, '\1')
|
104
|
-
end
|
105
|
-
|
106
|
-
def extract_subject(message)
|
107
|
-
if message.is_a?(Hash)
|
108
|
-
message[:subject] || "Newshound Notification"
|
109
|
-
else
|
110
|
-
"Newshound Notification"
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|