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=, replay, retrieve_by_topic_url, scheme, scheme=, search, 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



357
358
359
360
361
362
# File 'lib/rack-superfeedr.rb', line 357

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

Instance Method Details

#call(env) ⇒ Object

Called by Rack!



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/rack-superfeedr.rb', line 377

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



330
331
332
# File 'lib/rack-superfeedr.rb', line 330

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



343
344
345
# File 'lib/rack-superfeedr.rb', line 343

def on_verification(&block)
  @verification = block
end

#resetObject

Resets.



366
367
368
369
370
371
372
373
# File 'lib/rack-superfeedr.rb', line 366

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