class Shamu::Rack::Cookies

Expose the request cookies as a hash.

Attributes

cookies[R]
deleted_cookies[R]
env[R]

Public Class Methods

create( * ) click to toggle source

@return [Cookies]

# File lib/shamu/rack/cookies.rb, line 8
def self.create( * )
  fail "Add Shamu::Rack::CookiesMiddleware to use Shamu::Rack::Cookies"
end
new( env ) click to toggle source

@param [Hash] env the Rack environment

# File lib/shamu/rack/cookies.rb, line 13
def initialize( env )
  @env = env
  @cookies = {}
  @deleted_cookies = []
end

Public Instance Methods

[]( key )
Alias for: get
[]=( key, value )
Alias for: set
apply!( headers ) click to toggle source

Apply the cookies {#set} or {#delete deleted} to the actual rack response headers.

Modifies the `headers` hash!

@param [Hash] headers from rack response @return [Hash] the modified headers with cookie values.

# File lib/shamu/rack/cookies.rb, line 26
def apply!( headers )
  cookies.each do |key, value|
    ::Rack::Utils.set_cookie_header! headers, key, value
  end

  deleted_cookies.each do |key|
    ::Rack::Utils.delete_cookie_header! headers, key
  end

  headers
end
delete( key ) click to toggle source

Delete a cookie from the browser. @param [String] key or name of the cookie. @return [self]

# File lib/shamu/rack/cookies.rb, line 89
def delete( key )
  cookies.delete( key )
  @deleted_cookies << key if env_cookies.key?( key )
  self
end
get( key ) click to toggle source

Get a cookie value from the browser. @param [String] key or name of the cookie @return [String] cookie value

# File lib/shamu/rack/cookies.rb, line 41
def get( key )
  key = key.to_s

  if cookie = cookies[ key ]
    cookie[:value]
  else
    env_cookies[ key ]
  end
end
Also aliased as: []
key?( name ) click to toggle source

@param [String] name @return [Boolean] true if the cookie has been set.

# File lib/shamu/rack/cookies.rb, line 54
def key?( name )
  cookies.key?( name ) || env_cookies.key?( name )
end
set( key, value ) click to toggle source

Set or update a cookie in the headers.

@overload set( key, value )

@param [String] key or name of the cookie
@param [String] value to assign

@overload set( key, hash )

@param [String] key or name of the cookie
@option hash [String] :value
@option hash [String] :domain
@option hash [String] :path
@option hash [Integer] :max_age
@option hash [Time] :expires
@option hash [Boolean] :secure
@option hash [Boolean] :http_only

@return [self]

# File lib/shamu/rack/cookies.rb, line 75
def set( key, value )
  key = key.to_s
  deleted_cookies.delete( key )

  value = { value: value } unless value.is_a? Hash
  cookies[key] = value

  self
end
Also aliased as: []=

Private Instance Methods

env_cookies() click to toggle source
# File lib/shamu/rack/cookies.rb, line 101
def env_cookies
  @env_cookies ||= begin
    @env_cookies = {}
    string = env[ "HTTP_COOKIE" ]

    # Cribbed from Rack::Request#cookies
    parsed = ::Rack::Utils.parse_query( string, ";," ) { |s| ::Rack::Utils.unescape( s ) rescue s } # rubocop:disable Style/RescueModifier, Metrics/LineLength
    parsed.each do |k, v|
      @env_cookies[ k ] = Array === v ? v.first : v
    end
  end
end