A simple helper Class to parse / generate HTTP Cookies

gem install http_cookie_monster -v 0.1.3



CookieMonster is a Ruby library to work with HTTP Cookies. It allows to parse cookies from a HTTP header into an object and to convert the object back to a cookie string or response header.


Add this line to your application's Gemfile:

gem 'http_cookie_monster'

And then execute:

$ bundle

Or install it yourself as:

$ gem install http_cookie_monster


# include the gem into your code
require 'cookie_monster'

# a new cookie-monster object
cookie =

# a new cookie-monster object with initial attributes
cookie ={ :name => 'cookiename', :value => 'cookievalue', :httponly => true })

# a new cookie-monster object from a raw cookie HTTP request header
cookie = 'Cookie: name=value;; path=/; httponly'

# a new cookie-monster object from a cookie string
cookie = 'name=value;; path=/; httponly; secure'

# please note:
# name and value will be automatically URL decoded when parsed
# and automatically URL encoded when being set

# the following cookie attributes can be set
#     = 'myShinyCookie'          # the name will be URL encoded
cookie.value    = 'test|123|zyx'           # the value will be URL encoded as well
cookie.path     = '/'
cookie.domain   = ''
cookie.expires  = + 7*86400       # must be a Time object
cookie.httponly = true   = true

# the attributes can be accessed like this
puts cookie.value
puts cookie.path
puts cookie.domain
puts cookie.expires
puts cookie.httponly

# there are some helper methods available

# a new cookie string can be parsed like this
cookie.parse('name=value;; path=/; httponly; secure')

# or directly from a HTTP request header string
cookie.from_header('Cookie: name=value;; path=/; httponly; secure')

# is_valid? does some basic checks: is the cookie name set and is the expiry a Time object (if it's defined)
if cookie.is_valid?
  puts ":-)"
  puts ":-("

# runs the validation and throws exceptions

# sets the cookie expiry
cookie.expires_in_seconds(60)      # in 60 seconds
cookie.expires_in_seconds(3600)    # in 1 hour
cookie.expires_in_seconds(86400)   # in 1 day
cookie.expires_in_seconds(-60)     # expires the cookie in the browser

# expire cookies
cookie.expire!      # sets the expire date to a date in the past
cookie.delete!      # removes the cookie value

# export the cookie object - runs validate! and throws a RuntimeError exception if the cookie is invalid!
cookie.to_s         # convert to a string
cookie.to_header    # convert to a HTTP response header

# ... so this would be better
  puts cookie.to_header
rescue => ex
  puts "Failed to generate cookie: #{ex}"


After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at


The gem is available as open source under the terms of the MIT License.