class CookieMonster

Constants

VERSION

Attributes

domain[RW]
expires[RW]
httponly[RW]
name[RW]
path[RW]
secure[RW]
value[RW]

Public Class Methods

new(args=nil) click to toggle source

init - a raw cookie can be passed for parsing. a hash with attributes can be handed in as well.

# File lib/cookie_monster.rb, line 17
def initialize(args=nil)
  from_header(args) if args.is_a? String
  if args.is_a? Hash
    args.each do |k,v|
      _set([k,v])
    end
  end
  self
end

Public Instance Methods

_set(kv) click to toggle source

helper method to mass asign attributes

# File lib/cookie_monster.rb, line 29
def _set(kv)
  case kv.first.to_sym
  when :name
    @name = kv.last
  when :value
    @value = kv.last
  when :path
    @path = kv.last
  when :domain
    @domain = kv.last
  when :expires
    @expires = Time.parse(kv.last.gsub('GMT', 'UTC'))
  when :httponly
    @httponly = true
  when :secure
    @secure = true
  end
end
delete!() click to toggle source

deletes the cookie by removing it's value - browsers will then delete the cookie from their jar

# File lib/cookie_monster.rb, line 104
def delete!
  @value = ''
  self
end
expire!() click to toggle source

expires the cookie by settings its expire date to yesterday - browsers will then delete the cookie from their jar

# File lib/cookie_monster.rb, line 97
def expire!
  @expires = Time.now.utc - 86400;
  self
end
expires_in_seconds(secs) click to toggle source

sets the cookie expiry in seconds from now

# File lib/cookie_monster.rb, line 90
def expires_in_seconds(secs)
  @expires = Time.now.utc + secs;
  self
end
from_header(header) click to toggle source

parse a cookie header string

# File lib/cookie_monster.rb, line 50
def from_header(header)
  parse(header.gsub(/^set-cookie:\s*/i, '').gsub(/^cookie:\s*/i, ''))
end
is_valid?() click to toggle source

validates the cookie - returns true or false

# File lib/cookie_monster.rb, line 118
def is_valid?
  begin
    validate!
    true
  rescue => ex
    false
  end
end
parse(raw_cookie) click to toggle source

parse a raw cookie string

# File lib/cookie_monster.rb, line 56
def parse(raw_cookie)
  cookie = raw_cookie.split(/\;\s*/)

  kv = cookie.shift
  @name   = URI.unescape(kv.split('=').first)
  @value  = URI.unescape(kv.split('=').last)

  cookie.each do |kv|
    _set(kv.split('='))
  end

end
to_header() click to toggle source

convert the cookie to a HTTP response header

# File lib/cookie_monster.rb, line 83
def to_header
  validate!
  "Set-Cookie: " + to_s
end
to_s() click to toggle source

convert the cookie to a string

# File lib/cookie_monster.rb, line 71
def to_s
  validate!
  URI.escape(name.to_s) + '=' + (value ? URI.escape(value.to_s) : '') +
  (domain   ? '; domain='+domain        : '') +
  (path     ? '; path='+path            : '') +
  (expires  ? '; expires='+expires.to_time.utc.strftime('%a, %d-%b-%Y %T GMT') : '') +
  (httponly ? '; httponly'              : '') +
  (secure   ? '; secure'                : '')
end
validate!() click to toggle source

runs some simple validation against the cookie - throws exceptions!

# File lib/cookie_monster.rb, line 111
def validate!
  raise 'Cookie name must be defined' unless name
  raise 'Cookie expires is not an instance of the Time class' if expires and ! expires.is_a? Time
end