Class: SonarCookies::Cookie

Inherits:
Object
  • Object
show all
Includes:
Rack::Utils
Defined in:
lib/sonar/cookies.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_hostObject (readonly)

Returns the value of attribute default_host.



67
68
69
# File 'lib/sonar/cookies.rb', line 67

def default_host
  @default_host
end

#domainObject (readonly)

Returns the value of attribute domain.



67
68
69
# File 'lib/sonar/cookies.rb', line 67

def domain
  @domain
end

#expiresObject (readonly)

Returns the value of attribute expires.



67
68
69
# File 'lib/sonar/cookies.rb', line 67

def expires
  @expires
end

#nameObject (readonly)

Returns the value of attribute name.



67
68
69
# File 'lib/sonar/cookies.rb', line 67

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



67
68
69
# File 'lib/sonar/cookies.rb', line 67

def path
  @path
end

#rawObject (readonly)

Returns the value of attribute raw.



67
68
69
# File 'lib/sonar/cookies.rb', line 67

def raw
  @raw
end

#valueObject (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 <=> cookie
  [name, path, domain.reverse] <=> [cookie.name, cookie.path, cookie.domain.reverse]
end

#dispose_for?(uri) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/sonar/cookies.rb', line 103

def dispose_for? uri
  expired? ? false : valid?(uri)
end

#empty?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/sonar/cookies.rb', line 91

def empty?
  value.nil? || value.empty?
end

#expired?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/sonar/cookies.rb', line 99

def expired?
  expires && expires < ::Time.now.gmtime
end

#replaces?(cookie) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/sonar/cookies.rb', line 87

def replaces? cookie
  [name.downcase, domain, path] == [cookie.name.downcase, cookie.domain, cookie.path]
end

#secure?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/sonar/cookies.rb', line 95

def secure?
  @options.has_key?('secure')
end

#valid?(uri = nil) ⇒ Boolean

Returns:

  • (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