prowl 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +12 -10
- data/lib/prowl.rb +31 -11
- data/prowl.gemspec +1 -1
- data/test/prowl_test.rb +4 -4
- metadata +1 -1
data/README
CHANGED
@@ -22,11 +22,12 @@
|
|
22
22
|
together and lets you send push notifications via HTTP
|
23
23
|
to any iPhone that has the prowl app installed.
|
24
24
|
|
25
|
-
Prowl.add(
|
25
|
+
Prowl.add(
|
26
|
+
:apikey => "api key abc123def456",
|
26
27
|
:application => "World Saving Project Manager",
|
27
28
|
:event => "Canceled",
|
28
29
|
:description => "It sucked, so I canceled it. Sorry :("
|
29
|
-
|
30
|
+
)
|
30
31
|
|
31
32
|
The user has given you its API key. That lets you
|
32
33
|
send push notifications to the users iPhone, through
|
@@ -35,18 +36,19 @@
|
|
35
36
|
|
36
37
|
--~~ Usage ~~--
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
:
|
41
|
-
:
|
42
|
-
:
|
39
|
+
Four required parameters:
|
40
|
+
|
41
|
+
:apikey (string / array of strings)
|
42
|
+
:application (string)
|
43
|
+
:event (string)
|
44
|
+
:description (string)
|
43
45
|
|
44
46
|
You can use Prowl.add, or create instances of Prowl
|
45
47
|
manually.
|
46
48
|
|
47
|
-
Prowl.add(
|
49
|
+
Prowl.add(:apikey => "123abc", :application => "Foo", ...)
|
48
50
|
Prowl.verify("apikey")
|
49
51
|
|
50
|
-
p = Prowl.new("apikey123abc")
|
52
|
+
p = Prowl.new(:apikey => "apikey123abc", :application => "FooApp")
|
51
53
|
p.valid?
|
52
|
-
p.add(...)
|
54
|
+
p.add(:event => "It's valid", ...)
|
data/lib/prowl.rb
CHANGED
@@ -5,45 +5,65 @@ require 'uri'
|
|
5
5
|
|
6
6
|
class Prowl
|
7
7
|
class MissingAPIKey < RuntimeError; end
|
8
|
+
class TooManyAPIKeys < RuntimeError; end
|
8
9
|
class PriorityOutOfRange < RuntimeError; end
|
9
10
|
|
10
11
|
API_URL = "https://prowl.weks.net:443/publicapi"
|
12
|
+
MAX_API_KEYS = 5
|
11
13
|
PRIORITY_RANGE = -2..2
|
12
14
|
|
13
|
-
def initialize(
|
14
|
-
@
|
15
|
+
def initialize(defaults = {})
|
16
|
+
@defaults = defaults
|
15
17
|
end
|
16
18
|
|
17
19
|
def add(params = {})
|
18
20
|
perform("add", params)
|
19
21
|
end
|
20
22
|
|
23
|
+
# Modify this instance's defaults
|
24
|
+
def defaults(params)
|
25
|
+
@defaults = @defaults.merge(params)
|
26
|
+
end
|
27
|
+
|
21
28
|
def valid?
|
22
29
|
@valid ||= (perform("verify") == 200)
|
23
30
|
end
|
24
31
|
|
25
32
|
# Utility function that creates an instance and sends a prowl
|
26
|
-
def self.add(
|
27
|
-
new(
|
33
|
+
def self.add(params = {})
|
34
|
+
new(params).add
|
28
35
|
end
|
29
36
|
|
30
37
|
# Utility function to verify API keys
|
31
|
-
def self.verify(
|
32
|
-
new(
|
38
|
+
def self.verify(apikey)
|
39
|
+
new({:apikey => apikey}).valid?
|
33
40
|
end
|
34
41
|
|
35
42
|
private
|
36
43
|
|
37
|
-
def perform(action, params)
|
38
|
-
|
44
|
+
def perform(action, params = {})
|
45
|
+
# Merge the default params with any custom ones
|
46
|
+
unless !@defaults
|
47
|
+
params = @defaults.merge(params)
|
48
|
+
end
|
49
|
+
|
50
|
+
if !params[:apikey] || (params[:apikey].is_a?(Array) && params[:apikey].size < 1)
|
39
51
|
raise MissingAPIKey
|
40
52
|
end
|
41
53
|
|
54
|
+
# Raise an exception if we're trying to use more API keys than allowed for this action
|
55
|
+
if params[:apikey].is_a?(Array) && ((action == "verify" && params[:apikey].size > 1) || params[:apikey].size > MAX_API_KEYS)
|
56
|
+
raise TooManyAPIKeys
|
57
|
+
end
|
58
|
+
|
42
59
|
if params[:priority] && !PRIORITY_RANGE.include?(params[:priority])
|
43
60
|
raise PriorityOutOfRange
|
44
61
|
end
|
45
62
|
|
46
|
-
|
63
|
+
# If there are multiple API Keys in an array, merge them into a comma-delimited string
|
64
|
+
if params[:apikey].is_a?(Array)
|
65
|
+
params[:apikey] = params[:apikey].collect{|v| v + ","}.to_s.chop.chop
|
66
|
+
end
|
47
67
|
|
48
68
|
uri = URI.parse("#{API_URL}/#{action}")
|
49
69
|
http = Net::HTTP.new(uri.host, uri.port)
|
@@ -60,6 +80,6 @@ end
|
|
60
80
|
if __FILE__ == $0
|
61
81
|
api_key = "change me"
|
62
82
|
|
63
|
-
p Prowl.add(api_key, :application => "Fishes", :event => "silly", :description => "Awwawaw.", :priority => 1)
|
64
|
-
p Prowl.
|
83
|
+
p Prowl.add(:apikey => api_key, :application => "Fishes", :event => "silly", :description => "Awwawaw.", :priority => 1)
|
84
|
+
p Prowl.verify(api_key)
|
65
85
|
end
|
data/prowl.gemspec
CHANGED
data/test/prowl_test.rb
CHANGED
@@ -8,22 +8,22 @@ require 'mocha'
|
|
8
8
|
|
9
9
|
class ProwlTest < Test::Unit::TestCase
|
10
10
|
def test_not_without_an_api_key
|
11
|
-
assert_raises(Prowl::MissingAPIKey) { Prowl.add(nil) }
|
11
|
+
assert_raises(Prowl::MissingAPIKey) { Prowl.add(:apikey => nil) }
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_invalid_priority
|
15
|
-
assert_raises(Prowl::PriorityOutOfRange) { Prowl.add("foo", :priority => 10)}
|
15
|
+
assert_raises(Prowl::PriorityOutOfRange) { Prowl.add(:apikey => "foo", :priority => 10)}
|
16
16
|
end
|
17
17
|
|
18
18
|
def test_valid_api_key
|
19
19
|
Prowl.any_instance.expects(:perform).returns(200)
|
20
|
-
assert_equal 200, Prowl.add("my api key", :application => "Fishes", :event => "Silly", :description => "Blah")
|
20
|
+
assert_equal 200, Prowl.add(:apikey => "my api key", :application => "Fishes", :event => "Silly", :description => "Blah")
|
21
21
|
end
|
22
22
|
|
23
23
|
# Uh.. Such a silly test.
|
24
24
|
def test_invalid_api_key
|
25
25
|
Prowl.any_instance.expects(:perform).returns(666)
|
26
|
-
assert_equal 666, Prowl.add("my api key", :application => "Fishes", :event => "Silly", :description => "Blah")
|
26
|
+
assert_equal 666, Prowl.add(:apikey => "my api key", :application => "Fishes", :event => "Silly", :description => "Blah")
|
27
27
|
end
|
28
28
|
|
29
29
|
def test_verify_with_api_key
|