Class: RHACK::Cookie

Inherits:
Object show all
Defined in:
lib/rhack/cookie.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cookie

Returns a new instance of Cookie.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rhack/cookie.rb', line 7

def initialize(*args)
  if args[1].is Scout
    str, scout = *args
    ck = str//;\s*/
    ck[1..-1].each {|par|
      a = par/'='
      case a[0].downcase
        when 'path'; @path = (a[1] == '/') ? // : /^#{Regexp.escape a[1]}/
        when 'domain'; @domain = /(^|\.)#{Regexp.escape a[1].sub(/^./, '')}$/
        when 'expires'; @expires = a[1].to_time
      end
    }
    @name, @value = ck[0].split('=', 2)
    #@value.gsub!(/^['"]|['"]$/, '')
    #L.debug args if !@domain
    (scout.cookies[scout.uri.host] ||= {})[@name] = self
  else
    @name, cookie = args[0]
    case cookie
      when Array; @value, @path, @domain = cookie
      when Hash; @value, @path, @domain = cookie.value, cookie.path, cookie.domain
      else @value = args[1].to_s
    end
  end
  @path ||= //
  @domain ||= //
  @string = "#{@name}=#{@value}; "
end

Instance Method Details

#inspectObject



45
# File 'lib/rhack/cookie.rb', line 45

def inspect; @value.inspect end

#to_sObject



44
# File 'lib/rhack/cookie.rb', line 44

def to_s; @value end

#use(str, uri) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/rhack/cookie.rb', line 36

def use(str, uri)
  if !@expires or @expires > Time.now
    str << @string if uri.path[@path] and !uri.root || uri.host[@domain]
  else
    :expired
  end
end