Class: Spanx::Notifier::Slack

Inherits:
Base
  • Object
show all
Defined in:
lib/spanx/notifier/slack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Slack

Returns a new instance of Slack.



10
11
12
# File 'lib/spanx/notifier/slack.rb', line 10

def initialize(config)
  @config = config[:slack]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/spanx/notifier/slack.rb', line 8

def config
  @config
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/spanx/notifier/slack.rb', line 37

def enabled?
  config && config[:enabled]
end

#endpointObject



30
31
32
33
34
35
# File 'lib/spanx/notifier/slack.rb', line 30

def endpoint
  return nil unless enabled?
  token = config[:token]
  base_url = config[:base_url]
  URI.parse("#{base_url}/services/hooks/incoming-webhook?token=#{token}")
end

#publish(blocked_ip) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spanx/notifier/slack.rb', line 14

def publish blocked_ip
  return nil unless enabled?
  message = generate_block_ip_message(blocked_ip)

  uri = endpoint

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' => 'text/json'})
  request.body = { text: message }.to_json

  http.request(request)
end