serp-snippet-length-check 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: da67fa5a8f8963949acc4e43661675dfa97cff146b90242b7a007e393041a9da
4
+ data.tar.gz: 9b6adf90106d7128c5d6f899d7cc43f6da09fa2925fb0b695aa4c1c2c694e479
5
+ SHA512:
6
+ metadata.gz: a6edb095c17468720da611e65ac8ba2471d467260cd513785c43a6b29e82a5907219f13861de5be5677d2007ac87749b71f3bdafc8d083303136ddb656efc37d
7
+ data.tar.gz: 848bb27f89b6d9003e8215dcf98b1934745b6bfc5a5421d70d4b043bb645fe60e928aa52bc28654722704388817bec1946ba84d05c8b77e5c72c78dde7a88252
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 OneGTMLab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # serp-snippet-length-check
2
+
3
+ Ruby command line tool and library. Give it a CSV of pages with a title and a meta description and it counts the characters of each (after trimming), classifies every title and description as missing, too short, ok or too long against limits you set, lists the pages that need a fix, and lists titles and descriptions that appear on more than one page.
4
+
5
+ Part of the [OneGTMLab](https://www.onegtmlab.dev) free GTM tools. Background on the idea: [search engine optimization](https://www.onegtmlab.dev/glossary/search-engine-optimization).
6
+
7
+ ## Install
8
+
9
+ ```
10
+ gem install serp-snippet-length-check
11
+ ```
12
+
13
+ ## Use
14
+
15
+ CSV columns: url, title, description. Lengths are counted in characters after trimming spaces at both ends. A text is missing when it is empty after trimming, too short when it has fewer characters than the minimum, too long when it has more than the maximum, and ok otherwise. The four limits are parameters set by you: --title-min (default 30), --title-max (default 60), --description-min (default 70) and --description-max (default 155). The defaults are only starting values; they are not facts about any search engine, so set the limits you want to hold your pages to. A minimum may not be above its maximum. Duplicates are texts that are the same after trimming and ignoring upper and lower case; empty texts are never counted as duplicates. A missing file, a missing column, a bad option or a CSV with no rows stops the run with an error and a non-zero exit code.
16
+
17
+ ```
18
+ $ ruby bin/serp-snippet-length-check sample.csv --title-max 60 --description-max 155
19
+ pages: 7
20
+
21
+ field missing too short ok too long
22
+ title 1 1 4 1
23
+ description 1 1 4 1
24
+
25
+ pages needing a fix: 4
26
+ url field length state
27
+ https://example.com/about title 8 too short
28
+ https://example.com/pricing title 78 too long
29
+ https://example.com/pricing description 210 too long
30
+ https://example.com/blog title 0 missing
31
+ https://example.com/blog description 11 too short
32
+ https://example.com/contact description 0 missing
33
+
34
+ duplicate titles: 1
35
+ "Example Home Page | Alpha Tools Overview": https://example.com/, https://example.com/contact
36
+
37
+ duplicate descriptions: 1
38
+ "Alpha Tools helps teams plan, track and review their work in one place, with simple reports for every week.": https://example.com/, https://example.org/tool-a
39
+ ```
40
+
41
+ ## Test
42
+
43
+ ```
44
+ ruby test/run.rb
45
+ ```
46
+
47
+ Offline, no network calls, no dependencies beyond the language runtime. MIT licensed.
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/serp_snippet_length_check'
5
+
6
+ if ARGV.empty? || ARGV.include?('-h') || ARGV.include?('--help')
7
+ warn "usage: serp-snippet-length-check pages.csv [--title-min 30] [--title-max 60] [--description-min 70] [--description-max 155]\n" \
8
+ "CSV columns: url, title, description. More: https://www.onegtmlab.dev/glossary/search-engine-optimization"
9
+ exit 2
10
+ end
11
+
12
+ begin
13
+ path, limits = SerpSnippetLengthCheck.parse_args(ARGV)
14
+ pages = SerpSnippetLengthCheck.from_csv(File.read(path))
15
+ print SerpSnippetLengthCheck.render(SerpSnippetLengthCheck.analyze(pages, limits))
16
+ rescue ArgumentError, Errno::ENOENT, Errno::EISDIR, CSV::MalformedCSVError => e
17
+ abort "error: #{e.message}"
18
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+
5
+ # Check the length of page titles and meta descriptions in a CSV, against limits you set.
6
+ module SerpSnippetLengthCheck
7
+ STATES = ['missing', 'too short', 'ok', 'too long'].freeze
8
+ FIELDS = %w[title description].freeze
9
+ DEFAULTS = { 'title-min' => 30, 'title-max' => 60, 'description-min' => 70, 'description-max' => 155 }.freeze
10
+
11
+ Page = Struct.new(:url, :title, :description, keyword_init: true)
12
+
13
+ # state of one text: length is counted in characters after trimming.
14
+ def self.state(text, min, max)
15
+ len = text.to_s.strip.length
16
+ return 'missing' if len.zero?
17
+ return 'too short' if len < min
18
+ return 'too long' if len > max
19
+
20
+ 'ok'
21
+ end
22
+
23
+ def self.length(text)
24
+ text.to_s.strip.length
25
+ end
26
+
27
+ def self.from_csv(text)
28
+ table = CSV.parse(text, headers: true)
29
+ missing = %w[url title description] - table.headers.compact.map(&:strip)
30
+ raise ArgumentError, "missing column(s): #{missing.join(', ')}" unless missing.empty?
31
+ raise ArgumentError, 'no rows in the CSV' if table.empty?
32
+
33
+ table.map do |r|
34
+ Page.new(url: r['url'].to_s.strip, title: r['title'].to_s.strip, description: r['description'].to_s.strip)
35
+ end
36
+ end
37
+
38
+ # limits: hash with the four keys of DEFAULTS.
39
+ def self.analyze(pages, limits = DEFAULTS)
40
+ counts = FIELDS.to_h { |f| [f, STATES.to_h { |s| [s, 0] }] }
41
+ fixes = []
42
+ pages.each do |p|
43
+ FIELDS.each do |f|
44
+ st = state(p[f], limits["#{f}-min"], limits["#{f}-max"])
45
+ counts[f][st] += 1
46
+ fixes << [p.url, f, length(p[f]), st] unless st == 'ok'
47
+ end
48
+ end
49
+ { pages: pages.size, counts: counts, fixes: fixes,
50
+ duplicates: FIELDS.to_h { |f| [f, duplicates(pages, f)] } }
51
+ end
52
+
53
+ # Groups of pages sharing the same non-empty text (case-insensitive), in order of first appearance.
54
+ def self.duplicates(pages, field)
55
+ groups = {}
56
+ pages.each do |p|
57
+ text = p[field].to_s.strip
58
+ next if text.empty?
59
+
60
+ (groups[text.downcase] ||= { text: text, urls: [] })[:urls] << p.url
61
+ end
62
+ groups.values.select { |g| g[:urls].size > 1 }
63
+ end
64
+
65
+ def self.render(result)
66
+ out = +"pages: #{result[:pages]}\n\n"
67
+ out << format("%-12s %7s %9s %4s %9s\n", 'field', 'missing', 'too short', 'ok', 'too long')
68
+ FIELDS.each do |f|
69
+ c = result[:counts][f]
70
+ out << format("%-12s %7d %9d %4d %9d\n", f, c['missing'], c['too short'], c['ok'], c['too long'])
71
+ end
72
+ fix_pages = result[:fixes].map(&:first).uniq.size
73
+ out << "\npages needing a fix: #{fix_pages}\n"
74
+ unless result[:fixes].empty?
75
+ out << format("%-30s %-12s %6s %s\n", 'url', 'field', 'length', 'state')
76
+ result[:fixes].each { |u, f, l, s| out << format("%-30s %-12s %6d %s\n", u, f, l, s) }
77
+ end
78
+ FIELDS.each do |f|
79
+ dups = result[:duplicates][f]
80
+ out << "\nduplicate #{f}s: #{dups.empty? ? 'none' : dups.size}\n"
81
+ dups.each { |g| out << " \"#{g[:text]}\": #{g[:urls].join(', ')}\n" }
82
+ end
83
+ out
84
+ end
85
+
86
+ # Parse the command line. Returns [path, limits]; raises ArgumentError on bad input.
87
+ def self.parse_args(argv)
88
+ args = argv.dup
89
+ path = nil
90
+ limits = DEFAULTS.dup
91
+ until args.empty?
92
+ arg = args.shift
93
+ case arg
94
+ when /\A--(title|description)-(min|max)\z/
95
+ key = arg.sub('--', '')
96
+ raise ArgumentError, "#{arg} needs a value" if args.empty?
97
+
98
+ val = args.shift
99
+ raise ArgumentError, "bad #{arg} #{val.inspect}, expected a whole number" unless val.match?(/\A\d+\z/)
100
+
101
+ n = val.to_i
102
+ raise ArgumentError, "bad #{arg} #{val.inspect}, a maximum must be at least 1" if n.zero? && key.end_with?('max')
103
+
104
+ limits[key] = n
105
+ when /\A-/ then raise ArgumentError, "unknown option #{arg}"
106
+ else
107
+ raise ArgumentError, "unexpected extra argument #{arg.inspect}" if path
108
+
109
+ path = arg
110
+ end
111
+ end
112
+ raise ArgumentError, 'no CSV file given' unless path
113
+
114
+ FIELDS.each do |f|
115
+ if limits["#{f}-min"] > limits["#{f}-max"]
116
+ raise ArgumentError, "--#{f}-min #{limits["#{f}-min"]} is above --#{f}-max #{limits["#{f}-max"]}"
117
+ end
118
+ end
119
+ [path, limits]
120
+ end
121
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serp-snippet-length-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - OneGTMLab
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: csv
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: "0"
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: "0"
28
+ description: "Ruby CLI and library: from a CSV of pages (url, title, description) count characters after trimming, classify each title and description as missing, too short, ok or too long against limits you set, and list pages to fix and duplicate texts. Offline."
29
+ executables:
30
+ - serp-snippet-length-check
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - bin/serp-snippet-length-check
37
+ - lib/serp_snippet_length_check.rb
38
+ homepage: "https://www.onegtmlab.dev/glossary/search-engine-optimization"
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ source_code_uri: "https://github.com/jaizendra-netizen/serp-snippet-length-check"
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ -
49
+ - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "2.7"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ -
55
+ - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ requirements: []
59
+ rubygems_version: 4.1.0
60
+ specification_version: 4
61
+ summary: Check page title and meta description lengths in a CSV against limits you set
62
+ test_files: []