Class: Flame::Dispatcher::Cookies

Inherits:
Object
  • Object
show all
Defined in:
lib/flame/cookies.rb

Overview

Helper class for cookies

Instance Method Summary collapse

Constructor Details

#initialize(request_cookies, response) ⇒ Cookies

Returns a new instance of Cookies.



5
6
7
8
# File 'lib/flame/cookies.rb', line 5

def initialize(request_cookies, response)
	@request_cookies = request_cookies
	@response = response
end

Instance Method Details

#[](key) ⇒ Object

Get request cookies

Parameters:

  • key (String, Symbol)

    name of cookie



12
13
14
# File 'lib/flame/cookies.rb', line 12

def [](key)
	@request_cookies[key.to_s]
end

#[]=(key, new_value) ⇒ Object

Set (or delete) cookies for response

Examples:

Set new value to ‘cat` cookie

cookies['cat'] = 'nice cat'

Delete ‘cat` cookie

cookies['cat'] = nil

Parameters:

  • key (String, Symbol)

    name of cookie

  • new_value (Object, nil)

    value of cookie



23
24
25
26
# File 'lib/flame/cookies.rb', line 23

def []=(key, new_value)
	return @response.delete_cookie(key.to_s, path: '/') if new_value.nil?
	@response.set_cookie(key.to_s, value: new_value, path: '/')
end