Class: Lutaml::Hal::Cache::CacheEntry
- Inherits:
-
Object
- Object
- Lutaml::Hal::Cache::CacheEntry
- Defined in:
- lib/lutaml/hal/cache/cache_entry.rb
Instance Attribute Summary collapse
-
#cached_at ⇒ Object
Returns the value of attribute cached_at.
-
#hal_resource ⇒ Object
Returns the value of attribute hal_resource.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .create(url, response, hal_resource) ⇒ Object
- .from_storage_h(hash) ⇒ Object
- .rebuild_model(class_name, model_json) ⇒ Object
- .storage_format?(hash) ⇒ Boolean
Instance Method Summary collapse
- #age ⇒ Object
- #cacheable? ⇒ Boolean
- #conditional_headers ⇒ Object
- #expired?(default_ttl) ⇒ Boolean
-
#initialize(url: nil, cached_at: nil, metadata: nil, hal_resource: nil) ⇒ CacheEntry
constructor
A new instance of CacheEntry.
- #refresh_metadata(response) ⇒ Object
- #revalidatable? ⇒ Boolean
- #serve_stale?(max_stale = nil) ⇒ Boolean
- #to_json(*_args) ⇒ Object
- #to_storage_h ⇒ Object
- #valid?(default_ttl) ⇒ Boolean
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_at ⇒ Object
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_resource ⇒ Object
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 |
#metadata ⇒ Object
Returns the value of attribute metadata.
10 11 12 |
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 10 def @metadata end |
#url ⇒ Object
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
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
#age ⇒ Object
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
89 90 91 |
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 89 def cacheable? &.cacheable? != false end |
#conditional_headers ⇒ Object
85 86 87 |
# File 'lib/lutaml/hal/cache/cache_entry.rb', line 85 def conditional_headers &.conditional_headers || {} end |
#expired?(default_ttl) ⇒ 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
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
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_h ⇒ Object
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
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 |