aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/notifiers/hipchat.rb
blob: 33030ea367d03d7622e506e449d2eb0d4e2cff73 (plain)
1
2
3
4
5
6
7
8
9
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
105
106
require 'mauve/notifiers/debug'
require 'net/https'
require 'cgi'
require 'uri'

module Mauve
  module Notifiers
    module Hipchat
      class Default
        attr_accessor :token
        attr_reader   :name

        def initialize(name)
          @name = name
        end

        def logger
          @logger ||= Log4r::Logger.new self.class.to_s.sub(/::Default$/, '')
        end

        attr_reader :gateway

        def gateway=(uri)
          @gateway = URI.parse(uri)
        end

        def send_alert(destination, alert, all_alerts, conditions = {})
          msg = prepare_message(destination, alert, all_alerts, conditions)

          colour = case alert.level
                   when :urgent
                     'red'
                   when :normal
                     'yellow'
                   else
                     'green'
          end

          opts = {
            'color'   => colour,
            'message' => msg,
            'notify'  => true
          }

          uri = @gateway.dup

          #
          # If the destination is an email, it is a user
          #
          if destination =~ /@/
            uri.path = '/v2/user/' + CGI.escape(destination) + '/message'
            opts['message_type'] = 'text'
          else
            uri.path = '/v2/room/' + CGI.escape(destination) + '/notification'
            opts['message_type'] = 'html'
          end

          opts = opts.to_json

          uri.query = 'auth_token=' + CGI.escape(token)

          http = Net::HTTP.new(uri.host, uri.port)

          if uri.port == 443
            http.use_ssl = true
            http.verify_mode = OpenSSL::SSL::VERIFY_PEER
          end

          response = http.post(uri.request_uri, opts,
                               'Content-Type' => 'application/json',
                               'Content-Length' => opts.length.to_s)

          if response.is_a?(Net::HTTPSuccess)
            #
            # Woo -- return true!
            #
            true
          else
            logger.warn("Failed with code #{response.code}: #{response.message}")
            false
          end
        end

        protected

        def prepare_message(destination, alert, all_alerts, conditions = {})
          was_suppressed = conditions[:was_suppressed] || false
          will_suppress  = conditions[:will_suppress]  || false

          if destination =~ /@/
            template_file = File.join(File.dirname(__FILE__), 'templates', 'hipchat.txt.erb')
          else
            template_file = File.join(File.dirname(__FILE__), 'templates', 'hipchat.html.erb')
          end

          txt = if File.exist?(template_file)
                  ERB.new(File.read(template_file)).result(binding).chomp
                else
                  logger.error("Could not find #{template_file} template")
                  alert.to_s
          end
        end
      end
    end
  end
end