Class: Rack::CacheBuster::CacheControlHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/cache_buster/cache_control_header.rb

Constant Summary collapse

CacheControl =
"Cache-Control".freeze
Age =
"Age".freeze
MaxAge =
"max-age".freeze

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ CacheControlHeader

Returns a new instance of CacheControlHeader.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rack/cache_buster/cache_control_header.rb', line 8

def initialize(env)
  @age = env[Age].to_i
  @max_age = 0
  if env[CacheControl]
    parts = env[CacheControl].split(/ *, */)
    settings, options = parts.partition{|part| part =~ /=/ }
    settings = settings.inject({}){|acc, part|
      k, v = part.split(/ *= */, 2)
      acc.merge(k => v)
    }
    @max_age = settings.delete(MaxAge).to_i
    @other_parts = options + settings.map{|k,v| "#{k}=#{v}"}
  end
end

Instance Method Details

#expire_timeObject



31
32
33
# File 'lib/rack/cache_buster/cache_control_header.rb', line 31

def expire_time
  Time.now + expires_in
end

#expire_time=(t) ⇒ Object



35
36
37
# File 'lib/rack/cache_buster/cache_control_header.rb', line 35

def expire_time=(t)
  @max_age = [t.to_i - Time.now.to_i + @age, @age].max
end

#expired?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rack/cache_buster/cache_control_header.rb', line 27

def expired?
  expires_in <= 0
end

#expires_inObject



23
24
25
# File 'lib/rack/cache_buster/cache_control_header.rb', line 23

def expires_in
  @max_age - @age
end

#to_sObject



43
44
45
# File 'lib/rack/cache_buster/cache_control_header.rb', line 43

def to_s
  ["max-age=#{@max_age}", *@other_parts].join(", ")
end

#update_env(env) ⇒ Object



39
40
41
# File 'lib/rack/cache_buster/cache_control_header.rb', line 39

def update_env(env)
  env[CacheControl] = to_s
end