Git Product home page Git Product logo

nim-biscuits's Introduction

About

Biscuits is a Nim module for better cookie handling.

Differences from cookie module in standard library:

  • Better handling of multiple key-value pairs, particularly with regard to creating cookies.
  • Supports "max-age" cookie field.
  • Better separation of data from cookie special fields.
  • Better methods for editing cookies.
  • Object-oriented interface, if you like that kind of thing.

Examples:

# Start with a cookie, represented as a string.
var cookieExample : string = "username=John Doe; password=notverysecure; expires=Thu, 30 Dec 2015 12:00:00 UTC; path=/; secure"

# Parse the cookie.
var myBiscuit : Biscuit = parseBiscuit(cookieExample)

# If the password key is set and equal to "notverysecure", set it to something else.
if myBiscuit.hasKey("password") and myBiscuit.getKey("password") == "notverysecure":
    discard myBiscuit.setKey("password", "reallyshouldbechanged")

# Add a new key to the cookie.
discard myBiscuit.setKey("userLevel", "admin")

# Try to set the userLevel key to a different value, but don't overwrite if a value is already set.
discard myBiscuit.setKey("userLevel", "user", overwrite = false)
echo(myBiscuit.getKey("userLevel")) # still outputs "admin"

# Change the path of the cookie.
discard myBiscuit.setPath("/nimExample/")
echo(myBiscuit.getPath()) # ouputs "/nimExample/"

# Format the cookie as a string.
var cookieStr : string = $myBiscuit;
echo(cookieStr)
# outputs "userLevel=admin; username=John Doe; password=reallyshouldbechanged; path=/nimExample/; expires=Thu, 30 Dec 2015 12:00:00 UTC; secure"

# Create a new cookie. Can also be created with a string table, in order to use multiple key-value pairs.
var newBiscuit : Biscuit = createBiscuit("thisisakey", "thisisavalue", path = "/", maxAge = "300", httpOnly = true)
echo(newBiscuit.toString(includeName = true))
# outputs "Set-Cookie: thisisakey=thisisavalue; path=/; max-age=300; HttpOnly"

License

Biscuits is released under the MIT open source license.

nim-biscuits's People

Contributors

sdmcallister avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.