Sha256: 4002ed66563b96870f23f06c0774144dca48e0183d6eeeadbeeb9db70fb5b8d0

Contents?: true

Size: 1.31 KB

Versions: 8

Compression:

Stored size: 1.31 KB

Contents

require 'multi_json'

module Scrivito

class EditingContextMiddleware
  ENVKEY = 'SCRIVITO_EDITING_CONTEXT'

  def initialize(app)
    @app = app
  end

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

    url_params    = parse_url_params(request)
    header_params = parse_header_params(env)
    cookie_params = parse_cookie_params(request)

    display_mode = url_params['display_mode'] ||
        header_params['display_mode'] ||
        cookie_params['display_mode']

    workspace_id = url_params['workspace_id'] ||
        header_params['workspace_id'] ||
        cookie_params['workspace_id']

    env[ENVKEY] = EditingContext.new(
      display_mode: display_mode,
      editor: -> { Configuration.editing_auth_callback.call(env) },
      selected_workspace_id: workspace_id
    )

    @app.call(env)
  end

  private

  def parse_url_params(request)
    {
      'display_mode' => request.params['_scrivito_display_mode'],
      'workspace_id' => request.params['_scrivito_workspace_id'],
    }
  end

  def parse_header_params(env)
    parse_json_params(env['HTTP_SCRIVITO_EDITING_CONTEXT'])
  end

  def parse_cookie_params(request)
    parse_json_params(request.cookies['scrivito_editing_context'])
  end

  def parse_json_params(json_params)
    MultiJson.decode(json_params)
  rescue MultiJson::ParseError
    {}
  end
end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
scrivito_sdk-0.18.1 lib/scrivito/editing_context_middleware.rb
scrivito_sdk-0.18.0 lib/scrivito/editing_context_middleware.rb
scrivito_sdk-0.17.0 lib/scrivito/editing_context_middleware.rb
scrivito_sdk-0.16.0 lib/scrivito/editing_context_middleware.rb
scrivito_sdk-0.15.0 lib/scrivito/editing_context_middleware.rb
scrivito_sdk-0.14.0 lib/scrivito/editing_context_middleware.rb
scrivito_sdk-0.13.0 lib/scrivito/editing_context_middleware.rb
scrivito_sdk-0.12.0 lib/scrivito/editing_context_middleware.rb