-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcookies.rb
50 lines (38 loc) · 952 Bytes
/
cookies.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true
module Grape
class Cookies
extend Forwardable
DELETED_COOKIES_ATTRS = {
max_age: '0',
value: '',
expires: Time.at(0)
}.freeze
def_delegators :cookies, :[], :each
def initialize(rack_cookies)
@cookies = rack_cookies
@send_cookies = nil
end
def response_cookies
return unless @send_cookies
send_cookies.each do |name|
yield name, cookies[name]
end
end
def []=(name, value)
cookies[name] = value
send_cookies << name
end
# see https://github.com/rack/rack/blob/main/lib/rack/utils.rb#L338-L340
def delete(name, **opts)
self.[]=(name, opts.merge(DELETED_COOKIES_ATTRS))
end
private
def cookies
return @cookies unless @cookies.is_a?(Proc)
@cookies = @cookies.call.with_indifferent_access
end
def send_cookies
@send_cookies ||= Set.new
end
end
end