Class: Lutaml::Hal::Cache::CacheEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/hal/cache/cache_entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, cached_at: nil, metadata: nil, hal_resource: nil) ⇒ CacheEntry

Returns a new instance of CacheEntry.



12
13
14
15
16
17
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 12

def initialize(url: nil, cached_at: nil, metadata: nil, hal_resource: nil)
  @url = url
  @cached_at = cached_at
  @metadata = 
  @hal_resource = hal_resource
end

Instance Attribute Details

#cached_atObject

Returns the value of attribute cached_at.



10
11
12
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 10

def cached_at
  @cached_at
end

#hal_resourceObject

Returns the value of attribute hal_resource.



10
11
12
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 10

def hal_resource
  @hal_resource
end

#metadataObject

Returns the value of attribute metadata.



10
11
12
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 10

def 
  @metadata
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 10

def url
  @url
end

Class Method Details

.create(url, response, hal_resource) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 56

def self.create(url, response, hal_resource)
  new(
    url: url,
    cached_at: Time.now.to_s,
    metadata: CacheMetadata.from_response(response),
    hal_resource: hal_resource
  )
end

.from_storage_h(hash) ⇒ Object



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

def self.from_storage_h(hash)
  h = hash.transform_keys(&:to_s)
  new(
    url: h['url'],
    cached_at: h['cached_at'],
    metadata: h['metadata'] ? CacheMetadata.from_json(h['metadata']) : nil,
    hal_resource: rebuild_model(h['model_class'], h['model'])
  )
end

.rebuild_model(class_name, model_json) ⇒ Object



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

def self.rebuild_model(class_name, model_json)
  return nil unless class_name && model_json

  Object.const_get(class_name).from_json(model_json)
rescue NameError
  nil
end

.storage_format?(hash) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.storage_format?(hash)
  hash.key?('model') || hash.key?(:model) ||
    hash.key?('model_class') || hash.key?(:model_class)
end

Instance Method Details

#ageObject



98
99
100
101
102
103
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 98

def age
  return 0 unless cached_at

  cached_time = cached_at.is_a?(String) ? Time.parse(cached_at) : cached_at
  Time.now - cached_time
end

#cacheable?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 89

def cacheable?
  &.cacheable? != false
end

#conditional_headersObject



85
86
87
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 85

def conditional_headers
  &.conditional_headers || {}
end

#expired?(default_ttl) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 75

def expired?(default_ttl)
  !valid?(default_ttl)
end

#refresh_metadata(response) ⇒ Object



93
94
95
96
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 93

def (response)
  self.cached_at = Time.now.to_s
  self. = CacheMetadata.from_response(response)
end

#revalidatable?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 79

def revalidatable?
  return false unless 

  !!(.etag || .last_modified)
end

#serve_stale?(max_stale = nil) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 105

def serve_stale?(max_stale = nil)
  return false unless max_stale
  return false if valid?(Float::INFINITY)

  cached_time = cached_at.is_a?(String) ? Time.parse(cached_at) : cached_at
  current_age = Time.now - cached_time
  ttl = &.max_age || 0

  staleness = current_age - ttl
  current_age > ttl && staleness < max_stale
end

#to_json(*_args) ⇒ Object



29
30
31
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 29

def to_json(*_args)
  JSON.generate(to_storage_h)
end

#to_storage_hObject



19
20
21
22
23
24
25
26
27
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 19

def to_storage_h
  {
    'url' => url,
    'cached_at' => cached_at,
    'metadata' => &.to_json,
    'model_class' => hal_resource&.class&.name,
    'model' => hal_resource&.to_json
  }
end

#valid?(default_ttl) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 65

def valid?(default_ttl)
  return false unless cached_at

  cached_time = cached_at.is_a?(String) ? Time.parse(cached_at) : cached_at
  age = Time.now - cached_time
  ttl = &.max_age || default_ttl

  age < ttl
end