module SuperfeedrAPI

Public Instance Methods

base_path() click to toggle source
# File lib/rack-superfeedr.rb, line 43
def base_path
  @@base_path
end
base_path=(_base_path) click to toggle source
# File lib/rack-superfeedr.rb, line 39
def base_path= _base_path
  @@base_path = _base_path
end
host() click to toggle source
# File lib/rack-superfeedr.rb, line 35
def host
  @@host
end
host=(_host) click to toggle source
# File lib/rack-superfeedr.rb, line 31
def host= _host
  @@host = _host
end
list(opts = {}, &blk) click to toggle source

Lists subscriptions. options (first argument) include:

  • page (integer starts at 1)

  • by_page (integer, number of items per page)

  • search (string) a query string to only match certain subscriptions. Check Superfeedr docs for details

# File lib/rack-superfeedr.rb, line 198
def list(opts = {}, &blk)
  endpoint = @@superfeedr_endpoint
  request = {
    'hub.mode' => 'list',
    'detailed' => true
  }

  if opts[:page]
    request['page'] = opts[:page]
  end

  if opts[:by_page]
    request['by_page'] = opts[:by_page]
  end

  if opts[:search]
    request['search'] = opts[:search]
  end
  
  if opts[:login] && opts[:password]
    request['authorization'] = Base64.encode64( "#{opts[:login]}:#{opts[:password]}" ).chomp
  else
    request['authorization'] = Base64.encode64( "#{@@login}:#{@@password}" ).chomp
  end

  response = http_get(endpoint, request)

  r = [response.body, Integer(response.code) == 200, response]
  if blk
    blk.call(r) 
  end
  r
end
login() click to toggle source
# File lib/rack-superfeedr.rb, line 59
def login
  @@login
end
login=(_login) click to toggle source
# File lib/rack-superfeedr.rb, line 55
def login= _login
  @@login = _login
end
password() click to toggle source
# File lib/rack-superfeedr.rb, line 67
def password
  @@password
end
password=(_password) click to toggle source
# File lib/rack-superfeedr.rb, line 63
def password= _password
  @@password = _password
end
port() click to toggle source
# File lib/rack-superfeedr.rb, line 27
def port
  @@port
end
port=(_port) click to toggle source
# File lib/rack-superfeedr.rb, line 23
def port= _port
  @@port = _port
end
replay(url, id = nil, opts = {}, &blk) click to toggle source

Replay as notification. Required:

  • url of the topic to be replayed

  • id to build the callback url

The optional block will be called to let you confirm the subscription (or not). This is not applicable for if you use params => true It returns true if the unsubscription was successful (or will be confirmed if you used async => true in the options), false otherwise

# File lib/rack-superfeedr.rb, line 176
def replay(url, id = nil, opts = {}, &blk)
  endpoint = opts[:hub] || @@superfeedr_endpoint
  request = prep_request(url, id, endpoint, opts)

  request['hub.mode'] = 'replay'

  response = http_get(endpoint, request)

  r = response.body, Integer(response.code) == 204, response
  if blk
    blk.call(r) 
  end
  r
end
retrieve_by_topic_url(url, opts = {}, &blk) click to toggle source

Retrieve the content of a resource at Superfeedr A 2nd options argument can be supplied with

  • format => ‘json’ or ‘atom’ to specify the format of the notifications, defaults to atom

  • count => Integer (number of items to retrieve)

  • before => The id of an entry in the feed. The response will only include entries published before this one.

  • after => The id of an entry in the feed. The response will only include entries published after this one.

It yields 3 arguments to a block (if block is supplied. If not, just returns the triplet)

  • body of the response (useful if you used the retrieve option)

  • success flag

  • response (useful to debug failed requests mostly)

# File lib/rack-superfeedr.rb, line 125
def retrieve_by_topic_url(url, opts = {}, &blk)
  endpoint = opts[:hub] || @@superfeedr_endpoint
  request = prep_request(url, '', endpoint, opts)

  if opts[:format] == "json"
    request['format'] = "json"
  end

  if opts[:count]
    request['count'] = opts[:count]
  else
    request['count'] = 10
  end
  
  request['hub.mode'] = 'retrieve'

  response = http_get(endpoint, request)

  r = [response.body, Integer(response.code) == 200, response]
  if blk
    blk.call(r) 
  end
  r
end
scheme() click to toggle source
# File lib/rack-superfeedr.rb, line 51
def scheme
  @@scheme
end
scheme=(_scheme) click to toggle source
# File lib/rack-superfeedr.rb, line 47
def scheme= _scheme
  @@scheme = _scheme
end
subscribe(url, id = nil, opts = {}, &blk) click to toggle source

