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=, list, 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



307
308
309
310
311
312
# File 'lib/rack-superfeedr.rb', line 307

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

Instance Method Details

#call(env) ⇒ Object

Called by Rack!



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/rack-superfeedr.rb', line 327

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



280
281
282
# File 'lib/rack-superfeedr.rb', line 280

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



293
294
295
# File 'lib/rack-superfeedr.rb', line 293

def on_verification(&block)
  @verification = block
end

#resetObject

Resets.



316
317
318
319
320
321
322
323
# File 'lib/rack-superfeedr.rb', line 316

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