You can set and access cookies/session values in Lucky like this:
class FooAction < BrowserAction get "/foo" do cookies.set("name", "Sally") cookies.get("name") # Will return "Sally" cookies.get?("person") # Will return nil # You can use Symbol for your key as well session.set(:name, "Sally") session.get(:name) # Will return "Sally" session.get("person") # oops! An exception is raised because this key doesn't exist plain_text "Cookies!" end end
Cookies are encrypted by Lucky by default when you use
set. If you
need to set a raw value unencrypted, Lucky gives you that option:
cookies.set_raw("name", "Sally") # Sets a raw unencrypted cookie cookies.get_raw("name") # Will return "Sally"
If you need to remove a session, or delete a cookie.
# Delete a specific session key session.delete(:name) session.deleted?(:name) #=> true # Clear the current session session.clear # Delete a specific cookie cookies.delete("name") cookies.deleted?("person") #=> false # Delete all cookies cookies.clear
# This gives you a HTTP::Cookie or raise an exception c_is_for_cookie = cookies.get_raw("cookiedough") # Set the name of the cookie to "cookie-dough" c_is_for_cookie.name("cookie-dough") # Set the value of cookie-dough to "yum" c_is_for_cookie.value("yum") # Set the path for the cookie to "/baking" c_is_for_cookie.path("/baking") # Set the expires date to tomorrow c_is_for_cookie.expires(1.day.from_now) # Set the domain to cookie.monst.er c_is_for_cookie.domain("cookie.monst.er") # Make the cookie secure c_is_for_cookie.secure(true) # Set the cookie to HTTP only c_is_for_cookie.http_only(true)