pubsubhubbub 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = Ruby / Asynchronous PubSubHubbub Client
2
+
3
+ EventMachine based / asynchronous PubSubHubbub client. Supports posting single or multiple URLs.
4
+
5
+ Full Spec: http://code.google.com/p/pubsubhubbub
6
+
7
+ HTTP PubSub: Webhooks & PubSubHubbub: http://www.igvita.com/2009/06/29/http-pubsub-webhooks-pubsubhubbub
8
+
9
+ == Simple client example
10
+
11
+ EventMachine.run {
12
+ pub = EventMachine::PubSubHubbub.new('http://pubsubhubbub.appspot.com/publish').publish "http://www.test.com/"
13
+
14
+ pub.callback { puts "Successfully notified hub." }
15
+ pub.errback { puts "Uh oh, something broke: #{pub.response}" }
16
+ }
17
+
18
+ == Posting multiple URL's
19
+
20
+ EventMachine.run {
21
+ feeds = ["http://www.test.com", "http://www.test.com/2"]
22
+ pub = EventMachine::PubSubHubbub.new('http://pubsubhubbub.appspot.com/publish').publish feeds
23
+
24
+ pub.callback { puts "Successfully notified hub." }
25
+ pub.errback { puts "Uh oh, something broke: #{pub.response}" }
26
+ }
27
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,10 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require "rubygems"
4
+ require "eventmachine"
5
+ require "em-http"
6
+ require "cgi"
7
+
8
+ %w[ client ].each do |file|
9
+ require "pubsubhubbub/#{file}"
10
+ end
@@ -0,0 +1,60 @@
1
+ # #--
2
+ # Copyright (C)2009 Ilya Grigorik
3
+ #
4
+ # You can redistribute this under the terms of the Ruby
5
+ # #--
6
+
7
+ module EventMachine
8
+ class PubSubHubbub
9
+ include EventMachine::Deferrable
10
+
11
+ HEADERS = {"User-Agent" => "PubSubHubbub Ruby", "Content-Type" => "application/x-www-form-urlencoded"}
12
+
13
+ def initialize(hub)
14
+ @hub = hub.kind_of?(URI) ? hub : URI::parse(hub)
15
+ end
16
+
17
+ def publish(*feeds)
18
+ data = feeds.flatten.collect do |feed|
19
+ {'hub.url' => feed, 'hub.mode' => 'publish'}.to_params
20
+ end.join("&")
21
+
22
+ r = EventMachine::HttpRequest.new(@hub).post :body => data, :head => HEADERS
23
+ r.callback {
24
+ if r.response_header.status == 204
25
+ succeed r
26
+ else
27
+ fail r
28
+ end
29
+ }
30
+
31
+ r.errback { fail }
32
+ r
33
+ end
34
+
35
+ # These command will work only if the callback URL supports confirmation.
36
+ def subscribe(feed, callback, options = {}); command('subscribe', feed, callback, options); end
37
+ def unsubscribe(feed, callback, options = {}); command('unsubscribe', feed, callback, options); end
38
+
39
+ private
40
+
41
+ def command(cmd, feed, callback, options)
42
+ options['hub.verify'] ||= "sync"
43
+
44
+ params = {'hub.topic' => feed, 'hub.mode' => cmd, 'hub.callback' => callback}.merge(options).to_params
45
+ r = EventMachine::HttpRequest.new(@hub).post :body => params, :head => HEADERS
46
+
47
+ r.callback {
48
+ if r.response_header.status == 204
49
+ succeed r
50
+ else
51
+ fail r
52
+ end
53
+ }
54
+
55
+ r.errback { fail }
56
+ r
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,62 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'eventmachine'
4
+ require 'lib/pubsubhubbub'
5
+
6
+ describe EventMachine::PubSubHubbub do
7
+
8
+ def failed
9
+ EventMachine.stop
10
+ fail
11
+ end
12
+
13
+ it "should publish single feed to hub" do
14
+ EventMachine.run {
15
+ pub = EventMachine::PubSubHubbub.new('http://pubsubhubbub.appspot.com/publish').publish "http://www.test.com/"
16
+
17
+ pub.errback { failed }
18
+ pub.callback {
19
+ pub.response_header.status.should == 204
20
+ EventMachine.stop
21
+ }
22
+ }
23
+ end
24
+
25
+ it "should publish multiple feeds to hub" do
26
+ EventMachine.run {
27
+ feeds = ['http://www.test.com', 'http://www.test.com/2']
28
+ pub = EventMachine::PubSubHubbub.new('http://pubsubhubbub.appspot.com/publish').publish feeds
29
+
30
+ pub.errback { failed }
31
+ pub.callback {
32
+ pub.response_header.status.should == 204
33
+ EventMachine.stop
34
+ }
35
+ }
36
+ end
37
+
38
+ it "should subscribe a single feed to hub" do
39
+ EventMachine.run {
40
+ sub = EventMachine::PubSubHubbub.new('http://pubsubhubbub.appspot.com/').subscribe "http://blog.superfeedr.com/atom.xml", "http://superfeedr.com/hubbub", {}
41
+
42
+ sub.errback { failed }
43
+ sub.callback {
44
+ sub.response_header.status.should == 204
45
+ EventMachine.stop
46
+ }
47
+ }
48
+ end
49
+
50
+ it "should unsubscribe a single feed to hub" do
51
+ EventMachine.run {
52
+ sub = EventMachine::PubSubHubbub.new('http://pubsubhubbub.appspot.com/').unsubscribe "http://blog.superfeedr.com/atom.xml", "http://superfeedr.com/hubbub", {}
53
+
54
+ sub.errback { failed }
55
+ sub.callback {
56
+ sub.response_header.status.should == 204
57
+ EventMachine.stop
58
+ }
59
+ }
60
+ end
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pubsubhubbub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ilya Grigorik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-26 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: eventmachine
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.12.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: em-http-request
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.5
34
+ version:
35
+ description: Asynchronous PubSubHubbub client for Ruby
36
+ email: ilya@igvita.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - README.rdoc
45
+ - VERSION
46
+ - lib/pubsubhubbub.rb
47
+ - lib/pubsubhubbub/client.rb
48
+ - test/test_client.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/igrigorik/pubsubhubbub
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project: pubsubhubbub
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Asynchronous PubSubHubbub client for Ruby
77
+ test_files:
78
+ - test/test_client.rb