Class: Snoopit::Notifiers::Http
Instance Attribute Summary
#configuration, #klass, #name
Instance Method Summary
collapse
Constructor Details
#initialize(config = nil) ⇒ Http
The name ‘http’ is used by the Snooper to identify type of notifier to create The empty constructor is used to initialize the class when loaded dynamically After loaded dynamically the method set_config is called by the base class to set the configuration
14
15
16
17
|
# File 'lib/snoopit/notifiers/http.rb', line 14
def initialize(config=nil)
super config, 'http'
@api_key = config['api-key']
end
|
Instance Method Details
#build_request(uri, found, notify_params) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/snoopit/notifiers/http.rb', line 48
def build_request(uri, found, notify_params)
request = Net::HTTP::Post.new(uri.path)
request.basic_auth notify_params['user'], notify_params['password'] unless notify_params['user'].nil?
request.body = found.to_json
request
request
end
|
#get_conn(uri) ⇒ Object
56
57
58
59
60
61
62
63
|
# File 'lib/snoopit/notifiers/http.rb', line 56
def get_conn(uri)
case uri.scheme
when 'http'
set_http(uri)
when 'https'
set_https(uri)
end
end
|
#notify(found, notify_params) ⇒ Object
19
20
21
22
|
# File 'lib/snoopit/notifiers/http.rb', line 19
def notify(found, notify_params)
raise ArgumentError.new 'URL parameter must be set' if notify_params['url'].nil?
post_it found, notify_params, URI(notify_params['url'])
end
|
#post(conn, request) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/snoopit/notifiers/http.rb', line 30
def post(conn, request)
response = conn.request(request)
case response.code
when 200..299, 300..399
logger.debug 'Posted notification to: ' + response.uri.to_s
logger.debug.ap found
when 400..599
logger.warn 'Failed to send notification to uri: ' + response.uri.to_s
logger.warn 'Failed to send notification to uri: ' + response.message
end
rescue => e
Snoopit.logger.warn 'Failed to send notification to uri: ' + response.uri.to_s unless response.nil?
Snoopit.logger.warn 'Failed to send notification to uri: ' + e.message
Snoopit.logger.warn 'Failed to send notification to uri: ' + e.backtrace.join("\n")
ensure
conn.finish if conn.started?
end
|
#post_it(found, notify_params, uri) ⇒ Object
24
25
26
27
28
|
# File 'lib/snoopit/notifiers/http.rb', line 24
def post_it(found, notify_params, uri)
conn = get_conn uri
request = build_request uri, found, notify_params
post conn, request
end
|
76
77
78
79
|
# File 'lib/snoopit/notifiers/http.rb', line 76
def (request)
request['Content-Type'] = 'application/json'
request['Authorization'] = 'Token token=' + @api_key unless @api_key.nil?
end
|
#set_http(uri) ⇒ Object
65
66
67
|
# File 'lib/snoopit/notifiers/http.rb', line 65
def set_http(uri)
Net::HTTP.new uri.host, uri.port
end
|
#set_https(uri) ⇒ Object
69
70
71
72
73
74
|
# File 'lib/snoopit/notifiers/http.rb', line 69
def set_https(uri)
conn = Net::HTTP.new uri.host, uri.port
conn.use_ssl = true
conn.verify_mode = OpenSSL::SSL::VERIFY_PEER
conn
end
|