Class: Grape::Cookies

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

Instance Method Summary collapse

Constructor Details

#initializeCookies

Returns a new instance of Cookies.



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

def initialize
  @cookies = {}
  @send_cookies = {}
end

Instance Method Details

#[](name) ⇒ Object



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

def [](name)
  @cookies[name.to_s]
end

#[]=(name, value) ⇒ Object



27
28
29
30
# File 'lib/grape/cookies.rb', line 27

def []=(name, value)
  @cookies[name.to_s] = value
  @send_cookies[name.to_s] = true
end

#delete(name, **opts) ⇒ Object



36
37
38
39
# File 'lib/grape/cookies.rb', line 36

def delete(name, **opts)
  options = opts.merge(value: 'deleted', expires: Time.at(0))
  self.[]=(name, options)
end

#each(&block) ⇒ Object



32
33
34
# File 'lib/grape/cookies.rb', line 32

def each(&block)
  @cookies.each(&block)
end

#read(request) ⇒ Object



10
11
12
13
14
# File 'lib/grape/cookies.rb', line 10

def read(request)
  request.cookies.each do |name, value|
    @cookies[name.to_s] = value
  end
end

#write(header) ⇒ Object



16
17
18
19
20
21
# File 'lib/grape/cookies.rb', line 16

def write(header)
  @cookies.select { |key, _value| @send_cookies[key] == true }.each do |name, value|
    cookie_value = value.is_a?(Hash) ? value : { value: value }
    Rack::Utils.set_cookie_header! header, name, cookie_value
  end
end