Class: SonarCookies::Cookie
- Inherits:
-
Object
- Object
- SonarCookies::Cookie
- Includes:
- Rack::Utils
- Defined in:
- lib/sonar/cookies.rb
Instance Attribute Summary collapse
-
#default_host ⇒ Object
readonly
Returns the value of attribute default_host.
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#expires ⇒ Object
readonly
Returns the value of attribute expires.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #<=>(cookie) ⇒ Object
- #dispose_for?(uri) ⇒ Boolean
- #empty? ⇒ Boolean
- #expired? ⇒ Boolean
-
#initialize(raw, uri) ⇒ Cookie
constructor
A new instance of Cookie.
- #replaces?(cookie) ⇒ Boolean
- #secure? ⇒ Boolean
- #valid?(uri = nil) ⇒ Boolean
Constructor Details
#initialize(raw, uri) ⇒ Cookie
Returns a new instance of Cookie.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sonar/cookies.rb', line 69 def initialize raw, uri @default_host = ::SonarConstants::DEFAULT_HOST uri ||= default_uri uri.host ||= default_host @raw, @options = raw.split(/[;,] */n, 2) @name, @value = parse_query(@raw, ';').to_a.first @options = parse_query(@options, ';') @domain = @options['domain'] || uri.host || default_host @domain = '.' << @domain unless @domain =~ /\A\./ @path = @options['path'] || uri.path.sub(/\/[^\/]*\Z/, '') (expires = @options['expires']) && (@expires = ::Time.parse(expires)) end |
Instance Attribute Details
#default_host ⇒ Object (readonly)
Returns the value of attribute default_host.
67 68 69 |
# File 'lib/sonar/cookies.rb', line 67 def default_host @default_host end |
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
67 68 69 |
# File 'lib/sonar/cookies.rb', line 67 def domain @domain end |
#expires ⇒ Object (readonly)
Returns the value of attribute expires.
67 68 69 |
# File 'lib/sonar/cookies.rb', line 67 def expires @expires end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
67 68 69 |
# File 'lib/sonar/cookies.rb', line 67 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
67 68 69 |
# File 'lib/sonar/cookies.rb', line 67 def path @path end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
67 68 69 |
# File 'lib/sonar/cookies.rb', line 67 def raw @raw end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
67 68 69 |
# File 'lib/sonar/cookies.rb', line 67 def value @value end |
Instance Method Details
#<=>(cookie) ⇒ Object
116 117 118 |
# File 'lib/sonar/cookies.rb', line 116 def <=> [name, path, domain.reverse] <=> [.name, .path, .domain.reverse] end |
#dispose_for?(uri) ⇒ Boolean
103 104 105 |
# File 'lib/sonar/cookies.rb', line 103 def dispose_for? uri expired? ? false : valid?(uri) end |
#empty? ⇒ Boolean
91 92 93 |
# File 'lib/sonar/cookies.rb', line 91 def empty? value.nil? || value.empty? end |
#expired? ⇒ Boolean
99 100 101 |
# File 'lib/sonar/cookies.rb', line 99 def expired? expires && expires < ::Time.now.gmtime end |
#replaces?(cookie) ⇒ Boolean
87 88 89 |
# File 'lib/sonar/cookies.rb', line 87 def replaces? [name.downcase, domain, path] == [.name.downcase, .domain, .path] end |
#secure? ⇒ Boolean
95 96 97 |
# File 'lib/sonar/cookies.rb', line 95 def secure? @options.has_key?('secure') end |
#valid?(uri = nil) ⇒ Boolean
107 108 109 110 111 112 113 114 |
# File 'lib/sonar/cookies.rb', line 107 def valid? uri = nil uri ||= default_uri uri.host ||= default_host (secure? ? uri.scheme == 'https' : true) && (uri.host =~ /#{::Regexp.escape(domain.sub(/\A\./, ''))}\Z/i) && (uri.path =~ /\A#{::Regexp.escape(path)}/) end |