Subscribe you to a url. id is optional but strongly recommanded has a unique identifier for this url. It will be used to help you identify which feed is concerned by a notification. A 3rd options argument can be supplied with

  • retrieve => true if you want to retrieve the previous items in the feed

  • format => ‘json’ or ‘atom’ to specify the format of the notifications, defaults to atom

  • secret => a secret string used to compyte HMAC signatures so you can check that the data is coming from Superfeedr

  • sync => true (defaults to false) if you want to perfrom a verification of intent syncrhonously

  • async => true (defaults to false) if you want to perfrom a verification of intent asyncrhonously

  • hub => if you want to use an explicit hub, defaults to Superfeedr’s push.superfeedr.com

It yields 3 arguments to a block:

  • body of the response (useful if you used the retrieve option)

  • success flag

  • response (useful to debug failed requests mostly)

# File lib/rack-superfeedr.rb, line 85
def subscribe(url, id = nil, opts = {}, &blk)
  endpoint = opts[:hub] || @@superfeedr_endpoint
  request = prep_request(url, id, endpoint, opts)

  if opts[:retrieve]
    request['retrieve'] = true
  end
  
  if opts[:format] == "json"
    request['format'] = "json"
  end

  if opts[:secret]
    request['hub.secret'] = opts[:secret]
  else
    request['hub.secret'] = "WHAT DO WE PICK? A UNIQUE SCRET THE CALLBACK? SO WE CAN USE THAT ON NOTIFS?" 
  end
  
  request['hub.mode'] = 'subscribe'

  response = http_post(endpoint, request)

  r = [response.body, opts[:async] && Integer(response.code) == 202 || Integer(response.code) == 204 || opts[:retrieve] && Integer(response.code) == 200, response]
  if blk
    blk.call(r) 
  end
  r
end
superfeedr_endpoint() click to toggle source
# File lib/rack-superfeedr.rb, line 19
def superfeedr_endpoint
  @@superfeedr_endpoint
end
superfeedr_endpoint=(_superfeedr_endpoint) click to toggle source
# File lib/rack-superfeedr.rb, line 15
def superfeedr_endpoint= _superfeedr_endpoint
  @@superfeedr_endpoint = _superfeedr_endpoint
end
unsubscribe(url, id = nil, opts = {}, &blk) click to toggle source

Unsubscribes a url. If you used an id for the susbcription, you need to use _the same_. The optional block will be called to let you confirm the subscription (or not). This is not applicable for if you use params => true It returns true if the unsubscription was successful (or will be confirmed if you used async => true in the options), false otherwise

# File lib/rack-superfeedr.rb, line 154
def unsubscribe(url, id = nil, opts = {}, &blk)
  endpoint = opts[:hub] || @@superfeedr_endpoint
  request = prep_request(url, id, endpoint, opts)

  request['hub.mode'] = 'unsubscribe'

  response = http_post(endpoint, request)

  r = response.body, opts[:async] && Integer(response.code) == 202 || Integer(response.code) == 204, response
  if blk
    blk.call(r) 
  end
  r
end

Protected Instance Methods

generate_callback(url, feed_id) click to toggle source
# File lib/rack-superfeedr.rb, line 307
def generate_callback(url, feed_id)
  if @@scheme == "https" 
    port ||= @@port || 443
    URI::HTTPS.build({:scheme => @@scheme, :host => @@host, :path => "#{@@base_path}#{feed_id}", :port => port }).to_s
  else
    port ||= @@port || 80
    URI::HTTP.build({:scheme => @@scheme, :host => @@host, :path => "#{@@base_path}#{feed_id}", :port => port }).to_s
  end
end
http_get(url, opts) click to toggle source
# File lib/rack-superfeedr.rb, line 287
def http_get(url, opts)
  uri = URI.parse URI.encode(url)
  uri.query = URI.encode_www_form opts || {}
  uri.path=='/' if uri.path.empty?
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new uri.request_uri
  http.request(request)
end
http_post(url, opts) click to toggle source
# File lib/rack-superfeedr.rb, line 297
def http_post(url, opts)
  uri = URI.parse URI.encode(url)
  uri.path=='/' if uri.path.empty?
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new uri.request_uri
  request.set_form_data (opts||{})
  http.request(request)
end
prep_request(url = nil, id = nil, endpoint = nil, opts = {}) click to toggle source
# File lib/rack-superfeedr.rb, line 262
def prep_request(url = nil, id = nil, endpoint = nil, opts = {})
  feed_id = "#{id ? id : Base64.urlsafe_encode64(url)}"

  request = {
    'hub.topic' => url,
    'hub.callback' =>  generate_callback(url, feed_id)
  }

  if opts[:login] && opts[:password]
    request['authorization'] = Base64.encode64( "#{opts[:login]}:#{opts[:password]}" ).chomp
  elsif @@login && @@password
    request['authorization'] = Base64.encode64( "#{@@login}:#{@@password}" ).chomp
  end

  if opts[:async]
    request['hub.verify'] = 'async'
  end

  if opts[:sync]
    request['hub.verify'] = 'sync'
  end

  request
end