class WebConsole::Middleware - web-console-2.1.2 Documentation

# class WebConsole::Middleware

### Constants

DEFAULT_OPTIONS
TEMPLATES_PATH
UNAVAILABLE_SESSION_MESSAGE

### Public Class Methods

new(app, options = {}) click to toggle source

    # File lib/web_console/middleware.rb, line 22
    def initialize(app, options = {})
      @app     = app
      @options = DEFAULT_OPTIONS.merge(options)
    end

### Public Instance Methods

call(env) click to toggle source

    # File lib/web_console/middleware.rb, line 27
    def call(env)
      request = create_regular_or_whiny_request(env)
      return @app.call(env) unless request.from_whitelited_ip?

      if id = id_for_repl_session_update(request)
        return update_repl_session(id, request.params)
      elsif id = id_for_repl_session_stack_frame_change(request)
        return change_stack_trace(id, request.params)
      end

      status, headers, body = @app.call(env)

      if exception = env['web_console.exception']
        session = Session.from_exception(exception)
      elsif binding = env['web_console.binding']
        session = Session.from_binding(binding)
      end

      if session && request.acceptable_content_type?
        response = Rack::Response.new(body, status, headers)
        template = Template.new(env, session)

        response.write(template.render('index'))
        response.finish
      else
        [ status, headers, body ]
      end
    end

### Private Instance Methods

binding_change_re() click to toggle source

    # File lib/web_console/middleware.rb, line 67
    def binding_change_re
      @options[:binding_change_re]
    end

change_stack_trace(id, params) click to toggle source

    # File lib/web_console/middleware.rb, line 95
    def change_stack_trace(id, params)
      unless session = Session.find(id)
        return respond_with_unavailable_session(id)
      end

      session.switch_binding_to(params[:frame_id])

      status  = 200
      headers = { 'Content-Type' => 'application/json; charset = utf-8' }
      body    = { ok: true }.to_json

      Rack::Response.new(body, status, headers).finish
    end

create_regular_or_whiny_request(env) click to toggle source

    # File lib/web_console/middleware.rb, line 58
    def create_regular_or_whiny_request(env)
      request = Request.new(env)
      whiny_requests ? WhinyRequest.new(request) : request
    end

id_for_repl_session_stack_frame_change(request) click to toggle source

    # File lib/web_console/middleware.rb, line 77
    def id_for_repl_session_stack_frame_change(request)
      if request.xhr? && request.post?
        binding_change_re.match(request.path_info) { |m| m[:id] }
      end
    end

id_for_repl_session_update(request) click to toggle source

    # File lib/web_console/middleware.rb, line 71
    def id_for_repl_session_update(request)
      if request.xhr? && request.put?
        update_re.match(request.path_info) { |m| m[:id] }
      end
    end

respond_with_unavailable_session(id) click to toggle source

    # File lib/web_console/middleware.rb, line 109
    def respond_with_unavailable_session(id)
      status = 404
      headers = { 'Content-Type' => 'application/json; charset = utf-8' }
      body    = { output: format(UNAVAILABLE_SESSION_MESSAGE, id: id)}.to_json

      Rack::Response.new(body, status, headers).finish
    end

update_re() click to toggle source

    # File lib/web_console/middleware.rb, line 63
    def update_re
      @options[:update_re]
    end

update_repl_session(id, params) click to toggle source

    # File lib/web_console/middleware.rb, line 83
    def update_repl_session(id, params)
      unless session = Session.find(id)
        return respond_with_unavailable_session(id)
      end

      status  = 200
      headers = { 'Content-Type' => 'application/json; charset = utf-8' }
      body    = { output: session.eval(params[:input]) }.to_json

      Rack::Response.new(body, status, headers).finish
    end