Class: RackConsole::CookieScriptStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_console/cookie_script_storage.rb

Constant Summary collapse

WARNING_LIMIT_MSG =
->(max){ 'WARNING: stored script was limited to the first ' +
"#{max} chars to avoid issues with cookie overflow\n" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, cookie_key: '_rack-console-script', max_length: 4000) ⇒ CookieScriptStorage

Returns a new instance of CookieScriptStorage.



8
9
10
11
# File 'lib/rack_console/cookie_script_storage.rb', line 8

def initialize(env, cookie_key: '_rack-console-script', max_length: 4000)
  @env, @cookie_key, @max_length = env, cookie_key, max_length
  @script = ::CGI::Cookie.parse(env['HTTP_COOKIE'].to_s)[cookie_key]&.first || ''
end

Instance Attribute Details

#scriptObject

Returns the value of attribute script.



6
7
8
# File 'lib/rack_console/cookie_script_storage.rb', line 6

def script
  @script
end

Instance Method Details



15
16
17
18
19
20
21
22
# File 'lib/rack_console/cookie_script_storage.rb', line 15

def set_cookie_header!(headers = {})
  script = @script.to_s
  puts WARNING_LIMIT_MSG[@max_length] if script.size > @max_length
  cookie = { value: script[0...@max_length], path: @env['REQUEST_PATH'],
    domain: @env['SERVER_NAME'] }
  ::Rack::Utils.set_cookie_header! headers, @cookie_key, cookie
  headers
end