Sha256: b47d923afc5c4a9ac0385298163a3b80f8fa5cde08c427fddc7835224380f342

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 KB

Contents

module Stoor
  class Decorate
    include Rack::Utils

    FOOTER_REGEXP = /(<div id="footer">)(.*?)(<\/div>)/im

    def initialize(app); @app = app; end

    def call(env)
      @request = Rack::Request.new(env)

      if @request.session['gollum.author'].nil?
        @request.logger.info "No 'gollum.author' in session - skipping page decoration."
        return @app.call(env)
      end

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

      if !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
          !headers['transfer-encoding'] &&
          headers['content-type'] &&
          headers['content-type'].include?("text/html")

        # TODO: If the response isn't an Array, it's a Rack::File or something, so ignore
        if response.respond_to? :inject
          content = response.inject("") { |content, part| content << part }
          if match_data = content.match(FOOTER_REGEXP)
            new_body = "" <<
              match_data.pre_match <<
              match_data[1] <<
              before_existing_footer <<
              match_data[2] <<
              after_existing_footer <<
              match_data[3] <<
              match_data.post_match
            headers['Content-Length'] = new_body.bytesize.to_s
            return [status, headers, [new_body]]
          end
        end
      end

      [status, headers, response]
    end

    def before_existing_footer
      <<-HTML
        <div style="float: left;">
      HTML
    end

    def after_existing_footer
      <<-HTML
        </div>
        <div style="float: right;">
          <p style="text-align: right; font-size: .9em; line-height: 1.6em; color: #999; margin: 0.9em 0;">
            Commiting as <b>#{@request.session['gollum.author'][:name]}</b> (#{@request.session['gollum.author'][:email]})#{" | <a href='/logout'>Logout</a>" if ENV['GITHUB_AUTHORIZED']}
          </p>
        </div>
      HTML
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
stoor-0.1.4 lib/stoor/decorate.rb
stoor-0.1.3 lib/stoor/decorate.rb
stoor-0.1.2 lib/stoor/decorate.rb
stoor-0.1.1 lib/stoor/decorate.rb
stoor-0.1.0 lib/stoor/decorate.rb