Class: CookieMonster
- Inherits:
-
Object
- Object
- CookieMonster
- Defined in:
- lib/cookie_monster.rb,
lib/cookie_monster/version.rb
Constant Summary collapse
- VERSION =
"0.1.3"
Instance Attribute Summary collapse
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#expires ⇒ Object
Returns the value of attribute expires.
-
#httponly ⇒ Object
Returns the value of attribute httponly.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#secure ⇒ Object
Returns the value of attribute secure.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#_set(kv) ⇒ Object
helper method to mass asign attributes.
-
#delete! ⇒ Object
deletes the cookie by removing it’s value - browsers will then delete the cookie from their jar.
-
#expire! ⇒ Object
expires the cookie by settings its expire date to yesterday - browsers will then delete the cookie from their jar.
-
#expires_in_seconds(secs) ⇒ Object
sets the cookie expiry in seconds from now.
-
#from_header(header) ⇒ Object
parse a cookie header string.
-
#initialize(args = nil) ⇒ CookieMonster
constructor
init - a raw cookie can be passed for parsing.
-
#is_valid? ⇒ Boolean
validates the cookie - returns true or false.
-
#parse(raw_cookie) ⇒ Object
parse a raw cookie string.
-
#to_header ⇒ Object
convert the cookie to a HTTP response header.
-
#to_s ⇒ Object
convert the cookie to a string.
-
#validate! ⇒ Object
runs some simple validation against the cookie - throws exceptions!.
Constructor Details
#initialize(args = nil) ⇒ CookieMonster
init - a raw cookie can be passed for parsing. a hash with attributes can be handed in as well.
17 18 19 20 21 22 23 24 25 |
# 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 |
Instance Attribute Details
#domain ⇒ Object
Returns the value of attribute domain.
9 10 11 |
# File 'lib/cookie_monster.rb', line 9 def domain @domain end |
#expires ⇒ Object
Returns the value of attribute expires.
11 12 13 |
# File 'lib/cookie_monster.rb', line 11 def expires @expires end |
#httponly ⇒ Object
Returns the value of attribute httponly.
12 13 14 |
# File 'lib/cookie_monster.rb', line 12 def httponly @httponly end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/cookie_monster.rb', line 7 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
10 11 12 |
# File 'lib/cookie_monster.rb', line 10 def path @path end |
#secure ⇒ Object
Returns the value of attribute secure.
13 14 15 |
# File 'lib/cookie_monster.rb', line 13 def secure @secure end |
#value ⇒ Object
Returns the value of attribute value.
8 9 10 |
# File 'lib/cookie_monster.rb', line 8 def value @value end |
Instance Method Details
#_set(kv) ⇒ Object
helper method to mass asign attributes
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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! ⇒ Object
deletes the cookie by removing it’s value - browsers will then delete the cookie from their jar
104 105 106 107 |
# File 'lib/cookie_monster.rb', line 104 def delete! @value = '' self end |
#expire! ⇒ Object
expires the cookie by settings its expire date to yesterday - browsers will then delete the cookie from their jar
97 98 99 100 |
# File 'lib/cookie_monster.rb', line 97 def expire! @expires = Time.now.utc - 86400; self end |
#expires_in_seconds(secs) ⇒ Object
sets the cookie expiry in seconds from now
90 91 92 93 |
# File 'lib/cookie_monster.rb', line 90 def expires_in_seconds(secs) @expires = Time.now.utc + secs; self end |
#from_header(header) ⇒ Object
parse a cookie header string
50 51 52 |
# 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? ⇒ Boolean
validates the cookie - returns true or false
118 119 120 121 122 123 124 125 |
# File 'lib/cookie_monster.rb', line 118 def is_valid? begin validate! true rescue => ex false end end |
#parse(raw_cookie) ⇒ Object
parse a raw cookie string
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cookie_monster.rb', line 56 def parse() = .split(/\;\s*/) kv = .shift @name = URI.unescape(kv.split('=').first) @value = URI.unescape(kv.split('=').last) .each do |kv| _set(kv.split('=')) end end |
#to_header ⇒ Object
convert the cookie to a HTTP response header
83 84 85 86 |
# File 'lib/cookie_monster.rb', line 83 def to_header validate! "Set-Cookie: " + to_s end |
#to_s ⇒ Object
convert the cookie to a string
71 72 73 74 75 76 77 78 79 |
# 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! ⇒ Object
runs some simple validation against the cookie - throws exceptions!
111 112 113 114 |
# 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 |