Class: Shamu::Sessions::CookieStore
- Inherits:
-
Shamu::Services::Service
- Object
- Shamu::Services::Service
- Shamu::Sessions::CookieStore
- Includes:
- Shamu::Security::HashedValue, SessionStore
- Defined in:
- lib/shamu/sessions/cookie_store.rb
Overview
Track persistent values in a cookie stored on the user's machine. Values kept in the CookieStore are not encrypted but they are protected by HMAC hashing to ensure that they have not been modified.
To support cookies, in your service it must be instantiated as part of a Rack request and you must add Rack::CookieMiddleware to your app.
Adding support to a Rails app
# application.rb
config.middleware.use Shamu::Rack::CookiesMiddleware
In a standalone Rack app
require "shamu/rack"
app = Rack::Builder.new do
use Shamu::Rack::CookiesMiddleware
end
run app
Constant Summary collapse
- TTL =
How long cookies should be kept.
( 30 * 24 * 60 * 60 ).freeze
Dependencies collapse
Instance Method Summary collapse
-
#delete(key) ⇒ nil
Remove the value with the given key.
-
#fetch(key, &block) ⇒ Object
Fetch the value with the given key from the store.
-
#set(key, value) ⇒ value
Save a named value in the session.
Methods included from Shamu::Security::HashedValue
Methods included from SessionStore
Methods inherited from Shamu::Services::Service
#cache_for, #cached_lookup, #entity_list, #entity_lookup_list, #error, #find_by_lookup, #lazy_association, #lookup_association
Instance Attribute Details
#cookies ⇒ Shamu::Rack::Cookies
46 |
# File 'lib/shamu/sessions/cookie_store.rb', line 46 attr_dependency :cookies, Shamu::Rack::Cookies |
Instance Method Details
#delete(key) ⇒ nil
Remove the value with the given key.
73 74 75 |
# File 'lib/shamu/sessions/cookie_store.rb', line 73 def delete( key ) .delete( key ) end |
#fetch(key, &block) ⇒ Object
Fetch the value with the given key from the store. If they key does not yet exist, yields to the block and caches the result.
59 60 61 62 63 64 65 |
# File 'lib/shamu/sessions/cookie_store.rb', line 59 def fetch( key, &block ) if .key?( key ) verify_hash( .get( key ) ) elsif block_given? yield end end |
#set(key, value) ⇒ value
Save a named value in the session.
68 69 70 |
# File 'lib/shamu/sessions/cookie_store.rb', line 68 def set( key, value ) .set( key, value: hash_value( value ), secure: true, max_age: TTL ) end |