Class: Greed::Cookie::Jar
- Inherits:
-
Object
- Object
- Greed::Cookie::Jar
- Includes:
- Iterator
- Defined in:
- lib/greed/cookie/jar.rb
Instance Method Summary collapse
- #append_cookie(document_uri, cookie_hash) ⇒ Object
- #cookie_header_for(document_uri) ⇒ Object
- #cookie_record_for(document_uri) ⇒ Object
- #dump ⇒ Object
- #garbage_collect ⇒ Object
-
#initialize(state = nil, set_cookie_parser: nil, get_current_time: nil, calculate_expiration: nil, determine_domain: nil, determine_path: nil) ⇒ Jar
constructor
A new instance of Jar.
- #parse_set_cookie(document_uri, header) ⇒ Object
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 = || 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
#append_cookie(document_uri, cookie_hash) ⇒ Object
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 (document_uri, ) return false unless .present? current_time = @get_current_time.call parsed_document_uri = ::URI.parse(document_uri) base = .slice(:name, :value, :secure) domain_attributes = @determine_domain.call( parsed_document_uri.hostname, [:domain] ) path_attributes = @determine_path.call( parsed_document_uri.path, [: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, [:'max-age'], [: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 = path_holder.delete(base[:name]) return false unless domain_holder[final_path] = path_holder @cookie_map[final_domain] = domain_holder return true end |
#cookie_header_for(document_uri) ⇒ Object
100 101 102 103 104 |
# File 'lib/greed/cookie/jar.rb', line 100 def (document_uri) (document_uri).map do || "#{[:name]}=#{[:value]}" end.to_a.join('; ') end |
#cookie_record_for(document_uri) ⇒ Object
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 (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 (parsed_document_uri.path, is_secure, ip_address) if ip_address return [] unless domain_name.present? (parsed_document_uri.path, is_secure, domain_name) end |
#dump ⇒ Object
70 71 72 73 |
# File 'lib/greed/cookie/jar.rb', line 70 def dump garbage_collect @cookie_map.clone end |
#garbage_collect ⇒ Object
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 |, | ![:expires] || current_time < [: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 |
#parse_set_cookie(document_uri, header) ⇒ Object
106 107 108 |
# File 'lib/greed/cookie/jar.rb', line 106 def (document_uri, header) (document_uri, @set_cookie_parser.call(header)) end |