Sha256: ec6f7fec329e75fd2b309dcc6818ed7bdc2ea960378251b75a6f122cece571c8

Contents?: true

Size: 974 Bytes

Versions: 7

Compression:

Stored size: 974 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

    # rubocop:disable Layout/SpaceBeforeBrackets
    def delete(name, **opts)
      options = opts.merge(value: 'deleted', expires: Time.at(0))
      self.[]=(name, options)
    end
    # rubocop:enable Layout/SpaceBeforeBrackets
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
grape-2.0.0 lib/grape/cookies.rb
grape-1.8.0 lib/grape/cookies.rb
grape-1.7.1 lib/grape/cookies.rb
grape-1.7.0 lib/grape/cookies.rb
grape-1.6.2 lib/grape/cookies.rb
grape-1.6.1 lib/grape/cookies.rb
grape-1.6.0 lib/grape/cookies.rb