Class: Integrity::Notifier::Hooks

Inherits:
Notifier::Base
  • Object
show all
Defined in:
lib/integrity/notifier/hooks.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(build, config = {}) ⇒ Hooks

Returns a new instance of Hooks.



22
23
24
25
# File 'lib/integrity/notifier/hooks.rb', line 22

def initialize(build, config={})
  @uri = config.delete("uri")
  super
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



8
9
10
# File 'lib/integrity/notifier/hooks.rb', line 8

def uri
  @uri
end

Class Method Details

.to_hamlObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/integrity/notifier/hooks.rb', line 10

def self.to_haml
  <<-HAML
%p.normal
  %label{ :for => "hooks_notifier_uri" } Send to
  To send to multiple URLs, separate the URLs with three stars (***)
  %input.text#hooks_notifier_uri{                        |
    :name => "notifiers[Hooks][uri]",                    |
    :type => "text",                                     |
    :value => config["uri"] || ""}                       |
  HAML
end

Instance Method Details

#deliver!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/integrity/notifier/hooks.rb', line 27

def deliver!
  uri.split("***").each do |target|
    begin
      Timeout.timeout(5) do
        Integrity.log("POSTing to #{target}")
        response = Net::HTTP.post_form(URI.parse(target), {"payload" => payload.to_json})
        case response
        when Net::HTTPSuccess
          Integrity.log("POST to #{target} successful. Response: #{response.body}")
          true
        else
          # TODO: N retries
          Integrity.log("POST to #{target} failed (#{response.code} #{response.msg}). Response: \n#{response.body}")
          false
        end
      end
    rescue Errno::ECONNREFUSED
      Integrity.log("Connection refused for #{target}")
    rescue TimeoutError
      Integrity.log("Timed out POST to #{target}")
    end
  end
end

#payloadObject



51
52
53
54
55
56
57
58
# File 'lib/integrity/notifier/hooks.rb', line 51

def payload
  {
    "project"       => build.project.name,
    "short_message" => short_message,
    "full_message"  => full_message,
    "url"           => build_url
  }
end