Sha256: 137a522086a130c0ee488fc29a782f491af4290b2657152180a9d5b134255de3

Contents?: true

Size: 980 Bytes

Versions: 1

Compression:

Stored size: 980 Bytes

Contents

module Slacked
  SLACK_PROFILE_IMAGE=':robot_face:'
  SLACK_WEBHOOK_URL_KEY='SLACK_WEBHOOK'
  SLACK_DEFAULT_MESSAGE_KEY='SLACK_DEFAULT_MESSAGE'
  SLACK_CONFIG= {
      icon_emoji: SLACK_PROFILE_IMAGE
  }

  class << self
    def post message = ENV[SLACK_DEFAULT_MESSAGE_KEY]
      return false if message.nil? || message.empty? || disabled?
      notifier = slack_notifier
      notifier.ping message, SLACK_CONFIG
    end

    def post_async message
      Thread.start do
        result = post(message)
        defined?(ActiveRecord) ? ActiveRecord::Base.connection.close : nil
        result
      end
    end

    def disabled?
      return false unless rails?
      rails_config = Rails.application.config
      rails_config.respond_to?(:slacked_disabled) && rails_config.slacked_disabled
    end

    private
    def slack_notifier webhook_url = ENV[SLACK_WEBHOOK_URL_KEY]
      Slack::Notifier.new webhook_url
    end

    def rails?
      defined?(Rails)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slacked-0.9.0 lib/slacked/slack_post.rb