prowler 1.2.1 → 1.3.0

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,4 +1,13 @@
1
- *1.2.1 (January 27th, 2011)
1
+ *1.3.0 (January 31st, 2011)
2
+
3
+ * Add retrive_token and retrieve_api_key commands
4
+
5
+ * Allow passing of API keys to notify and verify
6
+
7
+ * Use an options hash to initialize instance
8
+
9
+
10
+ *1.2.1 (January 30th, 2011)
2
11
 
3
12
  * Correct typo in providerkey url parameter
4
13
 
data/INSTALL CHANGED
@@ -50,7 +50,7 @@ To use Prowler within your application just call the notify method, e.g.
50
50
  If you need to send to multiple accounts from within a single application you
51
51
  can create an instance of the Prowler class to override the global settings, e.g.
52
52
 
53
- prowler = Prowler.new('apikey', 'application')
53
+ prowler = Prowler.new(:application => 'application', :api_key => 'apikey')
54
54
  prowler.notify "Event", "Description"
55
55
 
56
56
  If performance is a concern then there is built in support for Delayed::Job.
data/README CHANGED
@@ -49,7 +49,7 @@ To use Prowler within your application just call the notify method, e.g.
49
49
  If you need to send to multiple accounts from within a single application you
50
50
  can create an instance of the Prowler class to override the global settings, e.g.
51
51
 
52
- prowler = Prowler.new('apikey', 'application')
52
+ prowler = Prowler.new(:application => 'application', :api_key => 'apikey')
53
53
  prowler.notify "Event", "Description"
54
54
 
55
55
  If performance is a concern then there is built in support for Delayed::Job.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.1
1
+ 1.3.0
@@ -1,6 +1,7 @@
1
1
  require 'prowler/configuration'
2
2
  require 'prowler/delayed_job'
3
3
  require 'prowler/priority'
4
+ require 'prowler/response'
4
5
  require 'prowler/version'
5
6
 
6
7
  module Prowler
@@ -8,6 +9,7 @@ module Prowler
8
9
  SERVICE_URL = "https://prowlapp.com/publicapi"
9
10
  USER_AGENT = "Prowler/#{VERSION}"
10
11
  MULTIPLE_APIKEY_COMMANDS = %w(add)
12
+ CONFIG_ATTRS = [:application, :provider_key, :api_key]
11
13
 
12
14
  class ConfigurationError < StandardError; end
13
15
 
@@ -16,75 +18,103 @@ module Prowler
16
18
  attr_accessor :application, :send_notifications #:nodoc:
17
19
 
18
20
  # Create an instance for sending to different accounts within a single Rails application
19
- # * api_key: Your API key.
20
- # * application: The name of your application.
21
- # * provider_key: Key to override the rate limit of 1000 requests per hour. (Optional)
22
- def initialize(api_key, application, provider_key = nil)
23
- @api_key, @application, @provider_key = api_key, application, provider_key
21
+ # Pass any of the following options to override the global configuration:
22
+ # * :application: The name of your application.
23
+ # * :provider_key: Key to override the rate limit of 1000 requests per hour.
24
+ # * :api_key: Your API key.
25
+ def initialize(*args)
26
+ if args.empty?
27
+ CONFIG_ATTRS.each{ |attr| send("#{attr}=".to_sym, Prowler.send(attr)) }
28
+ elsif args.first.is_a?(Hash)
29
+ CONFIG_ATTRS.each do |attr|
30
+ send("#{attr}=".to_sym, args[0][attr] || Prowler.send(attr))
31
+ end
32
+ else
33
+ @api_key, @application, @provider_key = args[0], args[1], args[2]
34
+ end
24
35
  end
25
36
 
26
37
  # Send a notification to your iPhone:
27
- # * event: The title of notification you want to send.
28
- # * message: The text of the notification message you want to send.
38
+ # * event: The title of notification you want to send.
39
+ # * message: The text of the notification message you want to send.
40
+ # * api_key: One or more API keys to be notified - uses the configured key(s) if not provided.
29
41
  #
30
42
  # The following options are supported:
31
- # * +:delayed+: Whether to use Delayed::Job to send notifications. (Optional)
32
- # * +:priority+: The priority of the notification - see Prowler::Priority. (Optional)
33
- # * +:url+: A custom url for the Prowl application to open. (Optional)
43
+ # * :delayed: Whether to use Delayed::Job to send notifications.
44
+ # * :priority: The priority of the notification - see Prowler::Priority.
45
+ # * :url: A custom url for the Prowl application to open.
34
46
  def notify(event, message, *args)
47
+ api_key = args.first.is_a?(String) || args.first.is_a?(Array) ? args.shift : self.api_key
48
+
35
49
  raise ConfigurationError, "You must provide an API key to send notifications" if api_key.nil?
36
50
  raise ConfigurationError, "You must provide an application name to send notifications" if application.nil?
37
51
 
38
- if args.first.is_a?(Hash)
39
- options = args.first
40
- options[:priority] ||= Prowler::Priority::NORMAL
41
- options[:delayed] ||= Prowler.delayed
52
+ if args.first.is_a?(Fixnum)
53
+ options = { :priority => args.shift, :delayed => args.shift || Prowler.delayed }
42
54
  else
43
- options = {
44
- :priority => args.shift || Prowler::Priority::NORMAL,
45
- :delayed => args.shift || Prowler.delayed
46
- }
55
+ options = args.last.is_a?(Hash) ? args.pop : {}
56
+ options = { :priority => Prowler::Priority::NORMAL, :delayed => Prowler.delayed }.merge(options)
47
57
  end
48
58
 
