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.



3
4
5
6
# File 'lib/grape/cookies.rb', line 3

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

Instance Method Details

#[](name) ⇒ Object



21
22
23
# File 'lib/grape/cookies.rb', line 21

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

#[]=(name, value) ⇒ Object



25
26
27
28
# File 'lib/grape/cookies.rb', line 25

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

#delete(name, opts = {}) ⇒ Object



34
35
36
37
# File 'lib/grape/cookies.rb', line 34

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

#each(&block) ⇒ Object



30
31
32
# File 'lib/grape/cookies.rb', line 30

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

#read(request) ⇒ Object



8
9
10
11
12
# File 'lib/grape/cookies.rb', line 8

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

#write(header) ⇒ Object



14
15
16
17
18
19
# File 'lib/grape/cookies.rb', line 14

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