Sha256: d3d5e040a8cf5f4146f8c5ef08a9fc21ee3b3a3ab8870215f3a8d4e0970c1954

Contents?: true

Size: 962 Bytes

Versions: 48

Compression:

Stored size: 962 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) == 0
        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

48 entries across 47 versions & 2 rubygems

Version Path
excon-1.0.0 lib/excon/middlewares/capture_cookies.rb
excon-0.112.0 lib/excon/middlewares/capture_cookies.rb
excon-0.111.0 lib/excon/middlewares/capture_cookies.rb
excon-0.110.0 lib/excon/middlewares/capture_cookies.rb
excon-0.109.0 lib/excon/middlewares/capture_cookies.rb
excon-0.108.0 lib/excon/middlewares/capture_cookies.rb
excon-0.107.0 lib/excon/middlewares/capture_cookies.rb
excon-0.106.0 lib/excon/middlewares/capture_cookies.rb
excon-0.105.0 lib/excon/middlewares/capture_cookies.rb
excon-0.104.0 lib/excon/middlewares/capture_cookies.rb
excon-0.103.0 lib/excon/middlewares/capture_cookies.rb
excon-0.102.0 lib/excon/middlewares/capture_cookies.rb
excon-0.101.0 lib/excon/middlewares/capture_cookies.rb
excon-0.100.0 lib/excon/middlewares/capture_cookies.rb
excon-0.99.0 lib/excon/middlewares/capture_cookies.rb
excon-0.98.0 lib/excon/middlewares/capture_cookies.rb
excon-0.97.2 lib/excon/middlewares/capture_cookies.rb
excon-0.97.1 lib/excon/middlewares/capture_cookies.rb
excon-0.97.0 lib/excon/middlewares/capture_cookies.rb
excon-0.96.0 lib/excon/middlewares/capture_cookies.rb