Meta Tags Generator — Ruby source
Build the SEO head tags for any page — title, description, canonical, robots, Open Graph with smart fallbacks, Twitter card and theme-color — with live length warnings.
This is the Ruby implementation — the same logic the interactive tool runs, in a shareable, citable form.
# meta-tags-generator — build the ordered SEO head-tag set: title,
# description, canonical, robots, Open Graph (og_title/og_description/og_url
# falling back to the base fields, og:type website whenever the block is
# present), Twitter card, theme-color. Ruby port (canonical TS:
# src/lib/meta-tags.ts; Go twin:
# cli/meta-tags-generator/meta-tags-generator.go). Same fields, same
# fallbacks, same emission order, same warnings (title > 60,
# description > 160) and the same hard errors (missing title/description,
# non-absolute og:image, non-hex theme-color suppress emission entirely).
module MetaTagsGenerator
module_function
HEX = /\A#[0-9a-fA-F]{6}\z/.freeze
HTTP = %r{\Ahttps?://}.freeze
# Build the ordered head-tag set. Takes a hash of the input fields
# (symbols, strings or nil — blanks are trimmed away); returns the result
# hash { ok:, errors:, warnings:, tags: }. Hard errors suppress emission
# entirely; length warnings never block emission.
def build_meta_tags(inp)
errors = []
warnings = []
trim = ->(s) { s.to_s.strip }
title = trim.call(inp[:title])
description = trim.call(inp[:description])
canonical = trim.call(inp[:canonical_url])
og_title = trim.call(inp[:og_title]).empty? ? title : trim.call(inp[:og_title])
og_description = trim.call(inp[:og_description]).empty? ? description : trim.call(inp[:og_description])
og_image = trim.call(inp[:og_image])
og_url = trim.call(inp[:og_url]).empty? ? canonical : trim.call(inp[:og_url])
theme_color = trim.call(inp[:theme_color])
errors << 'Title is required.' if title.empty?
errors << 'Description is required.' if description.empty?
errors << 'og:image must be an absolute http(s) URL.' if !og_image.empty? && og_image !~ HTTP
errors << 'theme-color must be a #rrggbb hex value.' if !theme_color.empty? && theme_color !~ HEX
return { ok: false, errors: errors, warnings: [], tags: [] } unless errors.empty?
warnings << "Title is #{title.length} characters — search results truncate around 60." if title.length > 60
if description.length > 160
warnings << "Description is #{description.length} characters — search results truncate around 160."
end
tags = ["<title>#{title}</title>", "<meta name=\"description\" content=\"#{description}\" />"]
tags << "<link rel=\"canonical\" href=\"#{canonical}\" />" unless canonical.empty?
tags << "<meta name=\"robots\" content=\"#{trim.call(inp[:robots])}\" />" unless trim.call(inp[:robots]).empty?
has_og = !trim.call(inp[:og_title]).empty? || !trim.call(inp[:og_description]).empty? ||
!og_image.empty? || !og_url.empty?
if has_og
tags << "<meta property=\"og:title\" content=\"#{og_title}\" />"
tags << "<meta property=\"og:description\" content=\"#{og_description}\" />"
tags << "<meta property=\"og:image\" content=\"#{og_image}\" />" unless og_image.empty?
tags << "<meta property=\"og:url\" content=\"#{og_url}\" />" unless og_url.empty?
tags << '<meta property="og:type" content="website" />'
end
tags << "<meta name=\"twitter:card\" content=\"#{inp[:twitter_card]}\" />" unless inp[:twitter_card].to_s.empty?
unless trim.call(inp[:twitter_site]).empty?
tags << "<meta name=\"twitter:site\" content=\"#{trim.call(inp[:twitter_site])}\" />"
end
tags << "<meta name=\"theme-color\" content=\"#{theme_color}\" />" unless theme_color.empty?
{ ok: true, errors: [], warnings: warnings, tags: tags }
end
end
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →