module SiteHub::CookieRewriting

TODO: - change in to object and remove .reek exclusions for UtilityFunction

Constants

ENDING_WITH_NEWLINE

Public Instance Methods

cookies_hash_to_string(cookies_hash) click to toggle source
# File lib/sitehub/cookie_rewriting.rb, line 27
def cookies_hash_to_string(cookies_hash)
  cookies_hash.values.inject(EMPTY_STRING.dup) do |cookie_string, cookie|
    cookie_string << "#{cookie}#{NEW_LINE}"
  end.sub(ENDING_WITH_NEWLINE, EMPTY_STRING)
end
cookies_string_as_hash(cookie_string) click to toggle source
# File lib/sitehub/cookie_rewriting.rb, line 33
def cookies_string_as_hash(cookie_string)
  cookie_string.lines.each_with_object({}) do |cookie_line, cookies|
    cookie = SiteHub::Cookie.new(cookie_line)
    cookies[cookie.name] = cookie
  end
end
rewrite_cookies(headers, substitute_domain:) click to toggle source
# File lib/sitehub/cookie_rewriting.rb, line 8
def rewrite_cookies(headers, substitute_domain:)
  cookies_hash = cookies_string_as_hash(headers[Constants::HttpHeaderKeys::SET_COOKIE])

  cookies_hash.values.each do |cookie|
    domain_attribute = cookie.find(:domain) || next
    update_domain(domain_attribute, substitute_domain)
  end
  headers[Constants::HttpHeaderKeys::SET_COOKIE] = cookies_hash_to_string(cookies_hash)
end
update_domain(domain_attribute, substitute_domain) click to toggle source
# File lib/sitehub/cookie_rewriting.rb, line 18
def update_domain(domain_attribute, substitute_domain)
  substitute = substitute_domain.dup
  if domain_attribute.value.start_with?(FULL_STOP)
    domain_attribute.update(substitute.prepend(FULL_STOP))
  else
    domain_attribute.update(substitute)
  end
end