Sha256: 3649a25b5d06547f4f1bbeea93dd0f49816dc16fe422dda1697f74ba520bca1c

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require "faraday"
require "http/cookie_jar"

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

    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
  end
end

if Faraday::Middleware.respond_to? :register_middleware
  Faraday::Middleware.register_middleware :cookie_jar => Faraday::CookieJar
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
faraday-cookie_jar-0.0.7 lib/faraday/cookie_jar.rb