prowl 0.1.1 → 0.1.2

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/README CHANGED
@@ -3,6 +3,8 @@
3
3
  Ruby wrapper for http://prowl.weks.net/.
4
4
  Written by August Lilleaas (http://august.lilleaas.net/).
5
5
 
6
+ Note: `send` has been renamed to `add`.
7
+
6
8
 
7
9
  --~~ Installation ~~--
8
10
 
@@ -20,11 +22,10 @@
20
22
  together and lets you send push notifications via HTTP
21
23
  to any iPhone that has the prowl app installed.
22
24
 
23
- Prowl.send({
25
+ Prowl.add("api key abc123def456", {
24
26
  :application => "World Saving Project Manager",
25
27
  :event => "Canceled",
26
- :description => "It sucked, so I canceled it. Sorry :(",
27
- :apikey => "abc123def456"
28
+ :description => "It sucked, so I canceled it. Sorry :("
28
29
  })
29
30
 
30
31
  The user has given you its API key. That lets you
@@ -40,14 +41,12 @@
40
41
  :event
41
42
  :description
42
43
 
43
- Other than that, you're free to choose from any of these
44
- highly convenient methods.
45
-
46
- Prowl.send({:apikey => "...", ...})
47
- Prowl.send({:username => "...", :password => "...", ...})
48
-
49
- p = Prowl.new("username", "password")
50
- p.send(...)
44
+ You can use Prowl.add, or create instances of Prowl
45
+ manually.
51
46
 
47
+ Prowl.add("apikey", :application => "Foo", ...)
48
+ Prowl.verify("apikey")
49
+
52
50
  p = Prowl.new("apikey123abc")
53
- p.send(...)
51
+ p.valid?
52
+ p.add(...)
@@ -2,30 +2,29 @@ require 'cgi'
2
2
  require 'net/https'
3
3
  require 'uri'
4
4
 
5
- require 'prowl/api_key_handler'
6
- require 'prowl/http_auth_handler'
7
5
 
8
6
  class Prowl
9
- # new('username', 'password') or new('apikeyhere').
10
- def initialize(*args)
11
- case args.length
12
- when 1 # api key
13
- @handler = Prowl::ApiKeyHandler.new(*args)
14
- when 2 # username/password
15
- @handler = Prowl::HttpAuthHandler.new(*args)
16
- else
17
- raise ArgumentError
18
- end
7
+ class MissingAPIKey < RuntimeError; end
8
+ class PriorityOutOfRange < RuntimeError; end
9
+
10
+ API_URL = "https://prowl.weks.net:443/publicapi"
11
+ PRIORITY_RANGE = -2..2
12
+
13
+ def initialize(api_key)
14
+ @api_key = api_key
15
+ end
16
+
17
+ def add(params = {})
18
+ perform("add", params)
19
19
  end
20
20
 
21
- def send(params)
22
- @handler.add(params)
21
+ def valid?
22
+ @valid ||= (perform("verify") == 200)
23
23
  end
24
24
 
25
25
  # Utility function that creates an instance and sends a prowl
26
- def self.send(*args)
27
- params = args.pop
28
- new(*args).send(params)
26
+ def self.add(api_key, params = {})
27
+ new(api_key).add(params)
29
28
  end
30
29
 
31
30
  # Utility function to verify API keys
@@ -33,39 +32,34 @@ class Prowl
33
32
  new(api_key).valid?
34
33
  end
35
34
 
36
- def valid?
37
- @handler.valid?
35
+ private
36
+
37
+ def perform(action, params)
38
+ if !@api_key
39
+ raise MissingAPIKey
40
+ end
41
+
42
+ if params[:priority] && !PRIORITY_RANGE.include?(params[:priority])
43
+ raise PriorityOutOfRange
44
+ end
45
+
46
+ params[:apikey] = @api_key
47
+
48
+ uri = URI.parse("#{API_URL}/#{action}")
49
+ http = Net::HTTP.new(uri.host, uri.port)
50
+ http.use_ssl = true
51
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
52
+
53
+ request = Net::HTTP::Get.new(uri.request_uri + "?" + params.map {|k, v| "#{k}=#{CGI.escape(v.to_s)}"}.join("&"))
54
+ response = http.request(request)
55
+ return response.code.to_i
38
56
  end
39
57
  end
40
58
 
41
59
  # For me and my good friend friend Textmate.
42
60
  if __FILE__ == $0
43
- puts "Your API key, please."
44
- api_key = gets
61
+ api_key = "change me"
45
62
 
46
- if api_key
47
- api_key.chomp!
48
- p Prowl.send(api_key, :application => "Fishes", :event => "silly", :description => "Awwawaw.")
49
- end
50
-
51
-
52
- puts "Test verification?"
53
- api_key = gets
54
-
55
- if api_key
56
- api_key.chomp!
57
- p Prowl.new(api_key).valid?
58
- end
59
-
60
- puts "Your username, please."
61
- username = gets.chomp
62
-
63
- puts "Your password, please."
64
- password = gets.chomp
65
-
66
- if username
67
- username.chomp!
68
- password.chomp!
69
- p Prowl.send(username, password, :application => "Fishes", :event => "silly", :description => "Awwawaw.")
70
- end
63
+ p Prowl.add(api_key, :application => "Fishes", :event => "silly", :description => "Awwawaw.", :priority => 1)
64
+ p Prowl.new(api_key).valid?
71
65
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "prowl"
3
- s.version = "0.1.1"
3
+ s.version = "0.1.2"
4
4
  s.date = "2009-07-08"
5
5
  s.authors = ["August Lilleaas"]
6
6
  s.email = "augustlilleaas@gmail.com"
@@ -10,8 +10,6 @@ Gem::Specification.new do |s|
10
10
  s.homepage = "http://prowl.rubyforge.org/"
11
11
  s.files = [
12
12
  "lib/prowl.rb",
13
- "lib/prowl/api_key_handler.rb",
14
- "lib/prowl/http_auth_handler.rb",
15
13
  "prowl.gemspec",
16
14
  "Rakefile",
17
15
  "README",
@@ -7,37 +7,30 @@ require 'rubygems'
7
7
  require 'mocha'
8
8
 
9
9
  class ProwlTest < Test::Unit::TestCase
10
+ def test_not_without_an_api_key
11
+ assert_raises(Prowl::MissingAPIKey) { Prowl.add(nil) }
12
+ end
13
+
14
+ def test_invalid_priority
15
+ assert_raises(Prowl::PriorityOutOfRange) { Prowl.add("foo", :priority => 10)}
16
+ end
17
+
10
18
  def test_valid_api_key
11
- Prowl::ApiKeyHandler.any_instance.expects(:perform).returns(200)
12
- assert_equal 200, Prowl.send("my api key", :application => "Fishes", :event => "Silly", :description => "Blah")
19
+ Prowl.any_instance.expects(:perform).returns(200)
20
+ assert_equal 200, Prowl.add("my api key", :application => "Fishes", :event => "Silly", :description => "Blah")
13
21
  end
14
22
 
15
23
  # Uh.. Such a silly test.
16
24
  def test_invalid_api_key
17
- Prowl::ApiKeyHandler.any_instance.expects(:perform).returns(666)
18
- assert_equal 666, Prowl.send("my api key", :application => "Fishes", :event => "Silly", :description => "Blah")
25
+ Prowl.any_instance.expects(:perform).returns(666)
26
+ assert_equal 666, Prowl.add("my api key", :application => "Fishes", :event => "Silly", :description => "Blah")
19
27
  end
20
28
 
21
- def test_valid_with_api_key
22
- Prowl::ApiKeyHandler.any_instance.expects(:perform).returns(200)
29
+ def test_verify_with_api_key
30
+ Prowl.any_instance.expects(:perform).returns(200)
23
31
  assert Prowl.verify("my api key")
24
32
 
25
- Prowl::ApiKeyHandler.any_instance.expects(:perform).returns(666)
33
+ Prowl.any_instance.expects(:perform).returns(666)
26
34
  assert !Prowl.verify("my api key")
27
35
  end
28
-
29
- def test_http_auth
30
- Prowl::HttpAuthHandler.any_instance.expects(:add).returns(200)
31
- assert_equal 200, Prowl.send("username", "password", :application => "Fishes", :event => "Silly", :description => "Blah")
32
- end
33
-
34
- # Also uh..
35
- def test_failed_http_auth
36
- Prowl::HttpAuthHandler.any_instance.expects(:add).returns(666)
37
- assert_equal 666, Prowl.send("username", "password", :application => "Fishes", :event => "Silly", :description => "Blah")
38
- end
39
-
40
- def test_valid_with_username_and_password
41
- assert_raises(RuntimeError) { Prowl.new("username", "password").valid? }
42
- end
43
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prowl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - August Lilleaas
@@ -23,14 +23,14 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - lib/prowl.rb
26
- - lib/prowl/api_key_handler.rb
27
- - lib/prowl/http_auth_handler.rb
28
26
  - prowl.gemspec
29
27
  - Rakefile
30
28
  - README
31
29
  - test/prowl_test.rb
32
30
  has_rdoc: true
33
31
  homepage: http://prowl.rubyforge.org/
32
+ licenses: []
33
+
34
34
  post_install_message:
35
35
  rdoc_options: []
36
36
 
@@ -51,9 +51,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  requirements: []
52
52
 
53
53
  rubyforge_project: prowl
54
- rubygems_version: 1.3.1
54
+ rubygems_version: 1.3.5
55
55
  signing_key:
56
- specification_version: 2
56
+ specification_version: 3
57
57
  summary: Wrapprer for prowl, http://prowl.weks.net/.
58
58
  test_files: []
59
59
 
@@ -1,31 +0,0 @@
1
- class Prowl
2
- class ApiKeyHandler
3
- API_URL = "https://prowl.weks.net:443/publicapi"
4
-
5
- def initialize(api_key)
6
- @api_key = api_key
7
- end
8
-
9
- def valid?
10
- @valid ||= (perform("verify", :apikey => @api_key) == 200)
11
- end
12
-
13
- def add(params)
14
- perform("add", params)
15
- end
16
-
17
- private
18
-
19
- def perform(action, params)
20
- uri = URI.parse("#{API_URL}/#{action}")
21
- http = Net::HTTP.new(uri.host, uri.port)
22
- http.use_ssl = true
23
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
24
-
25
- params[:apikey] = @api_key
26
- request = Net::HTTP::Get.new(uri.request_uri + "?" + params.map {|k, v| "#{k}=#{CGI.escape(v)}"}.join("&"))
27
- response = http.request(request)
28
- return response.code.to_i
29
- end
30
- end
31
- end
@@ -1,24 +0,0 @@
1
- class Prowl
2
- class HttpAuthHandler
3
- API_URL = URI.parse("https://prowl.weks.net:443/api/add_notification.php")
4
-
5
- def initialize(username, password)
6
- @username, @password = username, password
7
- end
8
-
9
- def valid?
10
- raise RuntimeError, "The API doesn't provide a method for determining if a username/password is valid."
11
- end
12
-
13
- def add(params)
14
- http = Net::HTTP.new(API_URL.host, API_URL.port)
15
- http.use_ssl = true
16
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
17
-
18
- request = Net::HTTP::Get.new(API_URL.request_uri + "?" + params.map {|k, v| "#{k}=#{CGI.escape(v)}"}.join("&"))
19
- request.basic_auth @username, @password
20
- response = http.request(request)
21
- return response.code.to_i
22
- end
23
- end
24
- end