Class: Semvergen::SlackNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/semvergen/slack_notifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ SlackNotifier



7
8
9
10
11
# File 'lib/semvergen/slack_notifier.rb', line 7

def initialize(opts)
  @webhook_url = opts["webhook_url"]
  @username    = opts["username"]
  @icons       = opts["icons"]
end

Instance Method Details

#attachment(gem_name, new_version, change_log_message) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/semvergen/slack_notifier.rb', line 34

def attachment(gem_name, new_version, change_log_message)
  {
    title:     "#{user} has published a new version of '#{gem_name}'",
    fallback:  change_log_message,
    fields:    [
                 {title: "Version", value: new_version},
                 {title: "Change Log", value: change_log_message}

               ],
    color:     "good",
    mrkdwn_in: ["text", "title", "fallback", "fields"]
  }
end

#gem_published(gem_name, new_version, change_log_message) ⇒ Object



13
14
15
16
17
18
# File 'lib/semvergen/slack_notifier.rb', line 13

def gem_published(gem_name, new_version, change_log_message)
  options = {}
  options[:icon_url] = @icons.sample if @icons
  options[:attachments] = [attachment(gem_name, new_version, change_log_message)]
  notifier.ping "Semvergen: release #{gem_name}", options
end

#notifierObject



20
21
22
23
24
25
# File 'lib/semvergen/slack_notifier.rb', line 20

def notifier
  @notifier ||= Slack::Notifier.new(
    @webhook_url,
    username: @username
  )
end

#userObject



27
28
29
30
31
32
# File 'lib/semvergen/slack_notifier.rb', line 27

def user
  [
    ENV["USER"],
    `whoami`
  ].compact.detect { |u| !u.nil? }
end