Sha256: 47309b617253ef14005813402c2bbaf86098d8e2496c53ecdd75254ac4ecdd4d
Contents?: true
Size: 1.12 KB
Versions: 2
Compression:
Stored size: 1.12 KB
Contents
require 'rack/utils' module GaEvents class Middleware def initialize(app) @app = app end def call(env) GaEvents::List.init status, headers, response = @app.call(env) headers = Rack::Utils::HeaderHash.new(headers) if GaEvents::List.present? request = Rack::Request.new(env) # Can outgrow, headers might get too big serialized = GaEvents::List.to_s if request.xhr? headers['X-GA-Events'] = serialized elsif is_html?(status, headers) body = response body = body.each.to_a.join('') if body.respond_to?(:each) body = body.sub('</body>', "<div data-ga-events='#{serialized}'></div>\\0") response = [body] end end [status, headers, response] end private # Taken from: # https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/jsonp.rb def is_html?(status, headers) !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status.to_i) && headers.key?('Content-Type') && headers['Content-Type'].include?('text/html') end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
ga_events-0.1.7 | lib/ga_events/middleware.rb |
ga_events-0.1.6 | lib/ga_events/middleware.rb |