Sha256: cb6e8db2559c0213e9a6e7be2848858d5c0392f188145f0cbd83877796401038

Contents?: true

Size: 958 Bytes

Versions: 6

Compression:

Stored size: 958 Bytes

Contents

# frozen_string_literal: true
module Excon
  module Middleware
    class CaptureCookies < Excon::Middleware::Base

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

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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
excon-1.2.3 lib/excon/middlewares/capture_cookies.rb
excon-1.2.2 lib/excon/middlewares/capture_cookies.rb
excon-1.2.1 lib/excon/middlewares/capture_cookies.rb
excon-1.2.0 lib/excon/middlewares/capture_cookies.rb
excon-1.1.1 lib/excon/middlewares/capture_cookies.rb
excon-1.1.0 lib/excon/middlewares/capture_cookies.rb