Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dkarter/cookie_monster
🍪 HTTP Cookie Encoder and Decoder in pure Elixir with zero runtime dependencies.
https://github.com/dkarter/cookie_monster
cookie cookies elixir erlang http phoenix phoenix-framework
Last synced: 4 months ago
JSON representation
🍪 HTTP Cookie Encoder and Decoder in pure Elixir with zero runtime dependencies.
- Host: GitHub
- URL: https://github.com/dkarter/cookie_monster
- Owner: dkarter
- License: other
- Created: 2020-06-24T06:09:25.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-09T04:13:13.000Z (about 1 year ago)
- Last Synced: 2024-09-14T07:50:54.857Z (5 months ago)
- Topics: cookie, cookies, elixir, erlang, http, phoenix, phoenix-framework
- Language: Elixir
- Homepage:
- Size: 1.05 MB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# CookieMonster
![Build Status](https://github.com/dkarter/cookie_monster/actions/workflows/elixir.yml/badge.svg) [![Hex.pm](https://img.shields.io/hexpm/v/cookie_monster)](https://hex.pm/packages/cookie_monster)
A simple HTTP Cookie encoder and decoder in pure Elixir with zero runtime dependencies![^1].
![cookie monster logo](https://github.com/dkarter/cookie_monster/raw/main/img/cookie_monster.png)
## Motivation
I recently worked on an app that needed to parse cookies. Initially I reached
out for [Plug](https://hex.pm/packages/plug)'s implementation to encode
and decode the cookies, but that meant bringing in a (relatively) large dependency just to use a small portion of it (my application was not web facing).Another issue I had with Plug's implementation is that it used the name of the cookie as a map key in the decoding result:
```elixir
"hello=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; HttpOnly; Secure"
|> Plug.Conn.Cookies.decode()# => %{"Expires" => "Wed, 21 Oct 2015 07:28:00 GMT", "hello" => "a3fWa"}
```I wanted something a little simpler, and dependency free that makes it easy to extract the name and
value. As a bonus, I wanted to decode the date into an Elixir native DateTime
so that I can easily check if a cookie is expired.## Example
#### Encoding
```elixir
alias CookieMonster.Cookie%Cookie{
name: "id",
value: "a3fWa",
expires: ~U[2015-10-21 07:28:00Z],
http_only: true,
secure: true
}
|> CookieMonster.encode!()# => "id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; HttpOnly; Secure"
```#### Decoding
```elixir
alias CookieMonster.Cookie"id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; HttpOnly; Secure"
|> CookieMonster.decode!()# => %Cookie{name: "id", value: "a3fWa", expires: ~U[2015-10-21 07:28:00Z], http_only: true, secure: true}
```## Installation
The package can be installed by adding `cookie_monster` to your list of
dependencies in `mix.exs`:```elixir
def deps do
[
{:cookie_monster, "~> 1.1.6"}
]
end
```The docs can be found at [https://hexdocs.pm/cookie_monster](https://hexdocs.pm/cookie_monster).
[^1]: Boundary is listed as a dependency, but it is not a runtime dependency and only used during compile time to ensure proper design