Class: Grape::Cookies

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

Instance Method Summary (collapse)

Constructor Details

- (Cookies) initialize

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

- (Object) [](name)



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

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

- (Object) []=(name, value)



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

- (Object) delete(name, opts = {})



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

- (Object) each(&block)



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

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

- (Object) read(request)



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

- (Object) write(header)



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