Module: RailsAiBridge::FreshnessHeader
- Defined in:
- lib/rails_ai_bridge/freshness_header.rb
Overview
Utility module to embed and extract freshness metadata from generated bridge files.
Constant Summary collapse
- HEADER_PATTERN =
/\A<!-- Generated at: ([^|]+) \| Source fingerprint: ([a-f0-9]{12})(?: \| rails-ai-bridge: v([^ ]+))? -->/i
Class Method Summary collapse
-
.embed(content, timestamp, fingerprint) ⇒ String
Embeds the freshness header at the top of the content (Markdown formats).
-
.embed_for(fmt, content, timestamp, fingerprint) ⇒ String
Embeds the freshness header appropriate for the given format.
-
.extract_fingerprint(content) ⇒ String?
Extracts the fingerprint from the markdown freshness header.
-
.extract_fingerprint_for(fmt, content) ⇒ String?
Extracts the fingerprint from content, dispatching on format.
-
.extract_metadata_for(fmt, content) ⇒ Array(String, String), Array(nil, nil)
Extracts [fingerprint, timestamp] tuple from content, dispatching on format.
-
.extract_timestamp(content) ⇒ String?
Extracts the timestamp from the markdown freshness header.
-
.extract_version(content) ⇒ String?
Extracts the gem version from the markdown freshness header.
Class Method Details
.embed(content, timestamp, fingerprint) ⇒ String
Embeds the freshness header at the top of the content (Markdown formats).
55 56 57 58 |
# File 'lib/rails_ai_bridge/freshness_header.rb', line 55 def (content, , fingerprint) header = "<!-- Generated at: #{} | Source fingerprint: #{fingerprint} | rails-ai-bridge: v#{RailsAiBridge::VERSION} -->\n" header + content end |
.embed_for(fmt, content, timestamp, fingerprint) ⇒ String
Embeds the freshness header appropriate for the given format. For JSON, merges a _meta key; for Markdown, prepends an HTML comment.
19 20 21 22 23 |
# File 'lib/rails_ai_bridge/freshness_header.rb', line 19 def (fmt, content, , fingerprint) return (content, , fingerprint) if fmt == :json (content, , fingerprint) end |
.extract_fingerprint(content) ⇒ String?
Extracts the fingerprint from the markdown freshness header.
64 65 66 67 |
# File 'lib/rails_ai_bridge/freshness_header.rb', line 64 def extract_fingerprint(content) match = content.match(HEADER_PATTERN) match ? match[2] : nil end |
.extract_fingerprint_for(fmt, content) ⇒ String?
Extracts the fingerprint from content, dispatching on format. Handles both JSON (_meta key) and Markdown (HTML comment header) formats.
43 44 45 46 47 |
# File 'lib/rails_ai_bridge/freshness_header.rb', line 43 def extract_fingerprint_for(fmt, content) return extract_json_fingerprint(content) if fmt == :json extract_fingerprint(content) end |
.extract_metadata_for(fmt, content) ⇒ Array(String, String), Array(nil, nil)
Extracts [fingerprint, timestamp] tuple from content, dispatching on format.
30 31 32 33 34 35 |
# File 'lib/rails_ai_bridge/freshness_header.rb', line 30 def (fmt, content) return [nil, nil] unless content return (content) if fmt == :json [extract_fingerprint(content), (content)] end |
.extract_timestamp(content) ⇒ String?
Extracts the timestamp from the markdown freshness header.
73 74 75 76 |
# File 'lib/rails_ai_bridge/freshness_header.rb', line 73 def (content) match = content.match(HEADER_PATTERN) match ? match[1] : nil end |
.extract_version(content) ⇒ String?
Extracts the gem version from the markdown freshness header.
82 83 84 85 |
# File 'lib/rails_ai_bridge/freshness_header.rb', line 82 def extract_version(content) match = content.match(HEADER_PATTERN) match ? match[3] : nil end |