pubsubhubbub4r 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/README.rdoc +85 -0
- data/Rakefile +2 -0
- data/lib/pubsubhubbub4r/client.rb +87 -0
- data/lib/pubsubhubbub4r/version.rb +3 -0
- data/lib/pubsubhubbub4r.rb +5 -0
- data/pubsubhubbub4r.gemspec +22 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 37ad0d3e0c9a3befab14f9ed2bfacfb58656b26d
|
4
|
+
data.tar.gz: 38441d457c5aa08c1abc3b2bd6008979ff509a4e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45141faed9709d6696c731cd60fbfbed0a976e245af46f1204f02639ca8eb6edfb7cf4d8e4d4eacd44c955efd15c0364ffc35749986f2c8fedb75ce6c4b98b16
|
7
|
+
data.tar.gz: 7a5de4c27806adb6d30094584222fef1967bc56d7b7f8d9d0114e797c76adfa341aee27b68b3adcc1eda775d4d6fccf73b1f8d06278be30b4443281a7efe0fca
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 choonkeat
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Pubsubhubbub4r
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'pubsubhubbub4r'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install pubsubhubbub4r
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/pubsubhubbub4r/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/README.rdoc
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
= Pubsubhubbub4r
|
2
|
+
|
3
|
+
Simple pubsubhubbub client lib for Ruby. API is Rails-friendly, not dependent.
|
4
|
+
|
5
|
+
== Files
|
6
|
+
|
7
|
+
|-- README.rdoc
|
8
|
+
`-- lib
|
9
|
+
`-- pubsubhubbub4r
|
10
|
+
`-- client.rb
|
11
|
+
|
12
|
+
== Example Rails controller
|
13
|
+
|
14
|
+
require 'pubsubhubbub4r/client'
|
15
|
+
class BasicPubsubhubbubController < ApplicationController
|
16
|
+
def notify
|
17
|
+
client = Pubsubhubbub4r::Client.new(Rails.cache, Rails.logger)
|
18
|
+
case params['hub.challenge'] && params['hub.mode']
|
19
|
+
when nil
|
20
|
+
# doc = Hpricot(request.body.read) # parse XML from "request.body.read"
|
21
|
+
# response.headers['X-Hub-On-Behalf-Of'] = 1 # optional subscriber count
|
22
|
+
render :nothing => true
|
23
|
+
when 'subscribe'
|
24
|
+
if reply = client.verify(params)
|
25
|
+
render :text => reply
|
26
|
+
else
|
27
|
+
render :nothing => true, :status => 404
|
28
|
+
end
|
29
|
+
when 'unsubscribe'
|
30
|
+
if reply = client.verify(params)
|
31
|
+
render :text => reply
|
32
|
+
else
|
33
|
+
render :nothing => true, :status => 404
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
== Example script/console usage with BasicPubsubhubbubController
|
40
|
+
|
41
|
+
Setup
|
42
|
+
require 'pubsubhubbub4r/client'
|
43
|
+
atom_url = "http://feedproxy.google.com/TechCrunch"
|
44
|
+
hub_url = "http://pubsubhubbub.appspot.com"
|
45
|
+
callback_url = app.url_for(:only_path => false, :controller => 'basic_pubsubhubbub', :action => 'notify')
|
46
|
+
client = Pubsubhubbub4r::Client.new(Rails.cache, Rails.logger)
|
47
|
+
|
48
|
+
Subscribe
|
49
|
+
leased_seconds = client.subscribe(hub_url, atom_url, callback_url)
|
50
|
+
if leased_seconds
|
51
|
+
puts "Hub verified subscription until #{Time.now + leased_seconds}"
|
52
|
+
else
|
53
|
+
puts "Hub will asynchronously verify subscription at BasicPubsubhubbubController#notify"
|
54
|
+
end
|
55
|
+
|
56
|
+
Unsubscribe
|
57
|
+
verified = client.unsubscribe(hub_url, atom_url, callback_url)
|
58
|
+
if verified
|
59
|
+
puts "Hub verified unsubscribe-request on the spot."
|
60
|
+
else
|
61
|
+
puts "Hub will asynchronously verify unsubscribe-request at BasicPubsubhubbubController#notify"
|
62
|
+
end
|
63
|
+
|
64
|
+
== Example didn't work
|
65
|
+
|
66
|
+
Make sure you've configured Rails.cache in your config/environments/development.rb or config/environments/production.rb; By default, memory cache is used, and data there cannot be shared between script/console and script/server. (Maglev?)
|
67
|
+
|
68
|
+
e.g.
|
69
|
+
config.cache_store = :file_store
|
70
|
+
|
71
|
+
== Production practice
|
72
|
+
|
73
|
+
You can "subscribe" and Hubs can "verify". But don't fully trust things to work after that. Continue
|
74
|
+
to use polling for any feed until you get notified (at least ONCE) of new content from the hub. Sad
|
75
|
+
but true.
|
76
|
+
|
77
|
+
== Further reading
|
78
|
+
|
79
|
+
- http://pubsubhubbub.googlecode.com/svn/trunk/pubsubhubbub-core-0.1.html
|
80
|
+
- http://code.google.com/p/pubsubhubbub/
|
81
|
+
- http://github.com/igrigorik/PubSubHubbub
|
82
|
+
|
83
|
+
== License
|
84
|
+
|
85
|
+
Copyright (c) 2009 Chew Choon Keat, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
begin
|
2
|
+
require 'digest/sha1'
|
3
|
+
rescue LoadError
|
4
|
+
require 'sha1'
|
5
|
+
end
|
6
|
+
|
7
|
+
module Pubsubhubbub4r
|
8
|
+
class Client
|
9
|
+
HTTP_TIMEOUT = 60
|
10
|
+
attr_accessor :cache, :logger, :headers, :post_data_overrides
|
11
|
+
|
12
|
+
def initialize(cache, logger = nil, headers = nil, post_data_overrides = nil)
|
13
|
+
# "cache" and "logger" must respond like Rails.cache / Rails.logger
|
14
|
+
@cache = cache
|
15
|
+
@logger = logger
|
16
|
+
@headers = headers
|
17
|
+
# e.g. {'hub.verify' => 'sync'}
|
18
|
+
@post_data_overrides = post_data_overrides
|
19
|
+
end
|
20
|
+
|
21
|
+
def subscribe(hub_url, topic, callback_url, lease_seconds = 2592000, ack_timeout_seconds = 86400)
|
22
|
+
post('subscribe', hub_url, topic, callback_url, lease_seconds, ack_timeout_seconds)
|
23
|
+
end
|
24
|
+
|
25
|
+
def unsubscribe(hub_url, topic, callback_url, lease_seconds = 2592000, ack_timeout_seconds = 86400)
|
26
|
+
post('unsubscribe', hub_url, topic, callback_url, lease_seconds, ack_timeout_seconds)
|
27
|
+
end
|
28
|
+
|
29
|
+
def verify(params)
|
30
|
+
cache_key = verification_cache_key(params['hub.verify_token'])
|
31
|
+
cached_secret = self.cache.read(cache_key)
|
32
|
+
new_secret = verification_secret(params['hub.mode'], params['hub.topic'])
|
33
|
+
self.logger.debug "verifying #{cache_key.inspect}: #{cached_secret.inspect} vs #{new_secret.inspect}" if self.logger
|
34
|
+
if cached_secret && cached_secret == new_secret
|
35
|
+
self.cache.delete(cache_key)
|
36
|
+
params['hub.challenge']
|
37
|
+
else
|
38
|
+
false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
44
|
+
def verification_cache_key(verify_token)
|
45
|
+
"verify_token-#{verify_token}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def verification_secret(mode, topic)
|
49
|
+
[mode, topic].join("#")
|
50
|
+
end
|
51
|
+
|
52
|
+
def post(mode, hub_url, topic, callback_url, lease_seconds, ack_timeout_seconds)
|
53
|
+
verify_token = SHA1.hexdigest("#{[mode, topic, callback_url, lease_seconds, rand(ack_timeout_seconds), Time.now.to_f].join('.')}")
|
54
|
+
cache_key = verification_cache_key(verify_token)
|
55
|
+
self.cache.write(cache_key, verification_secret(mode, topic))
|
56
|
+
uri = URI.parse(hub_url)
|
57
|
+
request_headers = { 'User-Agent' => 'Pubsubhubbub4r::Client' }.merge(self.headers || {})
|
58
|
+
request = Net::HTTP::Post.new(uri.request_uri, request_headers)
|
59
|
+
request.form_data = {
|
60
|
+
'hub.mode' => mode,
|
61
|
+
'hub.callback' => callback_url,
|
62
|
+
'hub.topic' => topic,
|
63
|
+
'hub.verify_token' => verify_token,
|
64
|
+
'hub.lease_seconds' => lease_seconds,
|
65
|
+
'hub.verify' => 'async',
|
66
|
+
}.merge(self.post_data_overrides || {})
|
67
|
+
session = Net::HTTP.new(uri.host, uri.port)
|
68
|
+
session.use_ssl = true if uri.scheme == "https"
|
69
|
+
response = session.start do |http|
|
70
|
+
http.read_timeout = HTTP_TIMEOUT
|
71
|
+
yield http if block_given?
|
72
|
+
http.request(request)
|
73
|
+
end
|
74
|
+
case response && response.code.to_s
|
75
|
+
when "204"
|
76
|
+
self.cache.delete(cache_key)
|
77
|
+
self.logger.debug "#{mode} : verified" if self.logger
|
78
|
+
lease_seconds
|
79
|
+
when "202"
|
80
|
+
self.logger.debug "#{mode} : accepted, verify later" if self.logger
|
81
|
+
nil
|
82
|
+
else
|
83
|
+
raise "#{response.code} #{response.message} #{response.body}".strip
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pubsubhubbub4r/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pubsubhubbub4r"
|
8
|
+
spec.version = Pubsubhubbub4r::VERSION
|
9
|
+
spec.authors = ["choonkeat"]
|
10
|
+
spec.email = ["choonkeat@gmail.com"]
|
11
|
+
spec.summary = %q{Simple pubsubhubbub client lib for Ruby. API is Rails-friendly, not Rails-dependent}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pubsubhubbub4r
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- choonkeat
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- choonkeat@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- README.rdoc
|
53
|
+
- Rakefile
|
54
|
+
- lib/pubsubhubbub4r.rb
|
55
|
+
- lib/pubsubhubbub4r/client.rb
|
56
|
+
- lib/pubsubhubbub4r/version.rb
|
57
|
+
- pubsubhubbub4r.gemspec
|
58
|
+
homepage: ''
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.4.3
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Simple pubsubhubbub client lib for Ruby. API is Rails-friendly, not Rails-dependent
|
82
|
+
test_files: []
|