osub 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/osub/subscriber.rb +35 -26
- data/lib/osub/subscribers.rb +23 -0
- data/lib/osub/version.rb +1 -1
- data/lib/osub.rb +3 -2
- data/osub.gemspec +2 -0
- metadata +18 -5
data/lib/osub/subscriber.rb
CHANGED
@@ -1,56 +1,65 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'uri'
|
3
3
|
|
4
|
+
require 'hmac-sha1'
|
5
|
+
|
4
6
|
module OSub
|
5
7
|
class Subscription
|
6
|
-
|
8
|
+
attr_reader :callback_url
|
9
|
+
attr_reader :topic_url
|
10
|
+
|
11
|
+
def initialize(callback_url, topic_url, secret = nil)
|
12
|
+
@tokens = []
|
13
|
+
|
14
|
+
secret = "" if secret == nil
|
15
|
+
@secret = secret.to_s
|
16
|
+
|
7
17
|
@callback_url = callback_url
|
8
18
|
@topic_url = topic_url
|
9
19
|
end
|
10
20
|
|
11
21
|
# Subscribe to the topic through the given hub.
|
12
|
-
def subscribe(hub_url = @topic_url)
|
13
|
-
|
22
|
+
def subscribe(hub_url = @topic_url, token = nil)
|
23
|
+
if token != nil
|
24
|
+
@tokens << token.to_s
|
25
|
+
end
|
26
|
+
change_subscription(:subscribe, hub_url, token)
|
14
27
|
end
|
15
28
|
|
16
29
|
# Unsubscribe to the topic through the given hub.
|
17
|
-
def unsubscribe(hub_url = @topic_url)
|
18
|
-
|
30
|
+
def unsubscribe(hub_url = @topic_url, token = nil)
|
31
|
+
if token != nil
|
32
|
+
@tokens << token.to_s
|
33
|
+
end
|
34
|
+
change_subscription(:unsubscribe, hub_url, token)
|
19
35
|
end
|
20
36
|
|
21
|
-
def change_subscription(mode, hub_url)
|
37
|
+
def change_subscription(mode, hub_url, token)
|
22
38
|
res = Net::HTTP.post_form(URI.parse(hub_url),
|
23
39
|
{ 'hub.mode' => mode.to_s,
|
24
40
|
'hub.callback' => @callback_url,
|
25
41
|
'hub.verify' => 'async',
|
26
|
-
'hub.verify_token' =>
|
42
|
+
'hub.verify_token' => token,
|
27
43
|
'hub.lease_seconds' => '',
|
28
|
-
'hub.secret' =>
|
44
|
+
'hub.secret' => @secret,
|
29
45
|
'hub.topic' => @topic_url})
|
30
46
|
end
|
31
47
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
module Subscriptions
|
38
|
-
def Subscriptions.new(callback_url, topic_url)
|
39
|
-
if not defined?(@@instances)
|
40
|
-
@@instances = {}
|
41
|
-
end
|
48
|
+
def verify_subscription(token)
|
49
|
+
result = @tokens.index(token) != nil
|
50
|
+
@tokens.delete(token)
|
42
51
|
|
43
|
-
|
44
|
-
|
45
|
-
@@instances[topic_url] = sub
|
46
|
-
end
|
52
|
+
result
|
53
|
+
end
|
47
54
|
|
48
|
-
|
55
|
+
def verify_content(body, signature)
|
56
|
+
hmac = HMAC::SHA1.hexdigest(@secret, body)
|
57
|
+
check = "sha1=" + hmac
|
58
|
+
check == signature
|
49
59
|
end
|
50
60
|
|
51
|
-
def
|
52
|
-
|
53
|
-
@@instances[topic_url]
|
61
|
+
def perform_challenge(challenge_code)
|
62
|
+
{:body => challenge_code, :status => 200}
|
54
63
|
end
|
55
64
|
end
|
56
65
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative 'subscription'
|
2
|
+
|
3
|
+
module OSub
|
4
|
+
module Subscribers
|
5
|
+
def Subscribers.new(callback_url, topic_url, secret = nil)
|
6
|
+
if not defined?(@@instances)
|
7
|
+
@@instances = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
if @@instances[topic_url] == nil
|
11
|
+
sub = OSub::Subscription.new(callback_url, topic_url, secret)
|
12
|
+
@@instances[topic_url] = sub
|
13
|
+
end
|
14
|
+
|
15
|
+
@@instances[topic_url]
|
16
|
+
end
|
17
|
+
|
18
|
+
def Subscribers.[](topic_url)
|
19
|
+
return nil if not defined?(@@instances)
|
20
|
+
@@instances[topic_url]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/osub/version.rb
CHANGED
data/lib/osub.rb
CHANGED
@@ -1,2 +1,3 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require_relative 'osub/subscriber'
|
2
|
+
require_relative 'osub/subscription'
|
3
|
+
|
data/osub.gemspec
CHANGED
@@ -14,6 +14,8 @@ Gem::Specification.new do |s|
|
|
14
14
|
|
15
15
|
s.rubyforge_project = "osub"
|
16
16
|
|
17
|
+
s.add_dependency "ruby-hmac"
|
18
|
+
|
17
19
|
s.files = `git ls-files`.split("\n")
|
18
20
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
21
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Hackers of the Severed Hand
|
@@ -14,10 +14,22 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-03-
|
17
|
+
date: 2011-03-14 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: ruby-hmac
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
21
33
|
description: This is a simple implementation of a subscriber in the PubSubHubbub protocol.
|
22
34
|
email:
|
23
35
|
- hotsh@xomb.org
|
@@ -33,6 +45,7 @@ files:
|
|
33
45
|
- Rakefile
|
34
46
|
- lib/osub.rb
|
35
47
|
- lib/osub/subscriber.rb
|
48
|
+
- lib/osub/subscribers.rb
|
36
49
|
- lib/osub/version.rb
|
37
50
|
- osub.gemspec
|
38
51
|
has_rdoc: true
|