module HTTParty_with_cookies

Attributes

cookies[R]

include HTTParty

Public Class Methods

included(base) click to toggle source
# File lib/httparty_with_cookies.rb, line 7
def self.included(base)
  base.extend HTTParty
  base.send :include, HTTParty
end

Public Instance Methods

request_cookies() click to toggle source
# File lib/httparty_with_cookies.rb, line 31
def request_cookies
  URI.unescape( @cookies.to_a.map do |cookie|
    cookie.join('=')
  end.join(';') )
end
set_cookies() click to toggle source
# File lib/httparty_with_cookies.rb, line 36
def set_cookies       #ugly hack to get the correct cookie array from the headers
  headers = nil       #3 years later: uglier meta hack to allow fetching headers from 3xx responses
  begin
    headers = @last_response.headers.instance_variable_get(:@header)
  rescue NoMethodError
    headers = @last_response.instance_variable_get(:@header)
  end

  return unless headers and headers['set-cookie']

  response_cookies = headers['set-cookie']
  return unless response_cookies and !response_cookies.empty?
  response_cookies = [ response_cookies ] if response_cookies.is_a? String
  response_cookies.each do |cookie|
    add_cookies( cookie.split(';')[0] )
  end
end

Private Instance Methods

add_cookies(*cookies) click to toggle source
# File lib/httparty_with_cookies.rb, line 55
def add_cookies *cookies
  @cookies||= {}
  cookies.each do |cookie|
    key, value = cookie.split '=', 2
    @cookies[ key ] = value
  end
end