Module: HTTParty_with_cookies

Defined in:
lib/httparty_with_cookies.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cookiesObject (readonly)

include HTTParty



5
6
7
# File 'lib/httparty_with_cookies.rb', line 5

def cookies
  @cookies
end

Class Method Details

.included(base) ⇒ Object



7
8
9
10
# File 'lib/httparty_with_cookies.rb', line 7

def self.included(base)
  base.extend HTTParty
  base.send :include, HTTParty
end

Instance Method Details

#request_cookiesObject



31
32
33
34
35
# 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_cookiesObject

ugly hack to get the correct cookie array from the headers



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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