Class: FeedbackSlackNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/nexmo_developer/app/services/feedback_slack_notifier.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feedback) ⇒ FeedbackSlackNotifier

Returns a new instance of FeedbackSlackNotifier.



6
7
8
9
# File 'lib/nexmo_developer/app/services/feedback_slack_notifier.rb', line 6

def initialize(feedback)
  @feedback = feedback
  @notifier = Slack::Notifier.new ENV['SLACK_WEBHOOK'], username: 'feedbot'
end

Class Method Details

.call(feedback) ⇒ Object



2
3
4
# File 'lib/nexmo_developer/app/services/feedback_slack_notifier.rb', line 2

def self.call(feedback)
  new(feedback).notify!
end

Instance Method Details

#admin_urlObject



27
28
29
# File 'lib/nexmo_developer/app/services/feedback_slack_notifier.rb', line 27

def admin_url
  "#{ENV['FEEDBOT_PROTOCOL']}://#{ENV['FEEDBOT_HOST']}/admin/feedbacks/#{@feedback.id}"
end

#answersObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/nexmo_developer/app/services/feedback_slack_notifier.rb', line 108

def answers
  @answers ||= @feedback.config.paths[@feedback.path]['steps'].map.with_index do |step, index|
    case step['type']
    when 'textarea'
      <<~HEREDOC
        *#{step['label'] || step['title']}*
        \t\t#{@feedback.steps[index]}
      HEREDOC
    when 'plain'
      "*#{step['title']}*"
    when 'radiogroup'
      options = step['questions'].map.with_index do |question, question_index|
        if question_index == @feedback.steps[index]
          "*#{question}*"
        else
          "#{question}"
        end
      end
      <<~HEREDOC
        *#{step['title']}*
        #{options.map { |o| "\t\t#{o}" }.join("\n")}
      HEREDOC
    when 'fieldset'
      fields = step['fields'].map.with_index do |field, _field_index|
        "*#{field['label']}:* #{@feedback.steps[index][field['name']]}"
      end
      <<~HEREDOC
        *#{step['title']}*
        #{fields.map { |f| "\t\t#{f}" }.join("\n")}
      HEREDOC
    end
  end
end

#bodyObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/nexmo_developer/app/services/feedback_slack_notifier.rb', line 86

def body
  <<~HEREDOC
    #{@feedback.config.title}

    *Paths:*
    #{paths.map { |p| "\t#{p}" }.join("\n")}

    *Answers:*
    #{answers.map { |a| "\t#{a}" }.join("\n")}
  HEREDOC
end

#emojiObject



11
12
13
14
15
16
17
# File 'lib/nexmo_developer/app/services/feedback_slack_notifier.rb', line 11

def emoji
  case @feedback.sentiment
  when 'negative' then ':-1:'
  when 'positive' then ':+1:'
  else ':neutral_face:'
  end
end

#notify!Object



82
83
84
# File 'lib/nexmo_developer/app/services/feedback_slack_notifier.rb', line 82

def notify!
  @notifier.post params
end

#paramsObject



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
# File 'lib/nexmo_developer/app/services/feedback_slack_notifier.rb', line 35

def params
  @params ||= begin
    options = {
      channel: '#documentation-feedbot-comments',
      attachments: [
        {
          title: "#{emoji} #{state} #{@feedback.sentiment.upcase_first} feedback",
          title_link: admin_url,
          text: '-',
          color: slack_color,
          fields: [
            {
              title: ':link: URL',
              value: @feedback.resource.uri,
            },
            {
              title: ':hammer_and_wrench: Nexmo Developer Admin Link',
              value: admin_url,
            },
          ],
        },
      ],
    }

    if @feedback.owner.email
      options[:attachments][0][:fields] << {
        title: ':bust_in_silhouette: Author',
        value: @feedback.owner.email,
      }
    end

    options[:attachments][0][:fields] << {
      title: ':speech_balloon: Feedback',
      value: body,
    }

    if @feedback.code_language
      options[:attachments][0][:fields] << {
        title: ':computer: Code Language',
        value: @feedback.code_language,
      }
    end

    options
  end
end

#pathsObject



98
99
100
101
102
103
104
105
106
# File 'lib/nexmo_developer/app/services/feedback_slack_notifier.rb', line 98

def paths
  @paths ||= @feedback.config.paths.map.with_index do |path, path_index|
    if @feedback.path == path_index
      "*#{path['question']}*"
    else
      path['question']
    end
  end
end

#slack_colorObject



19
20
21
22
23
24
25
# File 'lib/nexmo_developer/app/services/feedback_slack_notifier.rb', line 19

def slack_color
  case @feedback.sentiment
  when 'negative' then 'danger'
  when 'positive' then 'good'
  else '#ccc'
  end
end

#stateObject



31
32
33
# File 'lib/nexmo_developer/app/services/feedback_slack_notifier.rb', line 31

def state
  @feedback.created_at == @feedback.updated_at ? 'New' : 'Updated'
end