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

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

Overview

Represents HTTP response metadata from the request that created the HAL resource

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_headers(response) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lutaml/hal/cache/cache_metadata.rb', line 65

def self.extract_headers(response)
  case response
  when Hash
    # Response is already a hash, extract headers directly
    response.select { |k, _| k.is_a?(String) && k.match?(/^[a-z-]+$/) }
  when ->(r) { r.respond_to?(:headers) }
    # Response has headers method
    response.headers.to_h
  when ->(r) { r.respond_to?(:[]) }
    # Response is hash-like, try to extract common headers
    {
      'etag' => response['etag'],
      'last-modified' => response['last-modified'],
      'cache-control' => response['cache-control'],
      'expires' => response['expires'],
      'content-type' => response['content-type'],
      'date' => response['date'],
      'vary' => response['vary']
    }.compact
  else
    {}
  end
end

.extract_status_code(response) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lutaml/hal/cache/cache_metadata.rb', line 89

def self.extract_status_code(response)
  case response
  when Hash
    response['status'] || response[:status] || 200
  when ->(r) { r.respond_to?(:status) }
    response.status
  when ->(r) { r.respond_to?(:code) }
    response.code.to_i
  else
    200
  end
end

.from_response(response) ⇒ Object

Extract metadata from HTTP response headers



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

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

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

Instance Method Details

#cacheable?Boolean

Check if the metadata indicates the response is cacheable

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/lutaml/hal/cache/cache_metadata.rb', line 44

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

Generate conditional request headers for cache validation



36
37
38
39
40
41
# File 'lib/lutaml/hal/cache/cache_metadata.rb', line 36

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

#max_ageObject

Extract TTL from cache-control header



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

def max_age
  return nil unless cache_control

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

#revalidatable?Boolean

Check if the response can be revalidated with conditional requests

Returns:

  • (Boolean)


61
62
63
# File 'lib/lutaml/hal/cache/cache_metadata.rb', line 61

def revalidatable?
  !etag.nil? && !etag.empty? || !last_modified.nil? && !last_modified.empty?
end