Class: Faraday::HttpCache::CacheControl

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday/http_cache/cache_control.rb

Overview

Internal: A class to represent the 'Cache-Control' header options. This implementation is based on 'rack-cache' internals by Ryan Tomayko. It breaks the several directives into keys/values and stores them into a Hash.

Instance Method Summary collapse

Constructor Details

#initialize(header) ⇒ CacheControl

Internal: Initialize a new CacheControl.


10
11
12
# File 'lib/faraday/http_cache/cache_control.rb', line 10

def initialize(header)
  @directives = parse(header.to_s)
end

Instance Method Details

#max_ageObject

Internal: Gets the 'max-age' directive as an Integer.

Returns nil if the 'max-age' directive isn't present.


37
38
39
# File 'lib/faraday/http_cache/cache_control.rb', line 37

def max_age
  @directives['max-age'].to_i if @directives.key?('max-age')
end

#must_revalidate?Boolean

Internal: Checks if the 'must-revalidate' directive is present.

Returns:

  • (Boolean)

61
62
63
# File 'lib/faraday/http_cache/cache_control.rb', line 61

def must_revalidate?
  @directives['must-revalidate']
end

#no_cache?Boolean

Internal: Checks if the 'no-cache' directive is present.

Returns:

  • (Boolean)

25
26
27
# File 'lib/faraday/http_cache/cache_control.rb', line 25

def no_cache?
  @directives['no-cache']
end

#no_store?Boolean

Internal: Checks if the 'no-store' directive is present.

Returns:

  • (Boolean)

30
31
32
# File 'lib/faraday/http_cache/cache_control.rb', line 30

def no_store?
  @directives['no-store']
end

#normalize_max_ages(age) ⇒ Object

Internal: Gets the 'max-age' directive as an Integer.

takes the age header integer value and reduces the max-age and s-maxage if present to account for having to remove static age header when caching responses


45
46
47
48
49
50
# File 'lib/faraday/http_cache/cache_control.rb', line 45

def normalize_max_ages(age)
  if age > 0
    @directives['max-age'] = @directives['max-age'].to_i - age if @directives.key?('max-age')
    @directives['s-maxage'] = @directives['s-maxage'].to_i - age if @directives.key?('s-maxage')
  end
end

#private?Boolean

Internal: Checks if the 'private' directive is present.

Returns:

  • (Boolean)

20
21
22
# File 'lib/faraday/http_cache/cache_control.rb', line 20

def private?
  @directives['private']
end

#proxy_revalidate?Boolean

Internal: Checks if the 'proxy-revalidate' directive is present.

Returns:

  • (Boolean)

66
67
68
# File 'lib/faraday/http_cache/cache_control.rb', line 66

def proxy_revalidate?
  @directives['proxy-revalidate']
end

#public?Boolean

Internal: Checks if the 'public' directive is present.

Returns:

  • (Boolean)

15
16
17
# File 'lib/faraday/http_cache/cache_control.rb', line 15

def public?
  @directives['public']
end

#shared_max_ageObject Also known as: s_maxage

Internal: Gets the 's-maxage' directive as an Integer.

Returns nil if the 's-maxage' directive isn't present.


55
56
57
# File 'lib/faraday/http_cache/cache_control.rb', line 55

def shared_max_age
  @directives['s-maxage'].to_i if @directives.key?('s-maxage')
end

#to_sObject

Internal: Gets the String representation for the cache directives. Directives are joined by a '=' and then combined into a single String separated by commas. Directives with a 'true' value will omit the '=' sign and their value.

Returns the Cache Control string.


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/faraday/http_cache/cache_control.rb', line 76

def to_s
  booleans = []
  values = []

  @directives.each do |key, value|
    if value == true
      booleans << key
    elsif value
      values << "#{key}=#{value}"
    end
  end

  (booleans.sort + values.sort).join(', ')
end