Sha256: 4e32d8b0bd82964b8e940bffb72db374d11d3d8a77da217d492c789e729f7db9

Contents?: true

Size: 1.8 KB

Versions: 58

Compression:

Stored size: 1.8 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # The cookies plugin adds response methods for handling cookies.
    # Currently, you can set cookies with +set_cookie+ and delete cookies
    # with +delete_cookie+:
    #
    #   response.set_cookie('foo', 'bar')
    #   response.delete_cookie('foo')
    #
    # Pass a hash of cookie options when loading the plugin to set some
    # defaults for all cookies upon setting and deleting. This is particularly
    # useful for configuring the +domain+ and +path+ of all cookies.
    #
    #   plugin :cookies, domain: 'example.com', path: '/api'
    module Cookies
      # Allow setting default cookie options when loading the cookies plugin.
      def self.configure(app, opts={})
        app.opts[:cookies_opts] = (app.opts[:cookies_opts]||{}).merge(opts).freeze
      end

      module ResponseMethods
        # Modify the headers to include a Set-Cookie value that
        # deletes the cookie.  A value hash can be provided to
        # override the default one used to delete the cookie.
        # Example:
        #
        #   response.delete_cookie('foo')
        #   response.delete_cookie('foo', domain: 'example.org')
        def delete_cookie(key, value = {})
          ::Rack::Utils.delete_cookie_header!(@headers, key, roda_class.opts[:cookies_opts].merge(value))
        end

        # Set the cookie with the given key in the headers.
        #
        #   response.set_cookie('foo', 'bar')
        #   response.set_cookie('foo', value: 'bar', domain: 'example.org')
        def set_cookie(key, value)
          value = { :value=>value } unless value.respond_to?(:keys)
          ::Rack::Utils.set_cookie_header!(@headers, key, roda_class.opts[:cookies_opts].merge(value))
        end
      end
    end

    register_plugin(:cookies, Cookies)
  end
end

Version data entries

58 entries across 58 versions & 1 rubygems

Version Path
roda-3.55.0 lib/roda/plugins/cookies.rb
roda-3.54.0 lib/roda/plugins/cookies.rb
roda-3.53.0 lib/roda/plugins/cookies.rb
roda-3.52.0 lib/roda/plugins/cookies.rb
roda-3.51.0 lib/roda/plugins/cookies.rb
roda-3.50.0 lib/roda/plugins/cookies.rb
roda-3.49.0 lib/roda/plugins/cookies.rb
roda-3.48.0 lib/roda/plugins/cookies.rb
roda-3.47.0 lib/roda/plugins/cookies.rb
roda-3.46.0 lib/roda/plugins/cookies.rb
roda-3.45.0 lib/roda/plugins/cookies.rb
roda-3.44.0 lib/roda/plugins/cookies.rb
roda-3.43.1 lib/roda/plugins/cookies.rb
roda-3.43.0 lib/roda/plugins/cookies.rb
roda-3.42.0 lib/roda/plugins/cookies.rb
roda-3.41.0 lib/roda/plugins/cookies.rb
roda-3.40.0 lib/roda/plugins/cookies.rb
roda-3.39.0 lib/roda/plugins/cookies.rb
roda-3.38.0 lib/roda/plugins/cookies.rb
roda-3.37.0 lib/roda/plugins/cookies.rb