Sha256: c072168bb2b31fbdaf24e2c49b1093b9b198363e6cbce6aed91cb8b6a7eae203

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

module Rack
  class EnvNotifier
    class BodyInjector
      # Lookup for <body> tag and inject notification after

      BODY_TAG_REGEX = /<body>|<body[^(er)][^<]*>/

      attr_reader :content_length, :new_body, :notification_added

      def initialize(body, text_to_be_injected)
        @body                = body
        @text_to_be_injected = text_to_be_injected
      end

      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-env-notifier-0.0.6 lib/rack/env_notifier/body_injector.rb
rack-env-notifier-0.0.5 lib/rack/env_notifier/body_injector.rb