Sha256: e0b0b2c18b9d116d82181ba5973c32b3c9a39f4b08a420262c42acfac19df6a5

Contents?: true

Size: 1.25 KB

Versions: 7

Compression:

Stored size: 1.25 KB

Contents

module Workarea
  class ApplicationMiddleware
    def initialize(app)
      @app = app
    end

    def call(env)
      request = Rack::Request.new(env)
      return @app.call(env) if request.path =~ /(jpe?g|png|ico|gif|css|js|svg)$/

      setup_environment(env, request)
      set_segment_request_headers(env)
      status, headers, body = @app.call(env)
      set_segment_response_headers(env, headers)

      [status, headers, body]
    end

    def setup_environment(env, request)
      env['workarea.visit'] = Visit.new(env)
      env['workarea.cache_varies'] = Cache::Varies.new(env['workarea.visit']).to_s
      env['rack-cache.cache_key'] = Cache::RackCacheKey
      env['rack-cache.force-pass'] = env['workarea.visit'].admin?
    end

    def set_segment_request_headers(env)
      env['HTTP_X_WORKAREA_SEGMENTS'] = normalize_segment_ids(env['workarea.visit'])
    end

    def set_segment_response_headers(env, headers)
      headers['X-Workarea-Segments'] = normalize_segment_ids(env['workarea.visit'])

      if headers['X-Workarea-Segmented-Content'] == 'true'
        headers['Cache-Control'] = 'private, no-cache, max-age=0'
      end
    end

    private

    def normalize_segment_ids(visit)
      visit.applied_segments.map(&:id).sort.join(',')
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
workarea-core-3.5.8 app/middleware/workarea/application_middleware.rb
workarea-core-3.5.7 app/middleware/workarea/application_middleware.rb
workarea-core-3.5.6 app/middleware/workarea/application_middleware.rb
workarea-core-3.5.5 app/middleware/workarea/application_middleware.rb
workarea-core-3.5.4 app/middleware/workarea/application_middleware.rb
workarea-core-3.5.3 app/middleware/workarea/application_middleware.rb
workarea-core-3.5.2 app/middleware/workarea/application_middleware.rb