Module: Sinatra::Cookies

Defined in:
lib/sinatra/cookies.rb

Overview

Sinatra::Cookies

Easy way to deal with cookies

Usage

Allows you to read cookies:

get '/' do
  "value: #{cookie[:something]}"
end

And of course to write cookies:

get '/set' do
  cookies[:something] = 'foobar'
  redirect to('/')
end

And generally behaves like a hash:

get '/demo' do
  cookies.merge! 'foo' => 'bar', 'bar' => 'baz'
  cookies.keep_if { |key, value| key.start_with? 'b' }
  foo, bar = cookies.values_at 'foo', 'bar'
  "size: #{cookies.length}"
end

Classic Application

In a classic application simply require the helpers, and start using them:

require "sinatra"
require "sinatra/cookies"

# The rest of your classic application code goes here...

Modular Application

In a modular application you need to require the helpers, and then tell the application you will use them:

require "sinatra/base"
require "sinatra/cookies"

class MyApp < Sinatra::Base
  helpers Sinatra::Cookies

  # The rest of your modular application code goes here...
end

Defined Under Namespace

Classes: Jar

Instance Method Summary collapse

Instance Method Details

#cookiesObject



325
326
327
# File 'lib/sinatra/cookies.rb', line 325

def cookies
  @cookies ||= Jar.new(self)
end