59
+ options.merge!(
60
+ :application => application, :providerkey => provider_key,
61
+ :apikey => api_key, :event => event, :description => message
62
+ )
63
+
49
64
  if options.delete(:delayed)
50
- enqueue_delayed_job(event, message, options)
65
+ enqueue_delayed_job(options)
51
66
  else
52
- perform(
53
- :add, api_key, provider_key,
54
- options.merge({
55
- :application => application,
56
- :event => event,
57
- :description => message
58
- })
59
- )
67
+ perform(:add, options, :post, Success)
60
68
  end
61
69
  end
62
70
 
63
71
  # Verify the configured API key is valid
64
- def verify
65
- raise ConfigurationError, "You must provide an API key to verify" if api_key.nil?
66
- perform(:verify, api_key, provider_key, {}, :get)
72
+ def verify(api_key = nil)
73
+ raise ConfigurationError, "You must provide an API key to verify" if api_key.nil? && self.api_key.nil?
74
+ perform(:verify, { :providerkey => provider_key, :apikey => api_key || self.api_key }, :get, Success)
75
+ end
76
+
77
+ # Retrieve a registration token and confirmation url for the initial phase
78
+ # of fetching an API key for a user. The token is valid for 24 hours.
79
+ # This API command requires the provider_key to be configured.
80
+ #
81
+ # Returns either Prowler::Token object if successful or nil if an error occurs.
82
+ def retrieve_token
83
+ raise ConfigurationError, "You must have a provider key to retrieve API keys" if provider_key.nil?
84
+ perform("retrieve/token", { :providerkey => provider_key }, :get, Token)
85
+ end
86
+
87
+ # Retrieve an API key for a user using the token provided by retrieve_token.
88
+ # This API command requires the provider_key to be configured.
89
+ # * token: Token returned by retrieve_token command.
90
+ #
91
+ # Returns either Prowler::ApiKey object if successful or nil if an error occurs.
92
+ def retrieve_api_key(token)
93
+ raise ConfigurationError, "You must have a provider key to retrieve API keys" if provider_key.nil?
94
+ perform("retrieve/apikey", { :providerkey => provider_key, :token => token }, :get, ApiKey)
67
95
  end
68
96
 
69
97
  private
70
- def perform(command, api_key, provider_key, data = {}, method = :post) #:nodoc:
71
- params = { :apikey => format_api_key(command, api_key), :providerkey => provider_key }.merge(data).delete_if { |k,v| v.nil? }
98
+ def perform(command, params = {}, method = :post, klass = Success) #:nodoc:
99
+ params[:apikey] = format_api_key(command, params[:apikey]) if params.key?(:apikey)
100
+ params.delete_if { |k,v| v.nil? }
101
+
72
102
  case method
73
103
  when :post
74
- perform_post(command, params)
104
+ perform_post(command, params, klass)
75
105
  else
76
- perform_get(command, params)
106
+ perform_get(command, params, klass)
77
107
  end
78
108
  end
79
109
 
80
- def enqueue_delayed_job(event, message, options) #:nodoc:
110
+ def enqueue_delayed_job(options) #:nodoc:
81
111
  record = Delayed::Job.enqueue(Prowler::DelayedJob.new do |job|
82
- job.api_key = api_key
83
- job.provider_key = provider_key
84
- job.application = application
85
- job.event = event
86
- job.message = message
87
- job.priority = options[:priority] || Priority::NORMAL
112
+ job.api_key = options[:apikey]
113
+ job.provider_key = options[:providerkey]
114
+ job.application = options[:application]
115
+ job.event = options[:event]
116
+ job.message = options[:description]
117
+ job.priority = options[:priority]
88
118
  job.url = options[:url]
89
119
  end)
90
120
  !record.new_record?
@@ -114,20 +144,20 @@ module Prowler
114
144
  send_notifications && Prowler.send_notifications
115
145
  end
116
146
 
117
- def perform_get(command, params) #:nodoc:
147
+ def perform_get(command, params, klass) #:nodoc:
118
148
  url = URI.parse("#{SERVICE_URL}/#{command}?#{params.map{ |k,v| %(#{URI.encode(k.to_s)}=#{URI.encode(v.to_s)}) }.join('&')}")
119
149
  request = Net::HTTP::Get.new("#{url.path}?#{url.query}", headers)
120
- perform_request(url, request)
150
+ perform_request(url, request, klass)
121
151
  end
122
152
 
123
- def perform_post(command, params) #:nodoc:
153
+ def perform_post(command, params, klass) #:nodoc:
124
154
  url = URI.parse("#{SERVICE_URL}/#{command}")
125
155
  request = Net::HTTP::Post.new(url.path, headers)
126
156
  request.form_data = params
127
- perform_request(url, request)
157
+ perform_request(url, request, klass)
128
158
  end
129
159
 
130
- def perform_request(url, request) #:nodoc:
160
+ def perform_request(url, request, klass) #:nodoc:
131
161
  http = Net::HTTP.new(url.host, url.port)
132
162
  http.use_ssl = true
133
163
  if verify_certificate?
@@ -140,15 +170,25 @@ module Prowler
140
170
  http.open_timeout = Prowler.open_timeout
141
171
  http.start do
142
172
  begin
