augustl-prowl 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,53 @@
1
+ --~~ Prowl ~~--
2
+
3
+ Ruby wrapper for http://prowl.weks.net/.
4
+ Written by August Lilleaas (http://august.lilleaas.net/).
5
+
6
+
7
+ --~~ Installation ~~--
8
+
9
+ gem install prowl
10
+
11
+ or
12
+
13
+ gem install augustl-prowl --source=http://gems.github.com/
14
+
15
+ Code available here: http://github.com/augustl/ruby-prowl/tree
16
+
17
+ --~~ About ~~--
18
+
19
+ Prowl rocks! It's a webapp and a iPhone app that works
20
+ together and lets you send push notifications via HTTP
21
+ to any iPhone that has the prowl app installed.
22
+
23
+ Prowl.send({
24
+ :application => "World Saving Project Manager",
25
+ :event => "Canceled",
26
+ :description => "It sucked, so I canceled it. Sorry :(",
27
+ :apikey => "abc123def456"
28
+ })
29
+
30
+ The user has given you its API key. That lets you
31
+ send push notifications to the users iPhone, through
32
+ the Prowl iPhone app.
33
+
34
+
35
+ --~~ Usage ~~--
36
+
37
+ Three required parameters:
38
+
39
+ :application
40
+ :event
41
+ :description
42
+
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(...)
51
+
52
+ p = Prowl.new("apikey123abc")
53
+ p.send(...)
@@ -0,0 +1,6 @@
1
+ task :upload_rubyforge_site do
2
+ content = "<pre>#{File.read('README')}</pre>"
3
+ File.open("index.html", "w+") {|f| f << content }
4
+ `scp index.html leethal@rubyforge.org:/var/www/gforge-projects/prowl`
5
+ File.delete("index.html")
6
+ end
@@ -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,31 @@
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
@@ -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,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "prowl"
3
+ s.version = "0.1.1"
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
+ "lib/prowl/api_key_handler.rb",
14
+ "lib/prowl/http_auth_handler.rb",
15
+ "prowl.gemspec",
16
+ "Rakefile",
17
+ "README",
18
+ "test/prowl_test.rb"
19
+ ]
20
+ 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,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: augustl-prowl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
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 -07: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
+ - Rakefile
30
+ - README
31
+ - test/prowl_test.rb
32
+ has_rdoc: true
33
+ homepage: http://prowl.rubyforge.org/
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.2.0
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: Wrapprer for prowl, http://prowl.weks.net/.
58
+ test_files: []
59
+