Sha256: 7cdfe2d70bbedd9985d2b3076b1c600a59bd3c3ec0f5251cbed50d4a62d23837

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require 'rack'

module Stoor
  class AddAfter
    include Rack::Utils

    attr_reader :status, :headers
    attr_reader :rxp, :string, :content_type

    def initialize(app, rxp, string, content_type = 'text/html')
      @app, @rxp, @string, @content_type = app, rxp, string, content_type
    end

    def call(env)
      @status, @headers, response = @app.call(env)
      @headers = HeaderHash.new(@headers)

      if has_body && not_encoded && headers['content-type'] &&
        headers['content-type'].index(content_type)

        content = Array(response).join('')

        if content =~ rxp
          pre, match, post = $`, $&, $'
          new_body = pre + match + string + post
          headers['Content-Length'] = new_body.bytesize.to_s
          return [status, headers, [new_body]]
        end
      end

      [status, headers, response]
    end

    private

    def has_body
      !STATUS_WITH_NO_ENTITY_BODY.include?(status)
    end

    def not_encoded
      !headers['transfer-encoding']
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stoor-0.1.4 lib/stoor/add_after.rb