class Rack::Superfeedr
This is a Rack
Middleware that can be used in your rack-compatible web framework (Rails, Sinatra…) to perform subscriptions over at superfeedr using the PubSubHubbub API.
Public Class Methods
Initializes the Rack
Middleware Make sure you define the following class attribues before that: Rack::Superfeedr.superfeedr_endpoint => push.superfeedr.com, defaults (do not change!) Rack::Superfeedr.host => Host for your application, used to build callback urls Rack::Superfeedr.port => Port for your application, used to build callback urls, defaults to Rack::Superfeedr.base_path => Base path for callback urls. Defauls to ‘/superfeedr/feed/’ Rack::Superfeedr.scheme => Scheme to build callback urls, defaults to ‘http’ Rack::Superfeedr.login => Superfeedr
login Rack::Superfeedr.password => Superfeedr
password
# File lib/rack-superfeedr.rb, line 359 def initialize(app, &block) @app = app reset block.call(self) self end
Public Instance Methods
Called by Rack
!
# File lib/rack-superfeedr.rb, line 379 def call(env) req = Rack::Request.new(env) if env['REQUEST_METHOD'] == 'GET' && feed_id = env['PATH_INFO'].match(/#{Rack::Superfeedr.base_path}(.*)/) if @verification.call(req.params['hub.mode'], feed_id[1], req.params['hub.topic'], req) Rack::Response.new(req.params['hub.challenge'], 200).finish else Rack::Response.new("not valid", 404).finish end elsif env['REQUEST_METHOD'] == 'POST' && feed_id = env['PATH_INFO'].match(/#{Rack::Superfeedr.base_path}(.*)/) @callback.call(feed_id[1], req.body.read, req.env['HTTP_X_PUBSUBHUBBUB_TOPIC'], req) Rack::Response.new("Thanks!", 200).finish else @app.call(env) end end
This allows you to define what happens with the notifications. The block passed in argument is called for each notification, with 4 arguments
-
feed_id (used in subscriptions)
-
body (Atom or JSON) based on subscription
-
url (optional… if the hub supports that,
Superfeedr
does) -
Rack::Request object. Useful for debugging and checking signatures
# File lib/rack-superfeedr.rb, line 332 def on_notification(&block) @callback = block end
This allows you to define what happens with verification of intents It’s a block called with
-
mode: subscribe|unsubscribe
-
Feed id (if available/supplied upon subscription)
-
Feed url
-
request (the Rack::Request object, should probably not be used, except for debugging)
If the block returns true, subscription will be confirmed If it returns false, it will be denied
# File lib/rack-superfeedr.rb, line 345 def on_verification(&block) @verification = block end
Resets.
# File lib/rack-superfeedr.rb, line 368 def reset @callback = Proc.new { |feed_id, body, url, request| # Nothing to do by default! } @verification = Proc.new { |mode, feed_id, url, request| true # Accept all by default! } end