Class: JekyllRecker::Social::Slack

Inherits:
Share
  • Object
show all
Defined in:
lib/jekyll_recker/social.rb

Overview

Slack

Slack social sharing backend

Constant Summary collapse

KEY =
'slack'

Instance Method Summary collapse

Methods inherited from Share

#config, #config_key, #dry?, #initialize, #latest, #latest_title, #post_body, share

Methods included from Mixins::Logging

#logger

Constructor Details

This class inherits a constructor from JekyllRecker::Social::Share

Instance Method Details

#configure!Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jekyll_recker/social.rb', line 80

def configure!
  @creds = {}
  workspaces.each do |key, data|
    webhook = ENV["SLACK_#{key.upcase}_WEBHOOK"] || extract_from_config(data)
    if webhook.nil?
      raise "cannot find slack webhook for #{key} workspace!"
    end

    @creds[key] = webhook
  end
end

#post!Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/jekyll_recker/social.rb', line 92

def post!
  message_body = ::Slack::Notifier::Util::LinkFormatter.format(post_body)
  workspaces.each do |key, config|
    logger.info "posting to #{key} workspace"
    if @dry
      logger.info("BEGIN MESSAGE\n#{message_body.strip}\nEND MESSAGE")
    else
      ::Slack::Notifier.new(
        @creds[key].strip,
        channel: config.fetch('channel'),
        username: config.fetch('username'),
        icon_emoji: config.fetch('emoji')
      ).post(text: message_body)
    end
  end
end