def initialize(request_uri, pairs)
@request_uri = request_uri
@pairs = pairs
pair = @pairs.find {|k, v| /\Adomain\z/i =~ k }
if !pair || /\A\d+(?:\.\d+)+\z/ =~ request_uri.host
@domain = request_uri.host
@domain_pat = /\A#{Regexp.quote @domain}\z/i
else
cookie_domain = pair[1]
if /\A\./ !~ cookie_domain
raise ArgumentError, "An cookie domain not started with a dot: #{cookie_domain}"
end
if /\..*\./ !~ cookie_domain
raise ArgumentError, "An cookie domain needs more dots: #{cookie_domain}"
end
if /#{Regexp.quote cookie_domain}\z/ !~ request_uri.host
raise ArgumentError, "An cookie domain is not match: #{cookie_domain} is not suffix of #{request_uri.host}"
end
@domain = cookie_domain
@domain_pat = /#{Regexp.quote cookie_domain}\z/i
end
pair = @pairs.find {|k, v| /\Apath\z/i =~ k }
if !pair
@path = request_uri.path.sub(%r{[^/]*\z}, '')
@path_pat = /\A#{Regexp.quote @path}/
else
cookie_path = pair[1]
sep = %r{/\z} =~ cookie_path ? "" : '(\z|/)'
if %r{\A#{Regexp.quote cookie_path}#{sep}} !~ request_uri.path
raise ArgumentError, "An cookie path is not match: #{cookie_path} is not prefix of #{request_uri.path}"
end
@path = cookie_path
@path_pat = /\A#{Regexp.quote cookie_path}#{sep}/
end
end