peterc-prowl 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +54 -0
- data/Rakefile +6 -0
- data/lib/prowl.rb +85 -0
- data/peterc-prowl.gemspec +18 -0
- data/test/prowl_test.rb +36 -0
- metadata +59 -0
data/README
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--~~ Prowl ~~--
|
2
|
+
|
3
|
+
Ruby wrapper for http://prowl.weks.net/.
|
4
|
+
Written by August Lilleaas (http://august.lilleaas.net/).
|
5
|
+
|
6
|
+
Note: `send` has been renamed to `add`.
|
7
|
+
|
8
|
+
|
9
|
+
--~~ Installation ~~--
|
10
|
+
|
11
|
+
gem install prowl
|
12
|
+
|
13
|
+
or
|
14
|
+
|
15
|
+
gem install augustl-prowl --source=http://gems.github.com/
|
16
|
+
|
17
|
+
Code available here: http://github.com/augustl/ruby-prowl/tree
|
18
|
+
|
19
|
+
--~~ About ~~--
|
20
|
+
|
21
|
+
Prowl rocks! It's a webapp and a iPhone app that works
|
22
|
+
together and lets you send push notifications via HTTP
|
23
|
+
to any iPhone that has the prowl app installed.
|
24
|
+
|
25
|
+
Prowl.add(
|
26
|
+
:apikey => "api key abc123def456",
|
27
|
+
:application => "World Saving Project Manager",
|
28
|
+
:event => "Canceled",
|
29
|
+
:description => "It sucked, so I canceled it. Sorry :("
|
30
|
+
)
|
31
|
+
|
32
|
+
The user has given you its API key. That lets you
|
33
|
+
send push notifications to the users iPhone, through
|
34
|
+
the Prowl iPhone app.
|
35
|
+
|
36
|
+
|
37
|
+
--~~ Usage ~~--
|
38
|
+
|
39
|
+
Four required parameters:
|
40
|
+
|
41
|
+
:apikey (string / array of strings)
|
42
|
+
:application (string)
|
43
|
+
:event (string)
|
44
|
+
:description (string)
|
45
|
+
|
46
|
+
You can use Prowl.add, or create instances of Prowl
|
47
|
+
manually.
|
48
|
+
|
49
|
+
Prowl.add(:apikey => "123abc", :application => "Foo", ...)
|
50
|
+
Prowl.verify("apikey")
|
51
|
+
|
52
|
+
p = Prowl.new(:apikey => "apikey123abc", :application => "FooApp")
|
53
|
+
p.valid?
|
54
|
+
p.add(:event => "It's valid", ...)
|
data/Rakefile
ADDED
data/lib/prowl.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'net/https'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
|
6
|
+
class Prowl
|
7
|
+
class MissingAPIKey < RuntimeError; end
|
8
|
+
class TooManyAPIKeys < RuntimeError; end
|
9
|
+
class PriorityOutOfRange < RuntimeError; end
|
10
|
+
|
11
|
+
API_URL = "https://prowl.weks.net:443/publicapi"
|
12
|
+
MAX_API_KEYS = 5
|
13
|
+
PRIORITY_RANGE = -2..2
|
14
|
+
|
15
|
+
def initialize(defaults = {})
|
16
|
+
@defaults = defaults
|
17
|
+
end
|
18
|
+
|
19
|
+
def add(params = {})
|
20
|
+
perform("add", params)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Modify this instance's defaults
|
24
|
+
def defaults(params)
|
25
|
+
@defaults = @defaults.merge(params)
|
26
|
+
end
|
27
|
+
|
28
|
+
def valid?
|
29
|
+
@valid ||= (perform("verify") == 200)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Utility function that creates an instance and sends a prowl
|
33
|
+
def self.add(params = {})
|
34
|
+
new(params).add
|
35
|
+
end
|
36
|
+
|
37
|
+
# Utility function to verify API keys
|
38
|
+
def self.verify(apikey)
|
39
|
+
new({:apikey => apikey}).valid?
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
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)
|
51
|
+
raise MissingAPIKey
|
52
|
+
end
|
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
|
+
|
59
|
+
if params[:priority] && !PRIORITY_RANGE.include?(params[:priority])
|
60
|
+
raise PriorityOutOfRange
|
61
|
+
end
|
62
|
+
|
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
|
67
|
+
|
68
|
+
uri = URI.parse("#{API_URL}/#{action}")
|
69
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
70
|
+
http.use_ssl = true
|
71
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
72
|
+
|
73
|
+
request = Net::HTTP::Get.new(uri.request_uri + "?" + params.map {|k, v| "#{k}=#{CGI.escape(v.to_s)}"}.join("&"))
|
74
|
+
response = http.request(request)
|
75
|
+
return response.code.to_i
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# For me and my good friend friend Textmate.
|
80
|
+
if __FILE__ == $0
|
81
|
+
api_key = "change me"
|
82
|
+
|
83
|
+
p Prowl.add(:apikey => api_key, :application => "Fishes", :event => "silly", :description => "Awwawaw.", :priority => 1)
|
84
|
+
p Prowl.verify(api_key)
|
85
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "peterc-prowl"
|
3
|
+
s.version = "0.1.3"
|
4
|
+
s.date = "2009-07-08"
|
5
|
+
s.authors = ["August Lilleaas"]
|
6
|
+
s.email = "augustlilleaas@gmail.com"
|
7
|
+
s.rubyforge_project = "prowl"
|
8
|
+
s.has_rdoc = true
|
9
|
+
s.summary = "Wrapprer for prowl, http://prowl.weks.net/."
|
10
|
+
s.homepage = "http://prowl.rubyforge.org/"
|
11
|
+
s.files = [
|
12
|
+
"lib/prowl.rb",
|
13
|
+
"peterc-prowl.gemspec",
|
14
|
+
"Rakefile",
|
15
|
+
"README",
|
16
|
+
"test/prowl_test.rb"
|
17
|
+
]
|
18
|
+
end
|
data/test/prowl_test.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
$LOAD_PATH << File.expand_path("#{File.dirname(__FILE__)}/../lib")
|
4
|
+
require 'prowl'
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'mocha'
|
8
|
+
|
9
|
+
class ProwlTest < Test::Unit::TestCase
|
10
|
+
def test_not_without_an_api_key
|
11
|
+
assert_raises(Prowl::MissingAPIKey) { Prowl.add(:apikey => nil) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_invalid_priority
|
15
|
+
assert_raises(Prowl::PriorityOutOfRange) { Prowl.add(:apikey => "foo", :priority => 10)}
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_valid_api_key
|
19
|
+
Prowl.any_instance.expects(:perform).returns(200)
|
20
|
+
assert_equal 200, Prowl.add(:apikey => "my api key", :application => "Fishes", :event => "Silly", :description => "Blah")
|
21
|
+
end
|
22
|
+
|
23
|
+
# Uh.. Such a silly test.
|
24
|
+
def test_invalid_api_key
|
25
|
+
Prowl.any_instance.expects(:perform).returns(666)
|
26
|
+
assert_equal 666, Prowl.add(:apikey => "my api key", :application => "Fishes", :event => "Silly", :description => "Blah")
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_verify_with_api_key
|
30
|
+
Prowl.any_instance.expects(:perform).returns(200)
|
31
|
+
assert Prowl.verify("my api key")
|
32
|
+
|
33
|
+
Prowl.any_instance.expects(:perform).returns(666)
|
34
|
+
assert !Prowl.verify("my api key")
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: peterc-prowl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- August Lilleaas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-08 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: augustlilleaas@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/prowl.rb
|
26
|
+
- peterc-prowl.gemspec
|
27
|
+
- Rakefile
|
28
|
+
- README
|
29
|
+
- test/prowl_test.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://prowl.rubyforge.org/
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project: prowl
|
54
|
+
rubygems_version: 1.3.5
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Wrapprer for prowl, http://prowl.weks.net/.
|
58
|
+
test_files: []
|
59
|
+
|