143
- return true unless send_notifications?
144
- response = http.request(request)
145
- case response
146
- when Net::HTTPSuccess then
147
- logger.info "Prowl Success: #{response.class}"
148
- true
149
- else
150
- logger.error "Prowl Failure: #{response.class}\n#{response.body if response.respond_to? :body}"
151
- false
173
+ if send_notifications?
174
+ response = http.request(request)
175
+ case response
176
+ when Net::HTTPSuccess then
177
+ logger.info "Prowl Success: #{response.class}"
178
+ else
179
+ logger.error "Prowl Failure: #{response.class}"
180
+ klass = Error
181
+ end
182
+
183
+ unless response.body.empty?
184
+ document = REXML::Document.new(response.body)
185
+
186
+ if document && klass == Error
187
+ raise klass.new(document) if Prowler.raise_errors
188
+ elsif document
189
+ klass.new(document)
190
+ end
191
+ end
152
192
  end
153
193
  rescue TimeoutError => e
154
194
  logger.error "Timeout while contacting the Prowl server."
@@ -4,6 +4,7 @@ module Prowler
4
4
  attr_accessor :application, :send_notifications
5
5
  attr_accessor :read_timeout, :open_timeout #:nodoc:
6
6
  attr_accessor :delayed, :verify_certificate, :root_certificates
7
+ attr_accessor :raise_errors
7
8
 
8
9
  # Call this method to configure your account details in an initializer.
9
10
  def configure
@@ -18,6 +19,10 @@ module Prowler
18
19
  @delayed.nil? ? false : !!@delayed
19
20
  end
20
21
 
22
+ def raise_errors #:nodoc:
23
+ @raise_errors.nil? ? false : !!@raise_errors
24
+ end
25
+
21
26
  # Reset configuration
22
27
  def reset_configuration
23
28
  @application = @api_key = @provider_key = nil
@@ -0,0 +1,63 @@
1
+ module Prowler
2
+ class Error < StandardError
3
+ # The status code returned by the Prowl API
4
+ attr_reader :status
5
+
6
+ # The error message returned by the Prowl API
7
+ attr_reader :message
8
+
9
+ def initialize(document) #:nodoc:
10
+ error = document.elements["prowl/error"]
11
+
12
+ @status = error.attributes["code"].to_i
13
+ @message = "Prowl Failure: #{error.text}"
14
+ end
15
+ end
16
+
17
+ class Success
18
+ # The status code returned by the Prowl API - always 200
19
+ attr_reader :status
20
+
21
+ # The number of remaining requests until the counter is reset
22
+ attr_reader :remaining
23
+
24
+ # The time when the request counter will be reset
25
+ attr_reader :reset_date
26
+
27
+ def initialize(document) #:nodoc:
28
+ success = document.elements["prowl/success"]
29
+
30
+ @status = success.attributes["code"].to_i
31
+ @remaining = success.attributes["remaining"].to_i
32
+ @reset_date = Time.at(success.attributes["resetdate"].to_i)
33
+ end
34
+ end
35
+
36
+ class Token < Success
37
+ # The token returned by the Prowl API to use in a retrieve_api_key request
38
+ attr_reader :token
39
+
40
+ # The url to redirect a user to for access confirmation
41
+ attr_reader :url
42
+
43
+ def initialize(document) #:nodoc:
44
+ super(document)
45
+
46
+ retrieve = document.elements["prowl/retrieve"]
47
+ @token = retrieve.attributes["token"]
48
+ @url = retrieve.attributes["url"]
49
+ end
50
+ end
51
+
52
+ class ApiKey < Success
53
+ # The API key returned by the Prowl API
54
+ attr_reader :api_key
55
+
56
+ def initialize(document) #:nodoc:
57
+ super(document)
58
+
59
+ retrieve = document.elements["prowl/retrieve"]
60
+ @api_key = retrieve.attributes["apikey"]
61
+ end
62
+ end
63
+ end
@@ -1,3 +1,3 @@
1
1
  module Prowler
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
data/lib/prowler.rb CHANGED
@@ -32,7 +32,7 @@
32
32
  # If you need to send to multiple accounts from within a single application you
33
33
  # can create an instance of the Prowler class to override the global settings, e.g.
34
34
  #
35
- # prowler = Prowler.new('apikey', 'application')
35
+ # prowler = Prowler.new(:application => 'application', :api_key => 'apikey')
36
36
  # prowler.notify "Event", "Description", Prowler::Priority::NORMAL
37
37
  #
38
38
  # If performance is a concern then there is built in support for Delayed::Job.
@@ -57,6 +57,7 @@
57
57
 
58
58
  require 'logger'
59
59
  require 'net/https'
60
+ require 'rexml/document'
60
61
  require 'uri'
61
62
 
62
63
  require 'prowler/application'
@@ -65,31 +66,49 @@ require 'prowler/railtie' if defined?(Rails::Railtie)
65
66
  module Prowler
66
67
  class << self
67
68
  # Send a notification to your iPhone:
68
- # * event: The title of notification you want to send.
69
- # * message: The text of the notification message you want to send.
69
+ # * event: The title of notification you want to send.
70
+ # * message: The text of the notification message you want to send.
71
+ # * api_key: One or more API keys to be notified - uses the configured key(s) if not provided.
70
72
  #
71
73
  # The following options are supported:
72
- # * +:delayed+: Whether to use Delayed::Job to send notifications. (Optional)
73
- # * +:priority+: The priority of the notification - see Prowler::Priority. (Optional)
74
- # * +:url+: A custom url for the Prowl application to open. (Optional)
74
+ # * :delayed: Whether to use Delayed::Job to send notifications. (Optional)
75
+ # * :priority: The priority of the notification - see Prowler::Priority. (Optional)
76
+ # * :url: A custom url for the Prowl application to open. (Optional)
75
77
  def notify(event, message, *args)
76
- app = new(api_key, application, provider_key)
77
- app.notify(event, message, *args)
78
+ new.notify(event, message, *args)
78
79
  end
