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.



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

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.



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

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)


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

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

#no_cache?Boolean

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

Returns:

  • (Boolean)


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

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

#no_store?Boolean

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

Returns:

  • (Boolean)


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

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



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

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)


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

def private?
  @directives['private']
end

#proxy_revalidate?Boolean

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

Returns:

  • (Boolean)


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

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

#public?Boolean

Internal: Checks if the ‘public’ directive is present.

Returns:

  • (Boolean)


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

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.



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

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.



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

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