Class: NaiveCookie
- Inherits:
-
Object
- Object
- NaiveCookie
- Defined in:
- lib/naive_cookie.rb
Instance Method Summary collapse
- #error(text) ⇒ Object
- #expires ⇒ Object
- #http_only? ⇒ Boolean
-
#initialize(cookie_str) ⇒ NaiveCookie
constructor
Super naive cookie implementation It is not meant in any way to check the validity of the cookie It is only meant to check specific properties of cookies that are assumed to be present.
- #name ⇒ Object
- #path ⇒ Object
- #same_site ⇒ Object
- #secure? ⇒ Boolean
- #to_s ⇒ Object
- #validate!(rules) ⇒ Object
Constructor Details
#initialize(cookie_str) ⇒ NaiveCookie
Super naive cookie implementation It is not meant in any way to check the validity of the cookie It is only meant to check specific properties of cookies that are assumed to be present.
6 7 8 9 10 |
# File 'lib/naive_cookie.rb', line 6 def initialize() @data = .split("; ").map{ |s| s.index('=') ? s.split('=') : [s, true] }.to_h end |
Instance Method Details
#error(text) ⇒ Object
38 39 40 |
# File 'lib/naive_cookie.rb', line 38 def error(text) return "\t\t👺 #{text}" end |
#expires ⇒ Object
66 67 68 |
# File 'lib/naive_cookie.rb', line 66 def expires @data[@data.keys.find{ |item| item.downcase == "expires"}] end |
#http_only? ⇒ Boolean
50 51 52 |
# File 'lib/naive_cookie.rb', line 50 def http_only? !@data.keys.find{ |item| item.downcase == "httponly"}.nil? end |
#name ⇒ Object
62 63 64 |
# File 'lib/naive_cookie.rb', line 62 def name @data.keys[0] end |
#path ⇒ Object
58 59 60 |
# File 'lib/naive_cookie.rb', line 58 def path @data[@data.keys.find{ |item| item.downcase == "path"}] end |
#same_site ⇒ Object
54 55 56 |
# File 'lib/naive_cookie.rb', line 54 def same_site @data[@data.keys.find{ |item| item.downcase == "samesite"}] end |
#secure? ⇒ Boolean
46 47 48 |
# File 'lib/naive_cookie.rb', line 46 def secure? !@data.keys.find{ |item| item.downcase == "secure"}.nil? end |
#to_s ⇒ Object
42 43 44 |
# File 'lib/naive_cookie.rb', line 42 def to_s "#{self.name}, #{self.path}" end |
#validate!(rules) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/naive_cookie.rb', line 12 def validate!(rules) errors = [] if rules.key?("Path") && self.path != rules["Path"] errors.push(error("Path #{self.path} not matching #{rules["Path"]}.")) end if rules.key?("Secure") && self.secure? != rules["Secure"] errors.push(error("Cookie not secure.")) end if rules.key?("HttpOnly") && self.http_only? != rules["HttpOnly"] errors.push(error("Cookie expected to be set as HttpOnly.")) end if rules.key?("SameSite") && self.same_site != rules["SameSite"] errors.push(error("SameSite #{self.same_site} not matching #{rules["SameSite"]}.")) end if errors.length > 0 return errors else return nil end end |