79
80
 
80
81
  # Verify the configured API key is valid
81
- def verify
82
- app = new(api_key, application, provider_key)
83
- app.verify
82
+ # * api_key: API key to be verified - uses the first configured key(s) if not provided.
83
+ def verify(api_key = nil)
84
+ new.verify(api_key)
85
+ end
86
+
87
+ # Retrieve a registration token and confirmation url for the initial phase
88
+ # of fetching an API key for a user. The token is valid for 24 hours.
89
+ # This API command requires the provider_key to be configured.
90
+ #
91
+ # Returns either Prowler::Token object if successful or nil if an error occurs.
92
+ def retrieve_token
93
+ new.retrieve_token
94
+ end
95
+
96
+ # Retrieve an API key for a user using the token provided by retrieve_token.
97
+ # This API command requires the provider_key to be configured.
98
+ # * token: Token returned by retrieve_token command.
99
+ #
100
+ # Returns either Prowler::ApiKey object if successful or nil if an error occurs.
101
+ def retrieve_api_key(token)
102
+ new.retrieve_api_key(token)
84
103
  end
85
104
 
86
105
  # Create an instance for sending to different accounts within a single Rails application
87
- # * api_key: Your API key.
88
- # * application: The name of your application.
89
- # * provider_key: Key to override the rate limit of 1000 requests per hour. (Optional)
90
- def new(api_key, application, provider_key = nil)
91
- Prowler::Application.new(api_key, application, provider_key)
106
+ # Pass any of the following options to override the global configuration:
107
+ # * :application: The name of your application.
108
+ # * :provider_key: Key to override the rate limit of 1000 requests per hour.
109
+ # * :api_key: Your API key.
110
+ def new(*args)
111
+ Prowler::Application.new(*args)
92
112
  end
93
113
  end
94
114
  end
