Class: Faraday::CookieJar

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/cookie_jar.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ CookieJar

Returns a new instance of CookieJar.



6
7
8
9
# File 'lib/faraday/cookie_jar.rb', line 6

def initialize(app, options = {})
  super(app)
  @jar = options[:jar] || HTTP::CookieJar.new
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/faraday/cookie_jar.rb', line 11

def call(env)
  cookies = @jar.cookies(env[:url])
  unless cookies.empty?
    cookie_value = HTTP::Cookie.cookie_value(cookies)
    if env[:request_headers]["Cookie"]
      unless env[:request_headers]["Cookie"] == cookie_value
        env[:request_headers]["Cookie"] = cookie_value + ';' + env[:request_headers]["Cookie"]
      end
    else
      env[:request_headers]["Cookie"] = cookie_value
    end
  end

  @app.call(env).on_complete do |res|
    if res[:response_headers]
      if set_cookie = res[:response_headers]["Set-Cookie"]
        @jar.parse(set_cookie, env[:url])
      end
    end
  end
end