Sha256: dfad9d9a3649d3a57c325bf29f4df34f6d94531f685594c6cf75ed0ebb2d1801

Contents?: true

Size: 953 Bytes

Versions: 1

Compression:

Stored size: 953 Bytes

Contents

require 'net/http'
require 'uri'
require 'json'

module Spanx
  module Notifier
    class Slack < Base
      attr_reader :config

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

      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

      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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spanx-0.3.0 lib/spanx/notifier/slack.rb