Module: Herald::Watcher::Notifier::Post

Defined in:
lib/herald/notifiers/post.rb

Overview

note most of this code is duplicated between ping and post

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

lazy-load net/http when this Module is used



9
10
11
12
13
14
# File 'lib/herald/notifiers/post.rb', line 9

def self.extended(base)
  Herald.lazy_load('net/http')
  class << base
    attr_accessor :uri
  end
end

Instance Method Details

#notify(item) ⇒ Object



49
50
51
52
53
# File 'lib/herald/notifiers/post.rb', line 49

def notify(item)
  @uris.each do |uri|
    Net::HTTP.post_form(uri, { "title" => item.title, "message" => item.message }.merge(item.data))
  end
end

#parse_options(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/herald/notifiers/post.rb', line 16

def parse_options(options)
  @uris = []
  uris = Array(options.delete(:uri) || options.delete(:url) || options.delete(:uris) || options.delete(:urls))
  if uris.empty?
    raise ArgumentError, ":uri for :ping action not specified"
  end
  uris.each do |uri|
    begin
      uri = URI.parse(uri)
      # if URI lib can't resolve a protocol (because it was missing from string)
      if uri.class == URI::Generic
        uri = URI.parse("http://#{uri.path}")
      end
      # add trailing slash if nil path. path() will return nil
      # if uri passed was a domain missing a trailing slash. net/http's post
      # methods require the trailing slash to be present otherwise they fail
      uri.path = "/" if uri.path.empty?
      @uris << uri
    rescue URI::InvalidURIError
      raise ArgumentError, ":uri for :ping action invalid"
    end
  end
end

#testObject



40
41
42
43
44
45
46
47
# File 'lib/herald/notifiers/post.rb', line 40

def test
  @uris.each do |uri|
    response = Net::HTTP.new(uri.host).head('/')
    return if response.kind_of?(Net::HTTPOK)
    # TODO raise custom error types
    raise "#{response.code} status code returned for URI #{uri}. 200 code expected"
  end
end