Sha256: 6712ad7ae316b9ab26260a30f9118b911e5e68aa3345d118a13d02c05b25e776

Contents?: true

Size: 1.79 KB

Versions: 17

Compression:

Stored size: 1.79 KB

Contents

module Rack
  class MiniProfiler
    class ClientSettings
      
      COOKIE_NAME = "__profilin"

      BACKTRACE_DEFAULT = nil
      BACKTRACE_FULL = 1 
      BACKTRACE_NONE = 2

      attr_accessor :disable_profiling
      attr_accessor :backtrace_level

      
      def initialize(env)
        request = ::Rack::Request.new(env)
        @cookie = request.cookies[COOKIE_NAME] 
        if @cookie
          @cookie.split(",").map{|pair| pair.split("=")}.each do |k,v|
            @orig_disable_profiling = @disable_profiling = (v=='t') if k == "dp"
            @backtrace_level = v.to_i if k == "bt"
          end
        end

        @backtrace_level = nil if !@backtrace_level.nil? && (@backtrace_level == 0 || @backtrace_level > BACKTRACE_NONE) 
        @orig_backtrace_level = @backtrace_level

      end

      def write!(headers)
        if @orig_disable_profiling != @disable_profiling || @orig_backtrace_level != @backtrace_level || @cookie.nil?
          settings = {"p" =>  "t" }
          settings["dp"] = "t" if @disable_profiling 
          settings["bt"] = @backtrace_level if @backtrace_level
          settings_string = settings.map{|k,v| "#{k}=#{v}"}.join(",")
          Rack::Utils.set_cookie_header!(headers, COOKIE_NAME, :value => settings_string, :path => '/')
        end
      end

      def discard_cookie!(headers)
        Rack::Utils.delete_cookie_header!(headers, COOKIE_NAME, :path => '/')
      end

      def has_cookie?
        !@cookie.nil?
      end

      def disable_profiling? 
        @disable_profiling
      end

      def backtrace_full? 
        @backtrace_level == BACKTRACE_FULL
      end

      def backtrace_default? 
        @backtrace_level == BACKTRACE_DEFAULT
      end
      
      def backtrace_none? 
        @backtrace_level == BACKTRACE_NONE
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
rack-mini-profiler-0.1.29 Ruby/lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.28 Ruby/lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.27 Ruby/lib/mini_profiler/client_settings.rb
mini-mini-profiler-0.1.1 Ruby/lib/mini_profiler/client_settings.rb
mini-mini-profiler-0.1 Ruby/lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.26 Ruby/lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.25 Ruby/lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.24 Ruby/lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.23 Ruby/lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.22 Ruby/lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.21 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.20 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.19 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.18 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.17 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.16 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.1.15.pre lib/mini_profiler/client_settings.rb