Class: NotificationServices::SlackService

Inherits:
NotificationService
  • Object
show all
Defined in:
app/models/notification_services/slack_service.rb

Constant Summary collapse

Label =
"slack"

Instance Method Summary collapse

Instance Method Details

#check_paramsObject


10
11
12
13
14
# File 'app/models/notification_services/slack_service.rb', line 10

def check_params
  if Fields.detect {|f| self[f[0]].blank? }
    errors.add :base, "You must specify your Slack Hook url."
  end
end

#configured?Boolean


68
69
70
# File 'app/models/notification_services/slack_service.rb', line 68

def configured?
  service_url.present?
end

#create_notification(problem) ⇒ Object


64
65
66
# File 'app/models/notification_services/slack_service.rb', line 64

def create_notification(problem)
  HTTParty.post(service_url, :body => post_payload(problem), :headers => { 'Content-Type' => 'application/json' })
end

#message_for_slack(problem) ⇒ Object


16
17
18
# File 'app/models/notification_services/slack_service.rb', line 16

def message_for_slack(problem)
  "[#{problem.app.name}][#{problem.environment}][#{problem.where}]: #{problem.error_class} #{problem_url(problem)}"
end

#post_payload(problem) ⇒ Object


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 'app/models/notification_services/slack_service.rb', line 20

def post_payload(problem)
  {
    :attachments => [
      {
         :fallback => message_for_slack(problem),
         :pretext => "<#{problem_url(problem)}|Errbit - #{problem.app.name}: #{problem.error_class}>",
         :color => "#D00000",
         :fields => [
            {
               :title => "Environment",
               :value => problem.environment,
               :short => false
            },
            {
               :title => "Location",
               :value => problem.where,
               :short => false
            },
            {
               :title => "Message",
               :value => problem.message.to_s,
               :short => false
            },
            {
               :title => "First Noticed",
               :value => problem.first_notice_at,
               :short => false
            },
            {
               :title => "Last Noticed",
               :value => problem.last_notice_at,
               :short => false
            },
            {
               :title => "Times Occurred",
               :value => problem.notices_count,
               :short => false
            }
         ]
      }
    ]
  }.to_json
end