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.



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

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

Instance Method Details

#[](name) ⇒ Object



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

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

#[]=(name, value) ⇒ Object



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

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

#delete(name) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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

#read(request) ⇒ Object



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

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

#write(header) ⇒ Object



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

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