class Sinatra::Cookies::Jar
Attributes
Public Class Methods
Source
# File lib/sinatra/cookies.rb, line 62 def initialize(app) @response_array = nil @response_hash = {} @response = app.response @request = app.request @deleted = [] @options = { path: @request.script_name.to_s.empty? ? '/' : @request.script_name, domain: @request.host == 'localhost' ? nil : @request.host, secure: @request.secure?, httponly: true } return unless app.settings.respond_to? :cookie_options @options.merge! app.settings.cookie_options end
Public Instance Methods
Source
# File lib/sinatra/cookies.rb, line 81 def ==(other) other.respond_to? :to_hash and to_hash == other.to_hash end
Source
# File lib/sinatra/cookies.rb, line 85 def [](key) response_cookies[key.to_s] || request_cookies[key.to_s] end
Source
# File lib/sinatra/cookies.rb, line 89 def []=(key, value) set(key, value: value) end
Also aliased as: store
Source
# File lib/sinatra/cookies.rb, line 94 def assoc(key) to_hash.assoc(key.to_s) end
Source
# File lib/sinatra/cookies.rb, line 103 def compare_by_identity? false end
Source
# File lib/sinatra/cookies.rb, line 113 def delete(key) result = self[key] @response.delete_cookie(key.to_s, @options) result end
Source
# File lib/sinatra/cookies.rb, line 119 def delete_if return enum_for(__method__) unless block_given? each { |k, v| delete(k) if yield(k, v) } self end
Also aliased as: reject!
Source
# File lib/sinatra/cookies.rb, line 126 def each(&block) return enum_for(__method__) unless block_given? to_hash.each(&block) end
Also aliased as: each_pair
Source
# File lib/sinatra/cookies.rb, line 132 def each_key(&block) return enum_for(__method__) unless block_given? to_hash.each_key(&block) end
Source
# File lib/sinatra/cookies.rb, line 140 def each_value(&block) return enum_for(__method__) unless block_given? to_hash.each_value(&block) end
Source
# File lib/sinatra/cookies.rb, line 150 def fetch(key, &block) response_cookies.fetch(key.to_s) do request_cookies.fetch(key.to_s, &block) end end
Source
# File lib/sinatra/cookies.rb, line 162 def has_key?(key) response_cookies.key? key.to_s or request_cookies.key? key.to_s end
Source
# File lib/sinatra/cookies.rb, line 166 def has_value?(value) response_cookies.value? value or request_cookies.value? value end
Also aliased as: value?
Source
# File lib/sinatra/cookies.rb, line 177 def inspect "<##{self.class}: #{to_hash.inspect[1..-2]}>" end
Source
# File lib/sinatra/cookies.rb, line 187 def keep_if return enum_for(__method__) unless block_given? delete_if { |*a| !yield(*a) } end
Also aliased as: select!
Source
# File lib/sinatra/cookies.rb, line 207 def merge(other, &block) to_hash.merge(other, &block) end
Source
# File lib/sinatra/cookies.rb, line 211 def merge!(other) other.each_pair do |key, value| self[key] = if block_given? && include?(key) yield(key.to_s, self[key], value) else value end end end
Also aliased as: update
Source
# File lib/sinatra/cookies.rb, line 221 def rassoc(value) to_hash.rassoc(value) end
Source
# File lib/sinatra/cookies.rb, line 225 def rehash response_cookies.rehash request_cookies.rehash self end
Source
# File lib/sinatra/cookies.rb, line 231 def reject(&block) return enum_for(__method__) unless block_given? to_hash.reject(&block) end
Source
# File lib/sinatra/cookies.rb, line 239 def replace(other) select! { |k, _v| other.include?(k) or other.include?(k.to_s) } merge! other end
Source
# File lib/sinatra/cookies.rb, line 244 def select(&block) return enum_for(__method__) unless block_given? to_hash.select(&block) end
Source
# File lib/sinatra/cookies.rb, line 252 def set(key, options = {}) @response.set_cookie key.to_s, @options.merge(options) end
Source
# File lib/sinatra/cookies.rb, line 256 def shift key, value = to_hash.shift delete(key) [key, value] end
Source
# File lib/sinatra/cookies.rb, line 265 def sort(&block) to_hash.sort(&block) end
Source
# File lib/sinatra/cookies.rb, line 272 def to_hash request_cookies.merge(response_cookies) end
Source
# File lib/sinatra/cookies.rb, line 291 def values_at(*list) list.map { |k| self[k] } end
Private Instance Methods
Source
# File lib/sinatra/cookies.rb, line 311 def parse_response cookies_from_response = Array(@response['Set-Cookie']) return if @response_array == cookies_from_response hash = {} cookies_from_response.each do |line| key, value = line.split(';', 2).first.to_s.split('=', 2) next if key.nil? key = Rack::Utils.unescape(key) if line =~ /expires=Thu, 01[-\s]Jan[-\s]1970/ @deleted << key else @deleted.delete key hash[key] = value end end @response_hash.replace hash @response_array = cookies_from_response end
Source
# File lib/sinatra/cookies.rb, line 297 def warn(message) super "#{caller.first[/^[^:]:\d+:/]} warning: #{message}" end
Calls superclass method