Class: Slackify::Payload
- Inherits:
-
Object
- Object
- Slackify::Payload
- Defined in:
- lib/slackify.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build ⇒ Object
- #color ⇒ Object
- #default_field_map ⇒ Object
- #fetch(*args, &block) ⇒ Object
- #fields_map ⇒ Object
-
#initialize(context, status) ⇒ Payload
constructor
A new instance of Payload.
- #payload ⇒ Object
- #text ⇒ Object
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
#build ⇒ Object
15 16 17 |
# File 'lib/slackify.rb', line 15 def build "'payload=#{payload}'" end |
#color ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/slackify.rb', line 101 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 |
#default_field_map ⇒ Object
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 |
# File 'lib/slackify.rb', line 56 def default_field_map { 'status' => { title: 'Status', value: @status, short: true }, 'stage' => { title: 'Stage', value: fetch(:stage), short: true }, 'branch' => { title: 'Branch', value: fetch(:branch), short: true }, 'revision' => { title: 'Revision', value: fetch(:current_revision), short: true }, 'hosts' => { title: 'Hosts', value: fetch(:slack_hosts), short: true } } end |
#fetch(*args, &block) ⇒ Object
86 87 88 |
# File 'lib/slackify.rb', line 86 def fetch(*args, &block) @context.fetch(*args, &block) end |
#fields_map ⇒ Object
49 50 51 52 53 54 |
# File 'lib/slackify.rb', line 49 def fields_map @fields_map ||= [ default_field_map, fetch(:slack_custom_field_mapping), ].reduce(&:merge) end |
#payload ⇒ Object
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 |
# File 'lib/slackify.rb', line 19 def payload fields = fetch(:slack_fields).each_with_object([]) { |field, fields| if fields_map[field].fetch(:value).respond_to?(:call) field_data = fields_map[field] field_data[:value] = field_data.fetch(:value).call fields.push(field_data) else fields.push(fields_map[field]) end } 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: fields, mrkdwn_in: fetch(:slack_mrkdwn_in), } ] } ) end |
#text ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/slackify.rb', line 90 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 |