Sha256: a58a8cf65cdd73bb7b387ca92c2ee904c18289421e46eef029ab5d50fa96f982

Contents?: true

Size: 1.78 KB

Versions: 9

Compression:

Stored size: 1.78 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

9 entries across 9 versions & 1 rubygems

Version Path
rack-mini-profiler-0.9.9.2 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.9.9.1 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.9.9 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.9.8 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.9.7 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.9.6 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.9.5 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.9.4 lib/mini_profiler/client_settings.rb
rack-mini-profiler-0.9.3 lib/mini_profiler/client_settings.rb