class SupermarketSync::Notifier

> Notifications

Attributes

channels[RW]
url[RW]
username[RW]

Public Class Methods

new(**args) { |self| ... } click to toggle source
# File lib/supermarket_sync/notifier.rb, line 20
def initialize(**args)
  @url       = args[:url]
  @channels  = args[:channels]
  @username  = args[:username]
  @http_opts = args[:http_opts] || {}
  yield self if block_given?
end

Public Instance Methods

deprecated() click to toggle source

> Accumulators

# File lib/supermarket_sync/notifier.rb, line 49
def deprecated
  @deprecated ||= []
end
send!() click to toggle source
# File lib/supermarket_sync/notifier.rb, line 28
def send!
  messages = [build_deprecated, build_updated].compact
  return unless messages.any?
  Array(@channels).each do |channel|
    slack.ping '', attachments: messages, channel: channel
  end
end
slack() click to toggle source
# File lib/supermarket_sync/notifier.rb, line 36
def slack
  (cfg = {})[:username] = @username
  cfg[:http_options] = { verify_mode: OpenSSL::SSL::VERIFY_NONE }

  Slack::Notifier.new @url do
    defaults cfg
  end
end
updated() click to toggle source
# File lib/supermarket_sync/notifier.rb, line 53
def updated
  @updated ||= []
end

Private Instance Methods

build_deprecated() click to toggle source

> Message Constructors

# File lib/supermarket_sync/notifier.rb, line 60
            def build_deprecated # rubocop: disable MethodLength
      return unless deprecated.any?
      fields = deprecated.map do |cb|
        {
          title: cb[:source]['name'],
          short: true,
          value: <<-MSGBODY.gsub(/^\s+/, '')
            * Replacement: #{cb[:source]['replacement'] || '**NONE**'}
          MSGBODY
        }
      end

      {
        color: '#FF0000',
        mrkdwn_in: %w[pretext fields],
        fields: fields,
        pretext: <<-MSGBODY.gsub(/^\s+/, '')
          <!here> :warning: **The following cookbooks have been deprecated:**
        MSGBODY
      }
    end
build_updated() click to toggle source
# File lib/supermarket_sync/notifier.rb, line 82
            def build_updated # rubocop: disable AbcSize, MethodLength
      return unless updated.any?
      rows = updated.map do |cb|
        [
          '|',
          "[#{cb[:source]['name']}](#{cb[:source]['external_url']})",
          '|',
          ::File.basename(cb[:source]['latest_version']),
          '|',
          cb[:source]['deprecated'] ? ':warning:' : ':white_check_mark:',
          '|'
        ].join(' ')
      end.join("\n")

      {
        mrkdwn_in: %w[text],
        pretext: '<!here> :checkered_flag: **The following cookbooks have been synchronized:**',
        short: false,
        text: <<-MSGBODY.gsub(/^\s+/, '')
        | Cookbook | Version | Deprecation Status |
        |:--------:|:-------:|:------------------:|
        #{rows}
        MSGBODY
      }
    end