Class: Slackify::Payload

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, status) ⇒ Payload

Returns a new instance of Payload.



6
7
8
9
# File 'lib/slackify.rb', line 6

def initialize(context, status)
  @context = context
  @status = status
end

Class Method Details

.build(context, status) ⇒ Object



11
12
13
# File 'lib/slackify.rb', line 11

def self.build(context, status)
  new(context, status).build
end

Instance Method Details

#buildObject



15
16
17
# File 'lib/slackify.rb', line 15

def build
  "'payload=#{payload}'"
end

#colorObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/slackify.rb', line 79

def color
  case @status
  when :starting
    fetch(:slack_deploy_starting_color)
  when :success
    fetch(:slack_deploy_finished_color)
  when :failed
    fetch(:slack_deploy_failed_color)
  end
end

#fetch(*args, &block) ⇒ Object



64
65
66
# File 'lib/slackify.rb', line 64

def fetch(*args, &block)
  @context.fetch(*args, &block)
end

#payloadObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/slackify.rb', line 19

def payload
  MultiJson.dump(
    {
      channel: fetch(:slack_channel),
      username: fetch(:slack_username),
      icon_emoji: fetch(:slack_emoji),
      parse: fetch(:slack_parse),
      attachments: [
        {
          fallback: text,
          color: color,
          text: text,
          fields: [
            {
              title: 'Status',
              value: @status,
              short: true
            },
            {
              title: 'Stage',
              value: fetch(:stage),
              short: true
            },
            {
              title: 'Branch',
              value: fetch(:branch),
              short: true
            },
            {
              title: 'Revision',
              value: fetch(:current_revision),
              short: true
            },
            {
              title: 'Hosts',
              value: fetch(:slack_hosts),
              short: true
            },
          ]
        }
      ]
    }
  )
end

#textObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/slackify.rb', line 68

def text
  @text ||= case @status
  when :starting
    fetch(:slack_deploy_starting_text)
  when :success
    fetch(:slack_text)
  when :failed
    fetch(:slack_deploy_failed_text)
  end
end