{"id":13880400,"url":"https://github.com/piotrmurach/rack-policy","last_synced_at":"2025-08-01T05:06:16.993Z","repository":{"id":3695368,"uuid":"4766056","full_name":"piotrmurach/rack-policy","owner":"piotrmurach","description":"Rack middleware for the EU ePrivacy Directive compliance in Ruby Web Apps","archived":false,"fork":false,"pushed_at":"2018-02-21T13:49:59.000Z","size":46,"stargazers_count":147,"open_issues_count":3,"forks_count":14,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-31T08:11:16.802Z","etag":null,"topics":["cookie","padrino","rack-middleware","rails","ruby","session-cookie","sinatra"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/piotrmurach.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-06-23T16:52:19.000Z","updated_at":"2023-08-31T13:24:03.000Z","dependencies_parsed_at":"2022-08-21T00:50:24.682Z","dependency_job_id":null,"html_url":"https://github.com/piotrmurach/rack-policy","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Frack-policy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Frack-policy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Frack-policy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Frack-policy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/piotrmurach","download_url":"https://codeload.github.com/piotrmurach/rack-policy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247633315,"owners_count":20970302,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cookie","padrino","rack-middleware","rails","ruby","session-cookie","sinatra"],"created_at":"2024-08-06T08:03:00.398Z","updated_at":"2025-04-07T10:25:01.512Z","avatar_url":"https://github.com/piotrmurach.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Rack-Policy\n[![Gem Version](https://badge.fury.io/rb/rack-policy.svg)][gem]\n[![Build Status](https://secure.travis-ci.org/piotrmurach/rack-policy.svg?branch=master)][travis]\n[![Code Climate](https://codeclimate.com/github/piotrmurach/rack-policy/badges/gpa.svg)][codeclimate]\n[![Dependency Status](https://gemnasium.com/piotrmurach/rack-policy.svg?travis)][gemnasium]\n[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/rack-policy/badge.svg)][coverage]\n[![Inline docs](http://inch-ci.org/github/piotrmurach/rack-policy.svg?branch=master)][inchpages]\n\n[gem]: http://badge.fury.io/rb/rack-policy\n[travis]: http://travis-ci.org/piotrmurach/rack-policy\n[codeclimate]: https://codeclimate.com/github/piotrmurach/rack-policy\n[gemnasium]: https://gemnasium.com/piotrmurach/rack-policy\n[coverage]: https://coveralls.io/github/piotrmurach/rack-policy\n[inchpages]: http://inch-ci.org/github/piotrmurach/rack-policy\n\n\u003e This is Rack middleware that makes your app compliant with the 'EU ePrivacy Directive' whereby a user needs to provide implied consent before any data can be stored on his machine.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'rack-policy'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install rack-policy\n\n## Usage\n\nBy default when the Rack application is loaded no cookies will be set(provided no session cookies already exist), and any existing session cookies will be destroyed. Throughout the request cycle cookies now won't be set until the user has given explicit consent. This can be controlled by setting consent token\n\n```ruby\nRack::Policy::CookieLimiter, consent_token: 'allow_me'\n```\n\nThe very same `consent_token` is used to toggle the limiter behaviour.\n\nThe `cookies_accepted?` view helper method is automatically loaded for Rails, Sinatra \u0026 Padrino apps.\n\n## Examples\n\nAdding `Rack::Policy::CookieLimiter` to Rack applications\n\n### Rails 3.x\n\n```ruby\n# config/application.rb\nrequire 'rack/policy'\n\nclass Application \u003c Rails::Application\n  config.middleware.insert_before ActionDispatch::Cookies, Rack::Policy::CookieLimiter, consent_token: 'rack.policy'\nend\n```\n\nAnd then in your custom controller create actions responsible for setting and unsetting cookie policy\n\n```ruby\nclass CookiePolicyController \u003c ApplicationController\n\n  def allow\n    response.set_cookie 'rack.policy', {\n      value: 'true',\n      path: '/',\n      expires: 1.year.from_now.utc\n    }\n    render nothing: true\n  end\n\n  def deny\n    response.delete_cookie 'rack.policy'\n    render nothing: true\n  end\nend\n```\n\nFinally, in your view you can use helper method `cookies_accepted?` to display/toggle cookie information\n\n```ruby\n\u003c% cookies_accepted? do %\u003e\n  Accepted Cookies!\n\u003c% end %\u003e\n\nor\n\n\u003c% if cookies_accepted? %\u003e\n  Accepted Cookies!\n\u003c% else %\u003e\n  Cookies Not Accepted!\n\u003c% end %\u003e\n```\n\n### Rails 2.x\n\n```ruby\n# config/environment\n\nRails::Initializer.run do |config|\n  require 'rack/policy'\n  config.middleware.insert_before Rack::Lock, Rack::Policy::CookieLimiter, consent_token: 'rack.policy'\nend\n```\n\nSet and unset cookie consent in your controller and modify views logic in similar way to Rails 3.x example.\n\n### Sinatra\n\nFor classic style sinatra application do\n\n```ruby\n#!/usr/bin/env ruby -rubygems\nrequire 'sinatra'\nrequire 'rack/policy'\n\nconfigure do\n  use Rack::Policy::CookieLimiter, consent_token: 'rack.policy'\nend\n\nget('/') { \"Allow cookies to be set? \u003ca href='/allow'\u003eAllow\u003c/a\u003e\" }\n\nget('/allow') { response.set_cookie 'rack.policy' }\n\nget('/deny') { response.delete_cookie 'rack.policy' }\n```\n\nSimiliar to Rails 3.x example you can use `cookies_accpeted?` helper to manage view logic related to cookie policy information.\n\n### Padrino\n\n```ruby\n#!/usr/bin/env ruby -rubygems\nrequire 'padrino'\nrequire 'rack/policy'\n\nclass MyApp \u003c Padrino::Application\n  use Rack::Policy::CookieLimiter, consent_token: 'rack.policy'\nend\n```\n\n### Rackup\n\n```ruby\n#!/usr/bin/env rackup\nrequire 'rack/policy'\n\nuse Rack::Policy::CookieLimiter, consent_token: 'rack.policy'\n\nrun lambda { |env| [200, {'Content-Type' =\u003e 'text/plain'}, \"Hello, world!\\n\"] }\n```\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Added some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n\nThis project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## Copyright\n\nCopyright (c) 2012-2016 Piotr Murach. See LICENSE for further details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrmurach%2Frack-policy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiotrmurach%2Frack-policy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrmurach%2Frack-policy/lists"}