module GovukPersonalisation::Flash

Constants

MESSAGE_REGEX
MESSAGE_SEPARATOR
SESSION_SEPARATOR

Public Class Methods

decode_session(encoded_session) click to toggle source

Splits the session header into a session value (suitable for using in account-api calls) and flash messages.

@param encoded_session [String] the value of the `GOVUK-Account-Session` header

@return [Array(String, Array<String>), nil] the session value and the flash messages

# File lib/govuk_personalisation/flash.rb, line 14
def self.decode_session(encoded_session)
  session_bits = encoded_session&.split(SESSION_SEPARATOR)
  return if session_bits.blank?

  if session_bits.length == 1
    [session_bits[0], []]
  else
    [session_bits[0], session_bits[1].split(MESSAGE_SEPARATOR)]
  end
end
encode_session(session, flash) click to toggle source

Encodes the session value and a list of flash messages into a session header which can be returned to the user.

@param session [String] the session value @param flash [Array<String>] the flash messages, which must all be `valid_message?`

@return [String] the encoded session header value

# File lib/govuk_personalisation/flash.rb, line 32
def self.encode_session(session, flash)
  if flash.blank?
    session
  else
    "#{session}#{SESSION_SEPARATOR}#{flash.join(MESSAGE_SEPARATOR)}"
  end
end
valid_message?(message) click to toggle source

Check if a string is valid as a flash message.

@param message [String, nil] the flash message

@return [true, false] whether the message is valid or not.

# File lib/govuk_personalisation/flash.rb, line 45
def self.valid_message?(message)
  return false if message.nil?

  message.match? MESSAGE_REGEX
end