Class: Shamu::Rack::Cookies
- Inherits:
-
Object
- Object
- Shamu::Rack::Cookies
- Defined in:
- lib/shamu/rack/cookies.rb
Overview
Expose the request cookies as a hash.
Class Method Summary collapse
Instance Method Summary collapse
- #apply!(headers) ⇒ Hash
-
#delete(key) ⇒ self
Delete a cookie from the browser.
-
#get(key) ⇒ String
(also: #[])
Get a cookie value from the browser.
-
#initialize(env) ⇒ Cookies
constructor
A new instance of Cookies.
-
#key?(name) ⇒ Boolean
True if the cookie has been set.
-
#set(key, value) ⇒ self
(also: #[]=)
Set or update a cookie in the headers.
Constructor Details
#initialize(env) ⇒ Cookies
Returns a new instance of Cookies.
13 14 15 16 17 |
# File 'lib/shamu/rack/cookies.rb', line 13 def initialize( env ) @env = env @cookies = {} @deleted_cookies = [] end |
Class Method Details
.create ⇒ Cookies
8 9 10 |
# File 'lib/shamu/rack/cookies.rb', line 8 def self.create( * ) fail "Add Shamu::Rack::CookiesMiddleware to use Shamu::Rack::Cookies" end |
Instance Method Details
#apply!(headers) ⇒ Hash
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/shamu/rack/cookies.rb', line 26 def apply!( headers ) .each do |key, value| ::Rack::Utils. headers, key, value end .each do |key| ::Rack::Utils. headers, key end headers end |
#delete(key) ⇒ self
Delete a cookie from the browser.
89 90 91 92 93 |
# File 'lib/shamu/rack/cookies.rb', line 89 def delete( key ) .delete( key ) @deleted_cookies << key if .key?( key ) self end |
#get(key) ⇒ String Also known as: []
Get a cookie value from the browser.
41 42 43 44 45 46 47 48 49 |
# File 'lib/shamu/rack/cookies.rb', line 41 def get( key ) key = key.to_s if = [ key ] [:value] else [ key ] end end |
#key?(name) ⇒ Boolean
Returns true if the cookie has been set.
54 55 56 |
# File 'lib/shamu/rack/cookies.rb', line 54 def key?( name ) .key?( name ) || .key?( name ) end |
#set(key, value) ⇒ self #set(key, hash) ⇒ self Also known as: []=
Set or update a cookie in the headers.
75 76 77 78 79 80 81 82 83 |
# File 'lib/shamu/rack/cookies.rb', line 75 def set( key, value ) key = key.to_s .delete( key ) value = { value: value } unless value.is_a? Hash [key] = value self end |