Class: Brut::FrontEnd::Session
- Inherits:
-
Object
- Object
- Brut::FrontEnd::Session
- Defined in:
- lib/brut/front_end/session.rb
Overview
A class that represents the current session, as opposed to just a Hash. Generally, this can act like a Hash for setting and accessing values stored in the session. It provides a few useful additions:
- Your app can extend this to provide an app-specific API around the session, using
Brut.container.override("session_class",«your class»)
. - There is direct access to commonly-used data stored in the session, such as the flash.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access the underlying session directly.
-
#[]=(key, value) ⇒ Object
Set the session value for the key.
-
#delete(key) ⇒ Object
Delete a key from the session.
-
#flash ⇒ Object
Access the flash, as an instance of whatever class has been configured.
-
#flash=(new_flash) ⇒ Object
Set the flash.
-
#http_accept_language ⇒ Brut::I18n::HTTPAcceptLanguage
Return the interpretation of the
Accept-Language
header that was set by (#http_accept_language=). -
#http_accept_language=(http_accept_language) ⇒ Object
Set the
Accept-Language
for the session, as an I18n::HTTPAcceptLanguage. -
#initialize(rack_session:) ⇒ Session
constructor
Create the session based on the session provided by Rack.
-
#timezone ⇒ TZInfo::Timezone
Get the session timezone.
-
#timezone=(timezone) ⇒ Object
Set the session timezone, regardless of what the browser reports.
-
#timezone_from_browser ⇒ TZInfo::Timezone|nil
Get the timezone as reported by the browser, or nil if there isn't one or the browser sent and invalid value.
-
#timezone_from_browser=(timezone) ⇒ Object
Set the timezone as reported by the browser.
Constructor Details
#initialize(rack_session:) ⇒ Session
Create the session based on the session provided by Rack.
11 12 13 |
# File 'lib/brut/front_end/session.rb', line 11 def initialize(rack_session:) @rack_session = rack_session end |
Instance Method Details
#[](key) ⇒ Object
Access the underlying session directly
110 |
# File 'lib/brut/front_end/session.rb', line 110 def[](key) = @rack_session[key.to_s] |
#[]=(key, value) ⇒ Object
Set the session value for the key.
You are encouraged to send in a string. If you want to store rich data in the session, maybe don't? But if you must, add a method to do the marshalling in your app's subclass of this
118 119 120 |
# File 'lib/brut/front_end/session.rb', line 118 def[]=(key,value) @rack_session[key.to_s] = value end |
#delete(key) ⇒ Object
Delete a key from the session. This is preferred to setting the value to nil
123 |
# File 'lib/brut/front_end/session.rb', line 123 def delete(key) = @rack_session.delete(key.to_s) |
#flash ⇒ Object
Access the flash, as an instance of whatever class has been configured. Note that this returns a copy of the flash, so any changes will not be stored in the session unless you call (#flash=) after changing it. Generally, this isn't a big deal as Brut handles this for you.
128 129 130 |
# File 'lib/brut/front_end/session.rb', line 128 def flash Brut.container.flash_class.from_h(self[:__brut_flash]) end |
#flash=(new_flash) ⇒ Object
Set the flash.
132 133 134 |
# File 'lib/brut/front_end/session.rb', line 132 def flash=(new_flash) self[:__brut_flash] = new_flash.to_h end |
#http_accept_language ⇒ Brut::I18n::HTTPAcceptLanguage
Return the interpretation of the Accept-Language
header that was set by (#http_accept_language=).
will be returned.
19 |
# File 'lib/brut/front_end/session.rb', line 19 def http_accept_language = Brut::I18n::HTTPAcceptLanguage.from_session(self[:__brut_http_accept_language]) |
#http_accept_language=(http_accept_language) ⇒ Object
Set the Accept-Language
for the session, as an I18n::HTTPAcceptLanguage
23 24 25 |
# File 'lib/brut/front_end/session.rb', line 23 def http_accept_language=(http_accept_language) self[:__brut_http_accept_language] = http_accept_language.for_session end |
#timezone ⇒ TZInfo::Timezone
Get the session timezone. This is the preferred way to get a timezone for the current session. Always returns a value, based on the following logic:
- If #timezone= has been called with a value, that time zone is returned.
- If #timezone= has been given no value or
nil
, and #timezone_from_browser returns a value, that value is used. - If #timezone_from_browser returns
nil
,ENV["TZ"]
is used, assuming it is a valid time zone. - If 'ENV["TZ"]` is blank or invalid, UTC is returned.
It is in your best interest to ensure that each session has a valid time zone.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/brut/front_end/session.rb', line 75 def timezone tz_name = self[:__brut_timezone_override] timezone = nil if !tz_name.nil? begin timezone = TZInfo::Timezone.get(tz_name) rescue TZInfo::InvalidTimezoneIdentifier => ex Brut.container.instrumentation.record_exception(ex, class: self.class, invalid_tz_name: tz_name) end end if timezone.nil? timezone = self.timezone_from_browser end if timezone.nil? begin timezone = if ENV["TZ"] TZInfo::Timezone.get(ENV["TZ"]) else nil end rescue TZInfo::InvalidTimezoneIdentifier => ex Brut.container.instrumentation.record_exception(ex, class: self.class, invalid_env_tz: ENV['TZ']) nil end end if timezone.nil? timezone = TZInfo::Timezone.get("UTC") end timezone end |
#timezone=(timezone) ⇒ Object
Set the session timezone, regardless of what the browser reports.
nil
to clear this value and use the browser's time zone.
57 58 59 60 61 62 |
# File 'lib/brut/front_end/session.rb', line 57 def timezone=(timezone) if timezone.kind_of?(TZInfo::Timezone) timezone = timezone.name end self[:__brut_timezone_override] = timezone end |
#timezone_from_browser ⇒ TZInfo::Timezone|nil
Get the timezone as reported by the browser, or nil if there isn't one or the browser sent and invalid value
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/brut/front_end/session.rb', line 30 def timezone_from_browser tz_name = self[:__brut_timezone_from_browser] if tz_name.nil? return nil end begin TZInfo::Timezone.get(tz_name) rescue TZInfo::InvalidTimezoneIdentifier => ex Brut.container.instrumentation.record_exception(ex, class: self.class) nil end end |
#timezone_from_browser=(timezone) ⇒ Object
Set the timezone as reported by the browser.
46 47 48 49 50 51 |
# File 'lib/brut/front_end/session.rb', line 46 def timezone_from_browser=(timezone) if timezone.kind_of?(TZInfo::Timezone) timezone = timezone.name end self[:__brut_timezone_from_browser] = timezone end |