Class: Greed::Cookie::Jar

Inherits:
Object
  • Object
show all
Includes:
Iterator
Defined in:
lib/greed/cookie/jar.rb

Instance Method Summary collapse

Constructor Details

#initialize(state = nil, set_cookie_parser: nil, get_current_time: nil, calculate_expiration: nil, determine_domain: nil, determine_path: nil) ⇒ Jar

Returns a new instance of Jar.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/greed/cookie/jar.rb', line 14

def initialize(\
  state = nil,
  set_cookie_parser: nil,
  get_current_time: nil,
  calculate_expiration: nil,
  determine_domain: nil,
  determine_path: nil
)
  @set_cookie_parser = set_cookie_parser || Parser.new.method(:parse)
  @get_current_time = get_current_time || ::Time.method(:current)
  @calculate_expiration = calculate_expiration || ExpirationHandler.new.method(:calculate_expiration)
  @determine_domain = determine_domain || DomainHandler.new.method(:determine_domain)
  @determine_path = determine_path || PathHandler.new.method(:determine_path)
  @cookie_map = state || {}
end

Instance Method Details



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/greed/cookie/jar.rb', line 30

def append_cookie(document_uri, cookie_hash)
  return false unless cookie_hash.present?
  current_time = @get_current_time.call
  parsed_document_uri = ::URI.parse(document_uri)
  base = cookie_hash.slice(:name, :value, :secure)
  domain_attributes = @determine_domain.call(
    parsed_document_uri.hostname,
    cookie_hash[:domain]
  )
  path_attributes = @determine_path.call(
    parsed_document_uri.path,
    cookie_hash[:path]
  )
  final_domain = domain_attributes[:domain]
  final_path = path_attributes[:path]
  domain_holder = @cookie_map[final_domain].try(:clone) || {}
  path_holder = domain_holder[final_path].try(:clone) || {}
  expires_attributes = @calculate_expiration.call(
    current_time,
    cookie_hash[:'max-age'],
    cookie_hash[:expires]
  )
  path_holder[base[:name]] = base.merge(
    domain_attributes,
    expires_attributes,
    path_attributes,
  )
  domain_holder[final_path] = path_holder
  @cookie_map[final_domain] = domain_holder
  true
rescue DomainError, PathError
  return false
rescue Expired
  removed_cookie = path_holder.delete(base[:name])
  return false unless removed_cookie
  domain_holder[final_path] = path_holder
  @cookie_map[final_domain] = domain_holder
  return true
end


100
101
102
103
104
# File 'lib/greed/cookie/jar.rb', line 100

def cookie_header_for(document_uri)
  cookie_record_for(document_uri).map do |cookie_record|
    "#{cookie_record[:name]}=#{cookie_record[:value]}"
  end.to_a.join('; ')
end


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/greed/cookie/jar.rb', line 75

def cookie_record_for(document_uri)
  parsed_document_uri = begin
    ::URI.parse(document_uri)
  rescue ::URI::Error
    return []
  end
  is_secure = case parsed_document_uri
  when ::URI::HTTPS
    true
  when ::URI::HTTP
    false
  else
    return []
  end
  domain_name = parsed_document_uri.hostname
  ip_address = begin
    ::IPAddr.new(domain_name)
  rescue ::IPAddr::Error
    nil
  end
  return cookie_for_ip_address(parsed_document_uri.path, is_secure, ip_address) if ip_address
  return [] unless domain_name.present?
  cookie_for_domain(parsed_document_uri.path, is_secure, domain_name)
end

#dumpObject



70
71
72
73
# File 'lib/greed/cookie/jar.rb', line 70

def dump
  garbage_collect
  @cookie_map.clone
end

#garbage_collectObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/greed/cookie/jar.rb', line 110

def garbage_collect
  current_time = @get_current_time.call
  @cookie_map = @cookie_map.map do |domain_name, domain_holder|
    domain_holder.map do |path_name, path_holder|
      path_holder.select do |_cookie_name, cookie_record|
        !cookie_record[:expires] || current_time < cookie_record[:expires]
      end.yield_self do |filtered_result|
        break nil unless filtered_result.present?
        [path_name, filtered_result]
      end
    end.compact.to_h.yield_self do |filtered_result|
      break nil unless filtered_result.present?
      [domain_name, filtered_result]
    end
  end.compact.to_h
  nil
end


106
107
108
# File 'lib/greed/cookie/jar.rb', line 106

def parse_set_cookie(document_uri, header)
  append_cookie(document_uri, @set_cookie_parser.call(header))
end