Class: Excon::Middleware::CaptureCookies

Inherits:
Base
  • Object
show all
Defined in:
lib/excon/middlewares/capture_cookies.rb

Instance Method Summary collapse

Methods inherited from Base

#error_call, #initialize, #request_call, valid_parameter_keys

Constructor Details

This class inherits a constructor from Excon::Middleware::Base

Instance Method Details



6
7
8
# File 'lib/excon/middlewares/capture_cookies.rb', line 6

def extract_cookies_from_set_cookie(set_cookie)
  set_cookie.split(',').map { |full| full.split(';').first.strip }.join('; ')
end

#get_header(datum, header) ⇒ Object



10
11
12
13
14
15
# File 'lib/excon/middlewares/capture_cookies.rb', line 10

def get_header(datum, header)
  _, header_value = datum[:response][:headers].detect do |key, _|
    key.casecmp(header) == 0
  end
  header_value
end

#response_call(datum) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/excon/middlewares/capture_cookies.rb', line 17

def response_call(datum)
  cookie = get_header(datum, 'Set-Cookie')
  if cookie
    cookie = extract_cookies_from_set_cookie(cookie)
    unless datum[:headers].key?("Cookie")
      datum[:headers]["Cookie"] = cookie
    else
      original_cookies = datum[:headers]["Cookie"]
      datum[:headers]["Cookie"] = original_cookies.empty? ? cookie : [original_cookies,cookie].join('; ')
    end
  end
  super(datum)
end