CookieMonster
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.
Installation
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
Usage
require 'cookie_monster'
cookie = CookieMonster.new
cookie = CookieMonster.new({ :name => 'cookiename', :value => 'cookievalue', :httponly => true })
cookie = CookieMonster.new 'Cookie: name=value; domain=.domain.com; path=/; httponly'
cookie = CookieMonster.new 'name=value; domain=.domain.com; path=/; httponly; secure'
cookie.name = 'myShinyCookie'
cookie.value = 'test|123|zyx'
cookie.path = '/'
cookie.domain = '.my.domain.com'
cookie.expires = Time.now + 7*86400
cookie.httponly = true
cookie.secure = true
puts cookie.name
puts cookie.value
puts cookie.path
puts cookie.domain
puts cookie.expires
puts cookie.httponly
puts cookie.secure
if cookie.is_valid?
puts ":-)"
else
puts ":-("
end
cookie.validate!
cookie.expires_in_seconds(60)
cookie.expires_in_seconds(3600)
cookie.expires_in_seconds(86400)
cookie.expires_in_seconds(-60)
cookie.expire!
cookie.delete!
cookie.to_s
cookie.to_header
begin
puts cookie.to_header
rescue => ex
puts "Failed to generate cookie: #{ex}"
end
Development
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 rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/hirschnase/CookieMonster.
License
The gem is available as open source under the terms of the MIT License.