prowl 0.1.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/README ADDED
@@ -0,0 +1,40 @@
1
+ --~~ Prowl ~~--
2
+
3
+ Ruby wrapper for http://prowl.weks.net/.
4
+ Written by August Lilleaas (http://august.lilleaas.net/).
5
+
6
+ Prowl rocks! It's a webapp and a iPhone app that works
7
+ together and lets you send push notifications via HTTP
8
+ to any iPhone that has the prowl app installed.
9
+
10
+ Prowl.send({
11
+ :application => "World Saving Project Manager",
12
+ :event => "Canceled",
13
+ :description => "It sucked, so I canceled it. Sorry :(",
14
+ :apikey => "abc123def456"
15
+ })
16
+
17
+ The user has given you its API key. That lets you
18
+ send push notifications to the users iPhone, through
19
+ the Prowl iPhone app.
20
+
21
+
22
+ --~~ Usage ~~--
23
+
24
+ Three required parameters:
25
+
26
+ :application
27
+ :event
28
+ :description
29
+
30
+ Other than that, you're free to choose from any of these
31
+ highly convenient methods.
32
+
33
+ Prowl.send({:apikey => "...", ...})
34
+ Prowl.send({:username => "...", :password => "...", ...})
35
+
36
+ p = Prowl.new("username", "password")
37
+ p.send(...)
38
+
39
+ p = Prowl.new("apikey123abc")
40
+ p.send(...)
@@ -0,0 +1,71 @@
1
+ require 'cgi'
2
+ require 'net/https'
3
+ require 'uri'
4
+
5
+ require 'prowl/api_key_handler'
6
+ require 'prowl/http_auth_handler'
7
+
8
+ 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
19
+ end
20
+
21
+ def send(params)
22
+ @handler.add(params)
23
+ end
24
+
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)
29
+ end
30
+
31
+ # Utility function to verify API keys
32
+ def self.verify(api_key)
33
+ new(api_key).valid?
34
+ end
35
+
36
+ def valid?
37
+ @handler.valid?
38
+ end
39
+ end
40
+
41
+ # For me and my good friend friend Textmate.
42
+ if __FILE__ == $0
43
+ puts "Your API key, please."
44
+ api_key = gets
45
+
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
71
+ end
@@ -0,0 +1,33 @@
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
+ u = uri.request_uri + "?" + params.map {|k, v| "#{k}=#{CGI.escape(v)}"}.join("&")
27
+ p u
28
+ request = Net::HTTP::Get.new(u)
29
+ response = http.request(request)
30
+ return response.code.to_i
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,24 @@
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
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "prowl"
3
+ s.version = "0.1.0"
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://github.com/augustl/prowl"
11
+ s.files = [
12
+ "lib/prowl.rb",
13
+ "lib/prowl/api_key_handler.rb",
14
+ "lib/prowl/http_auth_handler.rb",
15
+ "prowl.gemspec",
16
+ "README",
17
+ "test/prowl_test.rb"
18
+ ]
19
+ end
@@ -0,0 +1,43 @@
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_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")
13
+ end
14
+
15
+ # Uh.. Such a silly test.
16
+ 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")
19
+ end
20
+
21
+ def test_valid_with_api_key
22
+ Prowl::ApiKeyHandler.any_instance.expects(:perform).returns(200)
23
+ assert Prowl.verify("my api key")
24
+
25
+ Prowl::ApiKeyHandler.any_instance.expects(:perform).returns(666)
26
+ assert !Prowl.verify("my api key")
27
+ 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
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prowl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
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 +02: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
+ - lib/prowl/api_key_handler.rb
27
+ - lib/prowl/http_auth_handler.rb
28
+ - prowl.gemspec
29
+ - README
30
+ - test/prowl_test.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/augustl/prowl
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project: prowl
53
+ rubygems_version: 1.3.1
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: Wrapprer for prowl, http://prowl.weks.net/.
57
+ test_files: []
58
+