Sha256: a430ea6cef6434c3aded9dd9e77b1fd8cd1e4ccdb9745cfe3b599daaf3a1b12b

Contents?: true

Size: 1.16 KB

Versions: 13

Compression:

Stored size: 1.16 KB

Contents

require 'net/http'

module NdrDevSupport
  # This Class publishes messages to Slack
  class SlackMessagePublisher
    def initialize(url, default_options = {})
      @url = url
      @default_options = default_options
    end

    def post(options = {})
      request = json_request
      request.body = message(options)

      use_ssl = request.uri.scheme == 'https'
      http =
        if proxy
          proxy.start(request.uri.host, use_ssl: use_ssl)
        else
          Net::HTTP.start(request.uri.host, request.uri.port, use_ssl: use_ssl)
        end

      http.request(request)
    end

    private

    def json_request
      uri = URI.parse(@url)
      request = Net::HTTP::Post.new(uri)
      # request.basic_auth(*@auth.split(':')) if @auth
      request['Content-Type'] = 'application/json'
      request
    end

    def message(options)
      @default_options.merge(options).to_json
    end

    def proxy
      return @proxy if @proxy

      return if ENV['https_proxy'].nil?
      host_and_port = ENV['https_proxy'].match(%r{\A(?:https?://)?([^:]+):(\d+)})[1, 2]

      return if host_and_port.nil?
      @proxy = Net::HTTP.Proxy(*host_and_port)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ndr_dev_support-7.3.1 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-7.3.0 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-7.2.6 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-7.2.5 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-7.2.4 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-7.2.3 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-7.2.2 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-7.2.1 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-7.2.0 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-7.1.0 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-7.0.0 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-6.1.9 lib/ndr_dev_support/slack_message_publisher.rb
ndr_dev_support-6.1.8 lib/ndr_dev_support/slack_message_publisher.rb