pixeltrix-prowler 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ *1.0.3 (July 9th, 2009)
2
+
3
+ * Add verify method for configured API key
4
+
5
+ * Use POST to send notifications
6
+
7
+
1
8
  *1.0.2 (July 8th, 2009)
2
9
 
3
10
  * Switch to new API using apikey and deprecate old API
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.0.3
data/lib/prowler.rb CHANGED
@@ -44,6 +44,11 @@ require 'net/https'
44
44
 
45
45
  module Prowler
46
46
 
47
+ API_PATH = "/publicapi/add"
48
+ DEPRECATED_API_PATH = "/api/add_notification.php?application=%s&event=%s&description=%s"
49
+ VERIFY_PATH = "/publicapi/verify?apikey=%s"
50
+ USER_AGENT = "Prowler/1.0.3"
51
+
47
52
  module Priority
48
53
  VERY_LOW = -2
49
54
  MODERATE = -1
@@ -98,14 +103,6 @@ module Prowler
98
103
  !@application.nil? && (!@api_key.nil? || !(@username.nil? || @password.nil?))
99
104
  end
100
105
 
101
- def path(*params) #:nodoc:
102
- sprintf("/publicapi/add?apikey=%s&priority=%d&application=%s&event=%s&description=%s", *params)
103
- end
104
-
105
- def deprecated_path(*params) #:nodoc:
106
- sprintf("/api/add_notification.php?application=%s&event=%s&description=%s", *params)
107
- end
108
-
109
106
  # Returns the default logger or a logger that prints to STDOUT.
110
107
  def logger
111
108
  ActiveRecord::Base.logger
@@ -125,14 +122,15 @@ module Prowler
125
122
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
126
123
  http.start do
127
124
  headers = {
128
- 'User-Agent' => 'ProwlScript/1.0'
125
+ 'User-Agent' => USER_AGENT
129
126
  }
130
127
  http.read_timeout = 5 # seconds
131
128
  http.open_timeout = 2 # seconds
132
129
  if api_key
133
- request = Net::HTTP::Get.new(path(api_key, priority, URI.escape(application), URI.escape(event), URI.escape(message)), headers)
130
+ request = Net::HTTP::Post.new(API_PATH, headers)
131
+ request.set_form_data({ 'apikey' => api_key, 'priority' => priority, 'application' => application, 'event' => event, 'description' => message })
134
132
  else
135
- request = Net::HTTP::Get.new(deprecated_path(URI.escape(application), URI.escape(event), URI.escape(message)), headers)
133
+ request = Net::HTTP::Get.new(sprintf(DEPRECATED_API_PATH, URI.escape(application), URI.escape(event), URI.escape(message)), headers)
136
134
  request.basic_auth(username, password)
137
135
  end
138
136
  response = begin
@@ -151,5 +149,36 @@ module Prowler
151
149
  end
152
150
  end
153
151
  end
152
+
153
+ # Verify the configured API key is valid
154
+ def verify
155
+ raise RuntimeError, "Prowler needs to be configured first before using it" unless api_key
156
+
157
+ http = Net::HTTP.new(host, port)
158
+ http.use_ssl = secure
159
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
160
+ http.start do
161
+ headers = {
162
+ 'User-Agent' => USER_AGENT
163
+ }
164
+ http.read_timeout = 5 # seconds
165
+ http.open_timeout = 2 # seconds
166
+ request = Net::HTTP::Get.new(sprintf(VERIFY_PATH, api_key), headers)
167
+ response = begin
168
+ http.request(request) if send_notifications?
169
+ rescue TimeoutError => e
170
+ logger.error "Timeout while contacting the Prowl server."
171
+ nil
172
+ end
173
+ case response
174
+ when Net::HTTPSuccess then
175
+ logger.info "Prowl Success: #{response.class}"
176
+ true
177
+ else
178
+ logger.error "Prowl Failure: #{response.class}\n#{response.body if response.respond_to? :body}"
179
+ false
180
+ end
181
+ end
182
+ end
154
183
  end
155
184
  end
data/prowler.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{prowler}
5
- s.version = "1.0.2"
5
+ s.version = "1.0.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Andrew White"]
9
- s.date = %q{2009-07-08}
9
+ s.date = %q{2009-07-09}
10
10
  s.description = %q{A simple wrapper class that provides basic access to the Prowl API.}
11
11
  s.email = %q{andyw@pixeltrix.co.uk}
12
12
  s.extra_rdoc_files = [
data/test/prowler_test.rb CHANGED
@@ -105,13 +105,6 @@ class ProwlerTest < Test::Unit::TestCase
105
105
  Prowler.send_notifications = false
106
106
  end
107
107
 
108
- should "encode the url parameters" do
109
- expectation = Prowler.expects(:path)
110
- expectation.with("apikey", 0, "Application%20Name", "Event%20Name", "Message%20Text")
111
- expectation.returns("/publicapi/add?apikey=apikey&priority=0application=Application%20Name&event=Event%20Name&description=Message%20Text")
112
- Prowler.notify("Event Name", "Message Text", Prowler::Priority::NORMAL)
113
- end
114
-
115
108
  should "not verify SSL certificates" do
116
109
  Net::HTTP.any_instance.expects(:use_ssl=).with(true)
117
110
  Net::HTTP.any_instance.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixeltrix-prowler
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew White
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-08 00:00:00 -07:00
12
+ date: 2009-07-09 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15