Sha256: 06bb1739986a070461c7c2df02c25a7843982ed8ac3ee389a368199e44e90c0b

Contents?: true

Size: 767 Bytes

Versions: 3

Compression:

Stored size: 767 Bytes

Contents

# frozen_string_literal: true

module WebConsole
  # Injects content into a Rack body.
  class Injector
    def initialize(body, headers)
      @body = "".dup

      body.each { |part| @body << part }
      body.close if body.respond_to?(:close)

      @headers = headers
    end

    def inject(content)
      # Set Content-Length header to the size of the current body
      # + the extra content. Otherwise the response will be truncated.
      if @headers["Content-Length"]
        @headers["Content-Length"] = (@body.bytesize + content.bytesize).to_s
      end

      [
        if position = @body.rindex("</body>")
          [ @body.insert(position, content) ]
        else
          [ @body << content ]
        end,
        @headers
      ]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
web-console-4.2.0 lib/web_console/injector.rb
web-console-4.1.0 lib/web_console/injector.rb
web-console-4.0.4 lib/web_console/injector.rb