Class: Rack::Client::CookieJar::Cookie

Inherits:
Struct
  • Object
show all
Defined in:
lib/rack/client/middleware/cookie_jar/cookie.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts = {}) ⇒ Cookie

Returns a new instance of Cookie.



22
23
24
25
26
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 22

def initialize(parts = {})
  parts.each do |k,v|
    send(:"#{k}=", v)
  end
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain

Returns:

  • (Object)

    the current value of domain



4
5
6
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 4

def domain
  @domain
end

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



4
5
6
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 4

def key
  @key
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



4
5
6
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 4

def path
  @path
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



4
5
6
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 4

def value
  @value
end

Class Method Details

.from(header) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 14

def self.from(header)
  data  = header.split('; ')
  tuple = data.shift.split('=')
  parts = data.map {|s| s.split('=') }

  new parts.inject('key'=> tuple.first, 'value'=> tuple.last) {|h,(k,v)| h.update(k => v)}
end

.merge(bottom, top) ⇒ Object



6
7
8
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 6

def self.merge(bottom, top)
  bottom.reject {|a| top.any? {|b| a == b } } | top
end

.parse(raw) ⇒ Object



10
11
12
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 10

def self.parse(raw)
  raw.split(', ').map {|header| from(header) }
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 37

def eql?(other)
  to_key == other.to_key
end

#fuzzy_domain_equal(other_domain) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 45

def fuzzy_domain_equal(other_domain)
  if domain =~ /^\./
    other_domain =~ /#{Regexp.escape(domain)}$/
  else
    domain == other_domain
  end
end

#fuzzy_path_equal(other_path) ⇒ Object



53
54
55
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 53

def fuzzy_path_equal(other_path)
  path == '/' || path == other_path
end

#match?(domain, path) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 41

def match?(domain, path)
  fuzzy_domain_equal(domain) && fuzzy_path_equal(path)
end

#to_headerObject



32
33
34
35
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 32

def to_header
  hash = members.zip(values).inject({}) {|h,(k,v)| h.update(k => v) }.reject {|k,v| v.nil?}
  "#{hash.delete('key')}=#{hash.delete('value')}" << ('; ' + hash.map {|(k,v)| "#{k}=#{v}" } * '; ' unless hash.empty?)
end

#to_keyObject



28
29
30
# File 'lib/rack/client/middleware/cookie_jar/cookie.rb', line 28

def to_key
  [ key, domain, path ] * ';'
end