Class: Rack::Superfeedr

Inherits:
Object
  • Object
show all
Extended by:
SuperfeedrAPI
Defined in:
lib/rack-superfeedr.rb

Overview

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.

Instance Method Summary collapse

Methods included from SuperfeedrAPI

base_path, base_path=, host, host=, login, login=, password, password=, port, port=, retrieve_by_topic_url, scheme, scheme=, subscribe, superfeedr_endpoint, superfeedr_endpoint=, unsubscribe

Constructor Details

#initialize(app, &block) ⇒ Superfeedr

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



276
277
278
279
280
281
# File 'lib/rack-superfeedr.rb', line 276

def initialize(app, &block)
  @app = app
  reset
  block.call(self)
  self
end

Instance Method Details

#call(env) ⇒ Object

Called by Rack!



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/rack-superfeedr.rb', line 296

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

#on_notification(&block) ⇒ Object

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



249
250
251
# File 'lib/rack-superfeedr.rb', line 249

def on_notification(&block)
  @callback = block
end

#on_verification(&block) ⇒ Object

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



262
263
264
# File 'lib/rack-superfeedr.rb', line 262

def on_verification(&block)
  @verification = block
end

#resetObject

Resets.



285
286
287
288
289
290
291
292
# File 'lib/rack-superfeedr.rb', line 285

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