Class: Rails::Approvals::SlackMessage

Inherits:
Object
  • Object
show all
Defined in:
app/models/rails/approvals/slack_message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ SlackMessage

Returns a new instance of SlackMessage.



6
7
8
# File 'app/models/rails/approvals/slack_message.rb', line 6

def initialize(request)
  @request = request
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



4
5
6
# File 'app/models/rails/approvals/slack_message.rb', line 4

def request
  @request
end

Instance Method Details

#as_jsonObject



10
11
12
13
14
15
16
17
18
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/rails/approvals/slack_message.rb', line 10

def as_json
   = [].tap do ||
    if request.reason?
       << {
        type: 'mrkdwn',
        text: "*Reason:*\n#{request.reason}",
      }
    end
  end

  blocks = [
    {
      type: 'section',
      text: {
        type: 'mrkdwn',
        text: "*#{request.requester}* is requesting to run *#{request.command}*.",
      },
    },
  ]

  if .any?
    blocks << {
      type: 'section',
      fields: ,
    }
  end

  blocks << {
    type: 'divider',
  }

  if request.requested?
    global_id = request.to_global_id
    blocks << {
      type: "actions",
      block_id: global_id.to_s,
      elements: [
        {
          type: "button",
          text: {
            type: "plain_text",
            emoji: true,
            text: "Approve"
          },
          style: "primary",
          value: "approve"
        },
        {
          type: "button",
          text: {
            type: "plain_text",
            emoji: true,
            text: "Deny"
          },
          style: "danger",
          value: "deny"
        }
      ]
    }
  elsif request.approved?
    blocks << {
      type: 'section',
      text: {
        type: 'mrkdwn',
        text: "✅ *#{request.responder}* has approved the session.",
      },
    }
  elsif request.denied?
    blocks << {
      type: 'section',
      text: {
        type: 'mrkdwn',
        text: "🛑 *#{request.responder}* has denied the session.",
      },
    }
  elsif request.canceled?
    blocks << {
      type: 'section',
      text: {
        type: 'mrkdwn',
        text: "Request was canceled by *#{request.requester}*.",
      },
    }
  elsif request.timed_out?
    blocks << {
      type: 'section',
      text: {
        type: 'mrkdwn',
        text: '🛑 Session timed out.',
      },
    }
  end

  blocks
end