class 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 [@status, @headers, @response] end private def render_dailycred_scripts <<-EOT #{'' unless @opts[:sdk]} EOT end end end