95
-
data/prowler.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{prowler}
8
- s.version = "1.2.1"
8
+ s.version = "1.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrew White"]
12
- s.date = %q{2011-01-30}
12
+ s.date = %q{2011-01-31}
13
13
  s.description = %q{A plugin/gem that provides access to the Prowl API (http://prowlapp.com). Works with Rails 2 or 3 as well as any other Ruby web frameworks or in your own scripts.}
14
14
  s.email = %q{andyw@pixeltrix.co.uk}
15
15
  s.extra_rdoc_files = [
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "lib/prowler/delayed_job.rb",
31
31
  "lib/prowler/priority.rb",
32
32
  "lib/prowler/railtie.rb",
33
+ "lib/prowler/response.rb",
33
34
  "lib/prowler/tasks.rb",
34
35
  "lib/prowler/version.rb",
35
36
  "prowler.gemspec",
data/test/prowler_test.rb CHANGED
@@ -26,10 +26,12 @@ class ProwlerTest < Test::Unit::TestCase
26
26
  Prowler.configure do |config|
27
27
  config.api_key = "apikey"
28
28
  config.application = "application"
29
+ config.provider_key = "providerkey"
29
30
  end
30
31
 
31
32
  assert_equal "apikey", Prowler.api_key
32
33
  assert_equal "application", Prowler.application
34
+ assert_equal "providerkey", Prowler.provider_key
33
35
  end
34
36
 
35
37
  should "not set a default application" do
@@ -40,10 +42,38 @@ class ProwlerTest < Test::Unit::TestCase
40
42
  assert_equal nil, Prowler.api_key
41
43
  end
42
44
 
43
- should "override class configuration when using an instance" do
44
- prowler = Prowler.new("apikey2", "application2")
45
- assert_equal "apikey2", prowler.api_key
46
- assert_equal "application2", prowler.application
45
+ context "when using an instance" do
46
+ setup do
47
+ Prowler.reset_configuration
48
+
49
+ Prowler.configure do |config|
50
+ config.api_key = "apikey"
51
+ config.application = "application"
52
+ config.provider_key = "providerkey"
53
+ end
54
+ end
55
+
56
+ should "inheirit config from global scope" do
57
+ prowler = Prowler.new
58
+ assert_equal "apikey", prowler.api_key
59
+ assert_equal "application", prowler.application
60
+ assert_equal "providerkey", prowler.provider_key
61
+ end
62
+
63
+ should "override application config" do
64
+ prowler = Prowler.new(:application => "application2")
65
+ assert_equal "application2", prowler.application
66
+ end
67
+
68
+ should "override provider_key config" do
69
+ prowler = Prowler.new(:provider_key => "providerkey2")
70
+ assert_equal "providerkey2", prowler.provider_key
71
+ end
72
+
73
+ should "override api_key config" do
74
+ prowler = Prowler.new(:api_key => "apikey2")
75
+ assert_equal "apikey2", prowler.api_key
76
+ end
47
77
  end
48
78
  end
49
79
 
@@ -59,12 +89,12 @@ class ProwlerTest < Test::Unit::TestCase
59
89
  should "raise an exception if API key not configured" do
60
90
  Prowler.reset_configuration
61
91
  assert_raises Prowler::ConfigurationError do
62
- Prowler.notify("Event", "Description")
92
+ Prowler.notify "Event", "Description"
63
93
  end
64
94
 
65
95
  prowler = Prowler.new(nil, nil)
66
96
  assert_raises Prowler::ConfigurationError do
67
- prowler.notify("Event", "Description")
97
+ prowler.notify "Event", "Description"
68
98
  end
69
99
  end
70
100
 
@@ -74,12 +104,12 @@ class ProwlerTest < Test::Unit::TestCase
74
104
  config.api_key = "apikey"
75
105
  end
76
106
  assert_raises Prowler::ConfigurationError do
77
- Prowler.notify("Event", "Description", Prowler::Priority::NORMAL)
107
+ Prowler.notify "Event", "Description"
78
108
  end
79
109
 
80
110
  prowler = Prowler.new("apikey", nil)
81
111
  assert_raises Prowler::ConfigurationError do
82
- prowler.notify("Event", "Description", Prowler::Priority::NORMAL)
112
+ prowler.notify "Event", "Description"
83
113
  end
84
114
  end
85
115
 
@@ -89,7 +119,7 @@ class ProwlerTest < Test::Unit::TestCase
89
119
  Net::HTTP.any_instance.expects(:ca_file=).with(File.expand_path('config/cacert.pem', RAILS_ROOT))
90
120
 
91
121
  Prowler.send_notifications = false
92
- Prowler.notify("Event Name", "Message Text")
122
+ Prowler.notify "Event Name", "Message Text"
93
123
  end
94
124
 
95
125
  should "not verify SSL certificates if verification is turned off" do
@@ -98,61 +128,199 @@ class ProwlerTest < Test::Unit::TestCase
98
128
 
99
129
  Prowler.send_notifications = false
100
130
  Prowler.verify_certificate = false
101
- Prowler.notify("Event Name", "Message Text")
131
+ Prowler.notify "Event Name", "Message Text"
102
132
  end
103
133
 
104
134
  should "not send notifications if send_notifications is false" do
135
+ stub_request(:post, "#{Prowler::SERVICE_URL}/add")
136
+
105
137
  Prowler.send_notifications = false
106
- assert_not_notified Prowler, "Event Name", "Message Text"
138
+ Prowler.notify "Event Name", "Message Text"
139
+
140
+ assert_not_requested :post, "#{Prowler::SERVICE_URL}/add"
107
141
  end
108
142
 
109
143
  should "send multiple API keys if configured" do
144
+ stub_request :post, "#{Prowler::SERVICE_URL}/add"
145
+
110
146
  Prowler.api_key = %w(apikey1 apikey2)
111
- assert_notified Prowler, "Event Name", "Message Text"
147
+ Prowler.notify "Event Name", "Message Text"
148
+
149
+ assert_requested :post, "#{Prowler::SERVICE_URL}/add", :body => {
150
+ :application => "Application Name",
151
+ :apikey => "apikey1,apikey2",
152
+ :event => "Event Name",
153
+ :description => "Message Text",
154
+ :priority => Prowler::Priority::NORMAL.to_s
155
+ }
112
156
  end
113
157
 
114
158
  should "log a successful response" do
159
+ stub_request(:post, "#{Prowler::SERVICE_URL}/add")
160
+
115
161
  Prowler.logger.expects(:info).with("Prowl Success: Net::HTTPOK")
116
- assert_notified Prowler, "Event Name", "Message Text"
162
+ Prowler.notify "Event Name", "Message Text"
163
+
164
+ assert_requested :post, "#{Prowler::SERVICE_URL}/add", :body => {
165
+ :application => "Application Name",
166
+ :apikey => "apikey",
167
+ :event => "Event Name",
168
+ :description => "Message Text",
169
+ :priority => Prowler::Priority::NORMAL.to_s
170
+ }
117
171
  end
118
172
 
119
173
  should "log an error response" do
120
- Prowler.logger.expects(:error).with("Prowl Failure: Net::HTTPInternalServerError\n")
121
- assert_notified Prowler, "Event Name", "Message Text" do |request|
122
- request.to_return(:status => 500, :body => "", :headers => {})
123
- end
174
+ stub_request(:post, "#{Prowler::SERVICE_URL}/add").
175
+ to_return(:status => 500, :headers => {}, :body => "")
176
+
177
+ Prowler.logger.expects(:error).with("Prowl Failure: Net::HTTPInternalServerError")
178
+ Prowler.notify "Event Name", "Message Text"
179
+
180
+ assert_requested :post, "#{Prowler::SERVICE_URL}/add", :body => {
181
+ :application => "Application Name",
182
+ :apikey => "apikey",
183
+ :event => "Event Name",
184
+ :description => "Message Text",
185
+ :priority => Prowler::Priority::NORMAL.to_s
186
+ }
124
187
  end
125
188
 
126
189
  should "delay sending if configured globally" do
127
- Prowler.delayed = true
128
- assert_delayed Prowler, "Event Name", "Message Text"
129
- end
190
+ Prowler::Application.any_instance.expects(:enqueue_delayed_job).with(
191
+ :application => "Application Name",
192
+ :providerkey => nil,
193
+ :apikey => "apikey",
194
+ :event => "Event Name",
195
+ :description => "Message Text",
196
+ :priority => Prowler::Priority::NORMAL
197
+ )
130
198
 
131
- should "delay sending using deprecated parameter" do
132
- Prowler.delayed = false
133
- assert_delayed Prowler, "Event Name", "Message Text", Prowler::Priority::NORMAL, true
199
+ Prowler.delayed = true
200
+ Prowler.notify "Event Name", "Message Text"
134
201
  end
135
202
 
136
203
  should "delay sending using options" do
204
+ Prowler::Application.any_instance.expects(:enqueue_delayed_job).with(
205
+ :application => "Application Name",
206
+ :providerkey => nil,
207
+ :apikey => "apikey",
208
+ :event => "Event Name",
209
+ :description => "Message Text",
210
+ :priority => Prowler::Priority::NORMAL
211
+ )
212
+
137
213
  Prowler.delayed = false
138
- assert_delayed Prowler, "Event Name", "Message Text", :delayed => true
214
+ Prowler.notify "Event Name", "Message Text", :delayed => true
139
215
  end
140
216
 
141
217
  should "send a custom url" do
142
- assert_notified Prowler, "Event Name", "Message Text", :url => "http://www.pixeltrix.co.uk"
143
- end
218
+ stub_request(:post, "#{Prowler::SERVICE_URL}/add")
144
219
 
145
- should "send with a high priority using deprecated parameter" do
146
- assert_notified Prowler, "Event Name", "Message Text", Prowler::Priority::HIGH
220
+ Prowler.notify "Event Name", "Message Text", :url => "http://www.pixeltrix.co.uk"
221
+
222
+ assert_requested :post, "#{Prowler::SERVICE_URL}/add", :body => {
223
+ :application => "Application Name",
224
+ :apikey => "apikey",
225
+ :event => "Event Name",
226
+ :description => "Message Text",
227
+ :priority => Prowler::Priority::NORMAL.to_s,
228
+ :url => "http://www.pixeltrix.co.uk"
229
+ }
147
230
  end
148
231
 
149
232
  should "send with a high priority using options" do
150
- assert_notified Prowler, "Event Name", "Message Text", :priority => Prowler::Priority::HIGH
233
+ stub_request(:post, "#{Prowler::SERVICE_URL}/add")
234
+
235
+ Prowler.notify "Event Name", "Message Text", :priority => Prowler::Priority::HIGH
236
+
237
+ assert_requested :post, "#{Prowler::SERVICE_URL}/add", :body => {
238
+ :application => "Application Name",
239
+ :apikey => "apikey",
240
+ :event => "Event Name",
241
+ :description => "Message Text",
242
+ :priority => Prowler::Priority::HIGH.to_s
243
+ }
151
244
  end
152
245
 
153
246
  should "send the provider key if configured" do
247
+ stub_request(:post, "#{Prowler::SERVICE_URL}/add")
248
+
154
249
  Prowler.provider_key = "providerkey"
155
- assert_notified Prowler, "Event Name", "Message Text"
250
+ Prowler.notify "Event Name", "Message Text"
251
+
252
+ assert_requested :post, "#{Prowler::SERVICE_URL}/add", :body => {
253
+ :application => "Application Name",
254
+ :providerkey => "providerkey",
255
+ :apikey => "apikey",
256
+ :event => "Event Name",
257
+ :description => "Message Text",
258
+ :priority => Prowler::Priority::NORMAL.to_s
259
+ }
260
+ end
261
+
262
+ should "allow passing the API key as a parameter" do
263
+ stub_request(:post, "#{Prowler::SERVICE_URL}/add")
264
+
265
+ Prowler.notify "Event Name", "Message Text", "apikey1"
266
+
267
+ assert_requested :post, "#{Prowler::SERVICE_URL}/add", :body => {
268
+ :application => "Application Name",
269
+ :apikey => "apikey1",
270
+ :event => "Event Name",
271
+ :description => "Message Text",
272
+ :priority => Prowler::Priority::NORMAL.to_s
273
+ }
274
+ end
275
+
276
+ should "allow passing multiple API keys as a parameter" do
277
+ stub_request(:post, "#{Prowler::SERVICE_URL}/add")
278
+
279
+ Prowler.notify "Event Name", "Message Text", %w[apikey1 apikey2]
280
+
281
+ assert_requested :post, "#{Prowler::SERVICE_URL}/add", :body => {
282
+ :application => "Application Name",
283
+ :apikey => "apikey1,apikey2",
284
+ :event => "Event Name",
285
+ :description => "Message Text",
286
+ :priority => Prowler::Priority::NORMAL.to_s
287
+ }
288
+ end
289
+
290
+ context "when raise_errors is true" do
291
+ should "raise an exception if the Prowl API returns an error" do
292
+ stub_request(:post, "#{Prowler::SERVICE_URL}/add").
293
+ to_return(:status => 500, :headers => {}, :body => <<-EOF
294
+ <?xml version="1.0" encoding="UTF-8"?>
295
+ <prowl>
296
+ <error code="500">Internal Server Error</error>
297
+ </prowl>
298
+ EOF
299
+ )
300
+
301
+ assert_raises(Prowler::Error) do
302
+ Prowler.raise_errors = true
303
+ Prowler.notify "Event Name", "Message Text"
304
+ end
305
+ end
306
+ end
307
+
308
+ context "when raise_errors is false" do
309
+ should "not raise an exception if the the Prowl API returns an error" do
310
+ stub_request(:post, "#{Prowler::SERVICE_URL}/add").
311
+ to_return(:status => 500, :headers => {}, :body => <<-EOF
312
+ <?xml version="1.0" encoding="UTF-8"?>
313
+ <prowl>
314
+ <error code="500">Internal Server Error</error>
315
+ </prowl>
316
+ EOF
317
+ )
318
+
319
+ assert_nothing_raised do
320
+ Prowler.raise_errors = false
321
+ Prowler.notify "Event Name", "Message Text"
322
+ end
323
+ end
156
324
  end
157
325
  end
158
326
 
@@ -178,13 +346,21 @@ class ProwlerTest < Test::Unit::TestCase
178
346
  end
179
347
 
180
348
  should "only verify the first API key" do
349
+ stub_request :get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey1"
350
+
181
351
  Prowler.api_key = %w(apikey1 apikey2)
182
- assert_verified Prowler, "apikey1"
352
+ Prowler.verify
353
+
354
+ assert_requested :get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey1"
183
355
  end
184
356
 
185
357
  should "not send notifications if send_notifications is false" do
358
+ stub_request :get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey"
359
+
186
360
  Prowler.send_notifications = false
187
- assert_not_verified Prowler
361
+ Prowler.verify
362
+
363
+ assert_not_requested :get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey"
188
364
  end
189
365
 
190
366
  should "verify SSL certificates by default" do
@@ -206,108 +382,175 @@ class ProwlerTest < Test::Unit::TestCase
206
382
  end
207
383
 
208
384
  should "send the provider key if configured" do
385
+ stub_request :get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey&providerkey=providerkey"
386
+
209
387
  Prowler.provider_key = "providerkey"
210
- assert_verified Prowler, "apikey", "providerkey"
388
+ Prowler.verify
389
+
390
+ assert_requested :get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey&providerkey=providerkey"
211
391
  end
212
- end
213
392
 
214
- private
215
- def verify_url
216
- "#{Prowler::SERVICE_URL}/verify"
393
+ should "allow passing an API key as a parameter" do
394
+ stub_request :get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey1"
395
+ Prowler.verify "apikey1"
396
+ assert_requested :get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey1"
217
397
  end
218
398
 
219
- def build_url(config, api_key, provider_key)
220
- if provider_key
221
- "#{verify_url}?providerkey=#{provider_key}&apikey=#{api_key}"
222
- else
223
- "#{verify_url}?apikey=#{api_key}"
224
- end
399
+ should "only verify the first API key passed as a parameter" do
400
+ stub_request :get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey1"
401
+ Prowler.verify %w[apikey1 apikey2]
402
+ assert_requested :get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey1"
225
403
  end
226
404
 
227
- def assert_verified(config, api_key = "apikey", provider_key = nil, &block)
228
- request = stub_request(:get, build_url(config, api_key, provider_key))
229
- request.with(:headers => { "Accept" => "*/*" })
230
- request.with(:headers => { "User-Agent" => Prowler::USER_AGENT })
405
+ context "when raise_errors is true" do
406
+ should "raise an exception if the Prowl API returns an error" do
407
+ stub_request(:get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey").
408
+ to_return(:status => 500, :headers => {}, :body => <<-EOF
409
+ <?xml version="1.0" encoding="UTF-8"?>
410
+ <prowl>
411
+ <error code="500">Internal Server Error</error>
412
+ </prowl>
413
+ EOF
414
+ )
231
415
 
232
- if block_given?
233
- yield request
234
- else
235
- request.to_return(:status => 200, :body => "", :headers => {})
416
+ assert_raises(Prowler::Error) do
417
+ Prowler.raise_errors = true
418
+ Prowler.verify
419
+ end
236
420
  end
237
-
238
- config.verify
239
- assert_requested :get, build_url(config, api_key, provider_key)
240
421
  end
241
422
 
242
- def assert_not_verified(config, api_key = "apikey", provider_key = nil)
243
- config.verify
244
- assert_not_requested :get, build_url(config, api_key, provider_key)
423
+ context "when raise_errors is false" do
424
+ should "not raise an exception if the the Prowl API returns an error" do
425
+ stub_request(:get, "#{Prowler::SERVICE_URL}/verify?apikey=apikey").
426
+ to_return(:status => 500, :headers => {}, :body => <<-EOF
427
+ <?xml version="1.0" encoding="UTF-8"?>
428
+ <prowl>
429
+ <error code="500">Internal Server Error</error>
430
+ </prowl>
431
+ EOF
432
+ )
433
+
434
+ assert_nothing_raised do
435
+ Prowler.raise_errors = false
436
+ Prowler.verify
437
+ end
438
+ end
245
439
  end
440
+ end
246
441
 
247
- def notify_url
248
- "#{Prowler::SERVICE_URL}/add"
442
+ context "Retrieving an API key" do
443
+ setup do
444
+ Prowler.reset_configuration
445
+ Prowler.configure do |config|
446
+ config.provider_key = "providerkey"
447
+ end
249
448
  end
250
449
 
251
- def build_request(config, event, message, options)
252
- body = {}
253
- if options.is_a?(Hash)
254
- body["priority"] = (options[:priority] || Prowler::Priority::NORMAL).to_s
255
- body["url"] = options[:url] if options[:url]
256
- else
257
- body["priority"] = (options || Prowler::Priority::NORMAL).to_s
258
- end
259
- body["application"] = config.application
260
- body["event"] = event
261
- body["apikey"] = Array(config.api_key).join(",")
262
- body["description"] = message
263
- body["providerkey"] = config.provider_key if config.provider_key
264
- body
265
- end
266
-
267
- def assert_notified(config, event, message, options = {}, &block)
268
- body = build_request(config, event, message, options)
269
-
270
- request = stub_request(:post, notify_url)
271
- request.with(:headers => { "Accept" => "*/*" })
272
- request.with(:headers => { "User-Agent" => Prowler::USER_AGENT })
273
- request.with(:headers => { "Content-Type" => "application/x-www-form-urlencoded" })
274
- request.with(:body => body)
275
-
276
- if block_given?
277
- yield request
278
- else
279
- request.to_return(:status => 200, :body => "", :headers => {})
450
+ should "raise an exception if the provider key is not configured" do
451
+ Prowler.reset_configuration
452
+ assert_raises Prowler::ConfigurationError do
453
+ Prowler.retrieve_token
280
454
  end
281
455
 
282
- config.notify event, message, options
283
- assert_requested :post, notify_url, :body => body
456
+ assert_raises Prowler::ConfigurationError do
457
+ Prowler.retrieve_api_key("token")
458
+ end
284
459
  end
285
460
 
286
- def assert_not_notified(config, event, message, options = {})
287
- config.notify event, message
288
- assert_not_requested :post, notify_url, :body => build_request(config, event, message, options)
461
+ should "request a token and return a Prowler::Token instance when successful" do
462
+ stub_request(:get, "#{Prowler::SERVICE_URL}/retrieve/token?providerkey=providerkey").
463
+ to_return(:status => 200, :headers => {}, :body => <<-EOF
464
+ <?xml version="1.0" encoding="UTF-8"?>
465
+ <prowl>
466
+ <success code="200" remaining="999" resetdate="1234567890" />
467
+ <retrieve token="token" url="https://prowlapp.com/retrieve.php?token=token" />
468
+ </prowl>
469
+ EOF
470
+ )
471
+
472
+ response = Prowler.retrieve_token
473
+
474
+ assert_requested :get, "#{Prowler::SERVICE_URL}/retrieve/token?providerkey=providerkey"
475
+ assert_instance_of Prowler::Token, response
476
+ assert_equal response.remaining, 999
477
+ assert_equal response.reset_date, Time.at(1234567890)
478
+ assert_equal response.token, "token"
479
+ assert_equal response.url, "https://prowlapp.com/retrieve.php?token=token"
289
480
  end
290
481
 
291
- def assert_delayed(config, event, message, *args, &block)
292
- if args.first.is_a?(Hash) || args.empty?
293
- options = args.first || {}
294
- delayed = options.delete(:delayed)
295
- options[:priority] ||= Prowler::Priority::NORMAL
482
+ should "request an API key and return a Prowler::ApiKey instance when successful" do
483
+ stub_request(:get, "#{Prowler::SERVICE_URL}/retrieve/apikey?providerkey=providerkey&token=token").
484
+ to_return(:status => 200, :headers => {}, :body => <<-EOF
485
+ <?xml version="1.0" encoding="UTF-8"?>
486
+ <prowl>
487
+ <success code="200" remaining="999" resetdate="1234567890" />
488
+ <retrieve apikey="apikey" />
489
+ </prowl>
490
+ EOF
491
+ )
296
492
 
297
- Prowler::Application.any_instance.expects(:enqueue_delayed_job).with("Event Name", "Message Text", options)
298
- config.notify event, message, options.merge(:delayed => delayed)
299
- else
300
- priority = args.shift
301
- delayed = args.shift
302
- options = { :priority => priority }
493
+ response = Prowler.retrieve_api_key("token")
303
494
 
304
- Prowler::Application.any_instance.expects(:enqueue_delayed_job).with("Event Name", "Message Text", options)
305
- config.notify event, message, priority, delayed
495
+ assert_requested :get, "#{Prowler::SERVICE_URL}/retrieve/apikey?providerkey=providerkey&token=token"
496
+ assert_instance_of Prowler::ApiKey, response
497
+ assert_equal response.remaining, 999
498
+ assert_equal response.reset_date, Time.at(1234567890)
499
+ assert_equal response.api_key, "apikey"
500
+ end
501
+
502
+ end
503
+
504
+ context "Using deprecated API" do
505
+ context "Prowler configuration" do
506
+ setup do
507
+ Prowler.reset_configuration
306
508
  end
307
509
 
308
- if delayed
309
- assert_not_requested :post, notify_url, :body => build_request(config, event, message, options)
510
+ should "override class configuration when using an instance" do
511
+ prowler = Prowler.new("apikey2", "application2", "providerkey2")
512
+ assert_equal "apikey2", prowler.api_key
513
+ assert_equal "application2", prowler.application
514
+ assert_equal "providerkey2", prowler.provider_key
310
515
  end
311
516
  end
312
517
 
518
+ context "Sending a notification" do
519
+ setup do
520
+ Prowler.reset_configuration
521
+ Prowler.configure do |config|
522
+ config.api_key = "apikey"
523
+ config.application = "Application Name"
524
+ end
525
+ end
526
+
527
+ should "delay sending using parameter" do
528
+ Prowler::Application.any_instance.expects(:enqueue_delayed_job).with(
529
+ :application => "Application Name",
530
+ :providerkey => nil,
531
+ :apikey => "apikey",
532
+ :event => "Event Name",
533
+ :description => "Message Text",
534
+ :priority => Prowler::Priority::NORMAL
535
+ )
536
+
537
+ Prowler.delayed = false
538
+ Prowler.notify "Event Name", "Message Text", Prowler::Priority::NORMAL, true
539
+ end
540
+
541
+ should "send with a high priority using parameter" do
542
+ stub_request(:post, "#{Prowler::SERVICE_URL}/add")
543
+
544
+ Prowler.notify "Event Name", "Message Text", Prowler::Priority::HIGH
545
+
546
+ assert_requested :post, "#{Prowler::SERVICE_URL}/add", :body => {
547
+ :application => "Application Name",
548
+ :apikey => "apikey",
549
+ :event => "Event Name",
550
+ :description => "Message Text",
551
+ :priority => Prowler::Priority::HIGH.to_s
552
+ }
553
+ end
554
+ end
555
+ end
313
556
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prowler
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 2
9
- - 1
10
- version: 1.2.1
8
+ - 3
9
+ - 0
10
+ version: 1.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew White
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-30 00:00:00 +00:00
18
+ date: 2011-01-31 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -42,6 +42,7 @@ files:
42
42
  - lib/prowler/delayed_job.rb
43
43
  - lib/prowler/priority.rb
44
44
  - lib/prowler/railtie.rb
45
+ - lib/prowler/response.rb
45
46
  - lib/prowler/tasks.rb
46
47
  - lib/prowler/version.rb
47
48
  - prowler.gemspec