Module: Kube::Schema::SchemaCache
- Defined in:
- lib/kube/schema/schema_cache.rb
Overview
Lazily downloads individual schema JSON files from the schemas branch on GitHub and caches them locally under Gem.cache_home (XDG_CACHE_HOME).
Usage:
Kube::Schema::SchemaCache.fetch("cert-manager.io/certificate_v1")
# => "/home/user/.cache/kube_schema/schemas/cert-manager.io/certificate_v1.json"
Kube::Schema::SchemaCache.read("v1.33.6/deployment")
# => "{...json contents...}"
Constant Summary collapse
- BASE_URL =
'https://raw.githubusercontent.com/general-intelligence-systems/kube_schema/refs/heads/schemas'
Class Attribute Summary collapse
-
.cache_dir ⇒ Object
Root directory for cached schemas.
Class Method Summary collapse
-
.cached?(file_path) ⇒ Boolean
Returns true if the schema is already cached locally.
-
.clear! ⇒ Object
Removes the entire cache directory.
-
.evict(file_path) ⇒ Object
Removes a single cached schema file.
-
.fetch(file_path) ⇒ Object
Returns the local file path for a schema, downloading it if not cached.
-
.local_path(file_path) ⇒ Object
Returns the local path where a schema would be cached (without downloading).
-
.read(file_path) ⇒ Object
Returns the JSON content as a String, downloading if necessary.
Class Attribute Details
.cache_dir ⇒ Object
Root directory for cached schemas. Defaults to ~/.cache/kube_schema/schemas (or $XDG_CACHE_HOME/kube_schema/schemas).
25 26 27 |
# File 'lib/kube/schema/schema_cache.rb', line 25 def cache_dir @cache_dir ||= File.join(Gem.cache_home, 'kube_schema', 'schemas') end |
Class Method Details
.cached?(file_path) ⇒ Boolean
Returns true if the schema is already cached locally.
63 64 65 |
# File 'lib/kube/schema/schema_cache.rb', line 63 def cached?(file_path) File.exist?(local_path(file_path)) end |
.clear! ⇒ Object
Removes the entire cache directory.
77 78 79 |
# File 'lib/kube/schema/schema_cache.rb', line 77 def clear! FileUtils.rm_rf(cache_dir) end |
.evict(file_path) ⇒ Object
Removes a single cached schema file.
68 69 70 71 72 73 74 |
# File 'lib/kube/schema/schema_cache.rb', line 68 def evict(file_path) path = local_path(file_path) if File.exist?(path) File.delete(path) end end |
.fetch(file_path) ⇒ Object
Returns the local file path for a schema, downloading it if not cached. The file_path should NOT include the .json extension.
SchemaCache.fetch("cert-manager.io/certificate_v1")
# => "/home/user/.cache/kube_schema/schemas/cert-manager.io/certificate_v1.json"
38 39 40 41 42 43 44 45 46 |
# File 'lib/kube/schema/schema_cache.rb', line 38 def fetch(file_path) local = local_path(file_path) if File.exist?(local) local else download!(file_path, local) end end |
.local_path(file_path) ⇒ Object
Returns the local path where a schema would be cached (without downloading).
54 55 56 57 58 59 60 |
# File 'lib/kube/schema/schema_cache.rb', line 54 def local_path(file_path) if file_path.end_with?(".json") raise "What are you doing???? don't put .json on the end...." else File.join(cache_dir, "#{file_path}.json") end end |
.read(file_path) ⇒ Object
Returns the JSON content as a String, downloading if necessary.
49 50 51 |
# File 'lib/kube/schema/schema_cache.rb', line 49 def read(file_path) File.read(fetch(file_path)) end |