Class: Lutaml::Hal::Cache::CacheMetadata

Inherits:
Model::Serializable
  • Object
show all
Defined in:
lib/lutaml/hal/cache/cache_metadata.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_response(response) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lutaml/hal/cache/cache_metadata.rb', line 18

def self.from_response(response)
  headers = ResponseAdapter.headers(response)

  new(
    etag: headers['etag'],
    last_modified: headers['last-modified'],
    cache_control: headers['cache-control'],
    expires: headers['expires'],
    status_code: ResponseAdapter.status_code(response),
    content_type: headers['content-type'],
    date: headers['date'],
    vary: headers['vary']
  )
end

Instance Method Details

#cacheable?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/lutaml/hal/cache/cache_metadata.rb', line 40

def cacheable?
  return false if cache_control&.include?('no-cache')
  return false if cache_control&.include?('no-store')
  return false if cache_control&.include?('private')

  true
end

#conditional_headersObject



33
34
35
36
37
38
# File 'lib/lutaml/hal/cache/cache_metadata.rb', line 33

def conditional_headers
  headers = {}
  headers['If-None-Match'] = etag if etag
  headers['If-Modified-Since'] = last_modified if last_modified
  headers
end

#max_ageObject



48
49
50
51
52
53
# File 'lib/lutaml/hal/cache/cache_metadata.rb', line 48

def max_age
  return nil unless cache_control

  match = cache_control.match(/max-age=(\d+)/)
  match ? match[1].to_i : nil
end

#revalidatable?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/lutaml/hal/cache/cache_metadata.rb', line 55

def revalidatable?
  !!(etag && !etag.empty?) || !!(last_modified && !last_modified.empty?)
end