class Rack::EnvNotifier::BodyInjector
Constants
- BODY_TAG_REGEX
Lookup for <body> tag and inject notification after
Attributes
content_length[R]
new_body[R]
notification_added[R]
Public Class Methods
new(body, text_to_be_injected)
click to toggle source
# File lib/rack/env_notifier/body_injector.rb, line 10 def initialize(body, text_to_be_injected) @body = body @text_to_be_injected = text_to_be_injected end
Public Instance Methods
inject!(env)
click to toggle source
# File lib/rack/env_notifier/body_injector.rb, line 15 def inject!(env) @env = env @body.close if @body.respond_to?(:close) # Convert String body to Array so it can respond to each method # In test environment body may be a String object @body = [@body] if @body.is_a? String @new_body = [] @body.each { |line| @new_body << line.to_s } @content_length = 0 @notification_added = false @new_body.each do |line| if !@notification_added && line['<body'] line.gsub! (BODY_TAG_REGEX) {|match| %{#{match}\n#{@text_to_be_injected}} } @notification_added = true end # Keep track of content_length @content_length += line.bytesize end @new_body = @body end