Guest User

get_cookies rewriting

a guest
Feb 18th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.74 KB | None | 0 0
  1. # I shoulde re-write this:
  2.  
  3. @cookies = res.headers['Set-Cookie']    
  4. @cookies = @cookies.split(" ")[0]
  5.  
  6. # using get_cookies method, and perhaps Regex:
  7.  
  8. def get_cookies
  9.   cookies = ""
  10.   if (self.headers.include?('Set-Cookie'))
  11.     set_cookies = self.headers['Set-Cookie']
  12.     key_vals = set_cookies.scan(/\s?([^, ;]+?)=([^, ;]*?);/)
  13.     key_vals.each do |k, v|
  14.       # Dont downcase actual cookie name as may be case sensitive
  15.       name = k.downcase
  16.       next if name == 'path'
  17.       next if name == 'expires'
  18.       next if name == 'domain'
  19.       next if name == 'max-age'
  20.       cookies << "#{k}=#{v}; "
  21.     end
  22.   end
  23.  
  24.   return cookies.strip
  25. end
  26.  
  27. # @cookies = res.get_cookies.sub(/;$/, "")
  28. # It's ok?
  29. # p.s I'm newbie in regex
Advertisement
Add Comment
Please, Sign In to add comment