Sha256: 828501f0dea515715ee3c569f1a074e666d4ff61aeb012631f1f6cd6abcc3acd

Contents?: true

Size: 877 Bytes

Versions: 10

Compression:

Stored size: 877 Bytes

Contents

# frozen_string_literal: true

module Grape
  class Cookies
    def initialize
      @cookies = {}
      @send_cookies = {}
    end

    def read(request)
      request.cookies.each do |name, value|
        @cookies[name.to_s] = value
      end
    end

    def write(header)
      @cookies.select { |key, _value| @send_cookies[key] == true }.each do |name, value|
        cookie_value = value.is_a?(Hash) ? value : { value: value }
        Rack::Utils.set_cookie_header! header, name, cookie_value
      end
    end

    def [](name)
      @cookies[name.to_s]
    end

    def []=(name, value)
      @cookies[name.to_s] = value
      @send_cookies[name.to_s] = true
    end

    def each(&block)
      @cookies.each(&block)
    end

    def delete(name, **opts)
      options = opts.merge(value: 'deleted', expires: Time.at(0))
      self.[]=(name, options)
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
grape-1.5.3 lib/grape/cookies.rb
grape-1.5.2 lib/grape/cookies.rb
grape-1.5.1 lib/grape/cookies.rb
grape-1.5.0 lib/grape/cookies.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/grape-1.4.0/lib/grape/cookies.rb
grape-1.4.0 lib/grape/cookies.rb
grape-1.3.3 lib/grape/cookies.rb
grape-1.3.2 lib/grape/cookies.rb
grape-1.3.1 lib/grape/cookies.rb
grape-1.3.0 lib/grape/cookies.rb