Sha256: 28df3f0c3e6d4993f487fa0d11cf34d6a97898661ff240e039b3012f0b5dbbd5

Contents?: true

Size: 1.5 KB

Versions: 19

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module HTTPX
  module Plugins::Cookies
    # The Cookie Jar
    #
    # It holds a bunch of cookies.
    class Jar
      using URIExtensions

      include Enumerable

      def initialize_dup(orig)
        super
        @cookies = orig.instance_variable_get(:@cookies).dup
      end

      def initialize(cookies = nil)
        @cookies = []

        cookies.each do |elem|
          cookie = case elem
                   when Cookie
                     elem
                   when Array
                     Cookie.new(*elem)
                   else
                     Cookie.new(elem)
          end

          @cookies << cookie
        end if cookies
      end

      def parse(set_cookie)
        SetCookieParser.call(set_cookie) do |name, value, attrs|
          add(Cookie.new(name, value, attrs))
        end
      end

      def add(cookie, path = nil)
        c = cookie.dup

        c.path = path if path && c.path == "/"

        @cookies << c
      end

      def [](uri)
        each(uri).sort
      end

      def each(uri = nil, &blk)
        return enum_for(__method__, uri) unless block_given?

        return @store.each(&blk) unless uri

        uri = URI(uri)

        now = Time.now
        tpath = uri.path

        @cookies.delete_if do |cookie|
          if cookie.expired?(now)
            true
          else
            yield cookie if cookie.valid_for_uri?(uri) && Cookie.path_match?(cookie.path, tpath)
            false
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
httpx-0.15.1 lib/httpx/plugins/cookies/jar.rb
httpx-0.15.0 lib/httpx/plugins/cookies/jar.rb
httpx-0.14.5 lib/httpx/plugins/cookies/jar.rb
httpx-0.14.4 lib/httpx/plugins/cookies/jar.rb
httpx-0.14.3 lib/httpx/plugins/cookies/jar.rb
httpx-0.14.2 lib/httpx/plugins/cookies/jar.rb
httpx-0.14.1 lib/httpx/plugins/cookies/jar.rb
httpx-0.14.0 lib/httpx/plugins/cookies/jar.rb
httpx-0.13.2 lib/httpx/plugins/cookies/jar.rb
httpx-0.13.1 lib/httpx/plugins/cookies/jar.rb
httpx-0.13.0 lib/httpx/plugins/cookies/jar.rb
httpx-0.12.0 lib/httpx/plugins/cookies/jar.rb
httpx-0.11.3 lib/httpx/plugins/cookies/jar.rb
httpx-0.11.2 lib/httpx/plugins/cookies/jar.rb
httpx-0.11.1 lib/httpx/plugins/cookies/jar.rb
httpx-0.11.0 lib/httpx/plugins/cookies/jar.rb
httpx-0.10.2 lib/httpx/plugins/cookies/jar.rb
httpx-0.10.1 lib/httpx/plugins/cookies/jar.rb
httpx-0.10.0 lib/httpx/plugins/cookies/jar.rb