module Dailycred class Middleware attr_accessor :client_id def initialize(app, client_id, opts={}) @opts = opts @opts[:url] ||= "https://www.dailycred.com" @opts[:modal] ||= false @opts[:sdk] ||= false @opts[:triggers] ||= [] @app = app @client_id = client_id end def call(env) @env = env @status, @headers, @response = @app.call(env) if @headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/ body = "" @response.each { |part| body << part } index = body.rindex("") if index body.insert(index, render_dailycred_scripts) @headers["Content-Length"] = body.length.to_s @response = [body] end end @env['rack.session']['omniauth.state'] ||= SecureRandom.hex(24) [@status, @headers, @response] end private def render_dailycred_scripts str =<<-EOT #{'' if @opts[:persona_audience]} EOT str2 =<<-EOT EOT str += str2 if @opts[:sdk] str += "\n" str end end end