yuno-proofreader 0.1.2 → 0.1.3

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.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -1
  3. data/lib/yuno/proofreader/analysis/adjustments.rb +30 -32
  4. data/lib/yuno/proofreader/analysis/crawler.rb +71 -73
  5. data/lib/yuno/proofreader/analysis/entries.rb +52 -54
  6. data/lib/yuno/proofreader/analysis/extractor.rb +162 -164
  7. data/lib/yuno/proofreader/analysis/graph.rb +86 -88
  8. data/lib/yuno/proofreader/analysis/judge.rb +80 -82
  9. data/lib/yuno/proofreader/analysis/judge_prompt.rb +50 -52
  10. data/lib/yuno/proofreader/analysis/judgments.rb +50 -52
  11. data/lib/yuno/proofreader/analysis/links.rb +44 -46
  12. data/lib/yuno/proofreader/analysis/list_order.rb +20 -22
  13. data/lib/yuno/proofreader/analysis/reasons.rb +44 -43
  14. data/lib/yuno/proofreader/analysis/report.rb +79 -81
  15. data/lib/yuno/proofreader/analysis/runner.rb +133 -135
  16. data/lib/yuno/proofreader/analysis/scorer.rb +97 -99
  17. data/lib/yuno/proofreader/analysis/settings.rb +65 -67
  18. data/lib/yuno/proofreader/analysis/signals.rb +77 -79
  19. data/lib/yuno/proofreader/analysis/structured_data.rb +40 -42
  20. data/lib/yuno/proofreader/analysis/url_tree.rb +34 -36
  21. data/lib/yuno/proofreader/analysis/weights.rb +67 -69
  22. data/lib/yuno/proofreader/audit/agenda.rb +63 -65
  23. data/lib/yuno/proofreader/audit/agenda_report.rb +44 -46
  24. data/lib/yuno/proofreader/audit/answers.rb +53 -55
  25. data/lib/yuno/proofreader/audit/attempt.rb +50 -52
  26. data/lib/yuno/proofreader/audit/batch.rb +49 -51
  27. data/lib/yuno/proofreader/audit/batch_report.rb +62 -64
  28. data/lib/yuno/proofreader/audit/cadence.rb +47 -49
  29. data/lib/yuno/proofreader/audit/instructions.rb +15 -17
  30. data/lib/yuno/proofreader/audit/log_fields.rb +16 -18
  31. data/lib/yuno/proofreader/audit/prompt.rb +118 -120
  32. data/lib/yuno/proofreader/audit/report.rb +116 -118
  33. data/lib/yuno/proofreader/audit/runner.rb +150 -152
  34. data/lib/yuno/proofreader/audit/schemas.rb +38 -40
  35. data/lib/yuno/proofreader/audit/steps.rb +46 -48
  36. data/lib/yuno/proofreader/cli/analysis_commands.rb +36 -38
  37. data/lib/yuno/proofreader/cli/audit_commands.rb +89 -91
  38. data/lib/yuno/proofreader/cli/command_log.rb +34 -36
  39. data/lib/yuno/proofreader/cli/flags.rb +33 -35
  40. data/lib/yuno/proofreader/cli/generate_commands.rb +99 -101
  41. data/lib/yuno/proofreader/cli/init_command.rb +123 -125
  42. data/lib/yuno/proofreader/cli/model_commands.rb +35 -37
  43. data/lib/yuno/proofreader/cli/queue_commands.rb +79 -81
  44. data/lib/yuno/proofreader/cli/sheet_commands.rb +42 -44
  45. data/lib/yuno/proofreader/cli.rb +197 -199
  46. data/lib/yuno/proofreader/config.rb +13 -17
  47. data/lib/yuno/proofreader/context.rb +45 -50
  48. data/lib/yuno/proofreader/env_file.rb +52 -56
  49. data/lib/yuno/proofreader/errors.rb +7 -9
  50. data/lib/yuno/proofreader/findings/finding.rb +73 -75
  51. data/lib/yuno/proofreader/findings/matching.rb +21 -23
  52. data/lib/yuno/proofreader/findings/merger.rb +35 -37
  53. data/lib/yuno/proofreader/findings/outline.rb +50 -52
  54. data/lib/yuno/proofreader/findings/plan.rb +14 -16
  55. data/lib/yuno/proofreader/findings/reconciler.rb +170 -172
  56. data/lib/yuno/proofreader/findings/source.rb +54 -56
  57. data/lib/yuno/proofreader/findings/text.rb +24 -26
  58. data/lib/yuno/proofreader/generate/cache.rb +54 -56
  59. data/lib/yuno/proofreader/generate/collector.rb +98 -100
  60. data/lib/yuno/proofreader/generate/describe.rb +18 -20
  61. data/lib/yuno/proofreader/generate/llm/prompt.rb +50 -52
  62. data/lib/yuno/proofreader/generate/llm/reader.rb +84 -86
  63. data/lib/yuno/proofreader/generate/llm/wording.rb +12 -14
  64. data/lib/yuno/proofreader/generate/llms_builder.rb +36 -38
  65. data/lib/yuno/proofreader/generate/llms_report.rb +82 -84
  66. data/lib/yuno/proofreader/generate/llms_txt.rb +99 -101
  67. data/lib/yuno/proofreader/generate/llms_writer.rb +43 -45
  68. data/lib/yuno/proofreader/generate/outline.rb +67 -69
  69. data/lib/yuno/proofreader/generate/passage.rb +41 -43
  70. data/lib/yuno/proofreader/generate/saved_answer.rb +42 -44
  71. data/lib/yuno/proofreader/http/client.rb +94 -96
  72. data/lib/yuno/proofreader/http/net_http_transport.rb +35 -37
  73. data/lib/yuno/proofreader/http/robots.rb +51 -53
  74. data/lib/yuno/proofreader/init/ask.rb +45 -47
  75. data/lib/yuno/proofreader/init/detector.rb +75 -77
  76. data/lib/yuno/proofreader/init/interview.rb +155 -157
  77. data/lib/yuno/proofreader/init/parsing.rb +37 -39
  78. data/lib/yuno/proofreader/init/web.rb +22 -24
  79. data/lib/yuno/proofreader/llm/client.rb +116 -118
  80. data/lib/yuno/proofreader/llm/defaults.rb +10 -12
  81. data/lib/yuno/proofreader/llm/errors.rb +17 -19
  82. data/lib/yuno/proofreader/llm/factory.rb +34 -36
  83. data/lib/yuno/proofreader/llm/handoff.rb +86 -88
  84. data/lib/yuno/proofreader/llm/http_transport.rb +25 -27
  85. data/lib/yuno/proofreader/llm/json_text.rb +12 -14
  86. data/lib/yuno/proofreader/llm/provider.rb +49 -51
  87. data/lib/yuno/proofreader/llm/providers/gemini.rb +111 -113
  88. data/lib/yuno/proofreader/llm/providers/manual.rb +34 -36
  89. data/lib/yuno/proofreader/llm/providers.rb +17 -19
  90. data/lib/yuno/proofreader/llm/types.rb +6 -8
  91. data/lib/yuno/proofreader/llm/usage.rb +8 -10
  92. data/lib/yuno/proofreader/log.rb +88 -96
  93. data/lib/yuno/proofreader/pages/fetcher.rb +53 -55
  94. data/lib/yuno/proofreader/pages/report.rb +37 -39
  95. data/lib/yuno/proofreader/pages/snapshot.rb +32 -34
  96. data/lib/yuno/proofreader/pages/trimmer.rb +23 -25
  97. data/lib/yuno/proofreader/settings_file.rb +35 -38
  98. data/lib/yuno/proofreader/site.rb +52 -54
  99. data/lib/yuno/proofreader/site_reader.rb +122 -124
  100. data/lib/yuno/proofreader/sitemap/aligner.rb +58 -60
  101. data/lib/yuno/proofreader/sitemap/importer.rb +19 -21
  102. data/lib/yuno/proofreader/sitemap/loader.rb +95 -97
  103. data/lib/yuno/proofreader/sitemap/mismatch.rb +53 -55
  104. data/lib/yuno/proofreader/sitemap/pairer.rb +171 -173
  105. data/lib/yuno/proofreader/sitemap/parser.rb +50 -52
  106. data/lib/yuno/proofreader/sitemap/report.rb +72 -74
  107. data/lib/yuno/proofreader/store/cells.rb +53 -57
  108. data/lib/yuno/proofreader/store/gateways/csv.rb +53 -56
  109. data/lib/yuno/proofreader/store/gateways/google.rb +152 -158
  110. data/lib/yuno/proofreader/store/memory.rb +60 -64
  111. data/lib/yuno/proofreader/store/records.rb +18 -20
  112. data/lib/yuno/proofreader/store/schema.rb +85 -89
  113. data/lib/yuno/proofreader/store/sheets.rb +132 -145
  114. data/lib/yuno/proofreader/store/tab_setup.rb +51 -54
  115. data/lib/yuno/proofreader/version.rb +1 -1
  116. data/lib/yuno/proofreader.rb +2 -4
  117. metadata +2 -2
@@ -1,108 +1,104 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Store
6
- STATUSES = ["open", "on staging", "done", "rejected"].freeze
3
+ module Yuno::Proofreader
4
+ module Store
5
+ STATUSES = ["open", "on staging", "done", "rejected"].freeze
7
6
 
8
- # One column of a tab. type is :text, :datetime (a sheet date, stored as a serial number), :integer or
9
- # :number (a decimal number).
10
- Column = Data.define(:name, :type, :width, :wrap) do
11
- def initialize(name:, type: :text, width: 120, wrap: false) = super
12
- end
13
-
14
- # What a tab looks like: its columns, the ones that must exist, the dropdown columns and the grid size.
15
- TabSchema = Data.define(:title, :columns, :required, :dropdowns, :rows) do
16
- def names = columns.map(&:name)
7
+ Column = Data.define(:name, :type, :width, :wrap) do
8
+ def initialize(name:, type: :text, width: 120, wrap: false) = super
9
+ end
17
10
 
18
- def column(name) = columns.find { |column| column.name == name }
11
+ # What a tab looks like: its columns, the ones that must exist, the dropdown columns and the grid size.
12
+ TabSchema = Data.define(:title, :columns, :required, :dropdowns, :rows) do
13
+ def names = columns.map(&:name)
19
14
 
20
- # The columns of the schema that a header row lacks, required or not.
21
- def absent(header) = names - normalized(header)
15
+ def column(name) = columns.find { |column| column.name == name }
22
16
 
23
- def check!(header)
24
- missing = required - normalized(header)
25
- return if missing.empty?
17
+ # The columns of the schema that a header row lacks, required or not.
18
+ def absent(header) = names - normalized(header)
26
19
 
27
- raise StoreError, "Tab #{title} is missing the column(s) #{missing.join(', ')}. Add them to the header row " \
28
- "with exactly these names, or delete the tab and run prepare again."
29
- end
20
+ def check!(header)
21
+ missing = required - normalized(header)
22
+ return if missing.empty?
30
23
 
31
- def normalized(header) = header.map { |name| name.to_s.strip.downcase }
24
+ raise StoreError, "Tab #{title} is missing the column(s) #{missing.join(', ')}. Add them to the header row " \
25
+ "with exactly these names, or delete the tab and run prepare again."
32
26
  end
33
27
 
34
- # The tabs the tool works with.
35
- module Schema
36
- SUGGESTION_COLUMNS = [
37
- Column.new(name: "timestamp", type: :datetime, width: 150),
38
- Column.new(name: "page", width: 180),
39
- Column.new(name: "language", width: 80),
40
- Column.new(name: "category", width: 100),
41
- Column.new(name: "effort", width: 80),
42
- Column.new(name: "approximate_place", width: 220, wrap: true),
43
- Column.new(name: "original", width: 300, wrap: true),
44
- Column.new(name: "suggested", width: 300, wrap: true),
45
- Column.new(name: "reason", width: 300, wrap: true),
46
- Column.new(name: "verified", width: 80),
47
- Column.new(name: "status", width: 100),
48
- Column.new(name: "found_by", width: 110),
49
- Column.new(name: "last_seen", type: :datetime, width: 150),
50
- Column.new(name: "amount_seen", type: :integer, width: 90)
51
- ].freeze
52
- SUGGESTION_REQUIRED = %w[page language category effort original suggested reason verified status].freeze
28
+ def normalized(header) = header.map { |name| name.to_s.strip.downcase }
29
+ end
53
30
 
54
- # Written by the analysis, except boost, which a person fills in.
55
- IMPORTANCE_COLUMNS = [
56
- Column.new(name: "importance", type: :integer, width: 100),
57
- Column.new(name: "tier", type: :integer, width: 60),
58
- Column.new(name: "importance_notes", width: 320, wrap: true),
59
- Column.new(name: "analyzed_at", type: :datetime, width: 150),
60
- Column.new(name: "boost", type: :number, width: 70)
61
- ].freeze
31
+ # The tabs the tool works with.
32
+ module Schema
33
+ SUGGESTION_COLUMNS = [
34
+ Column.new(name: "timestamp", type: :datetime, width: 150),
35
+ Column.new(name: "page", width: 180),
36
+ Column.new(name: "language", width: 80),
37
+ Column.new(name: "category", width: 100),
38
+ Column.new(name: "effort", width: 80),
39
+ Column.new(name: "approximate_place", width: 220, wrap: true),
40
+ Column.new(name: "original", width: 300, wrap: true),
41
+ Column.new(name: "suggested", width: 300, wrap: true),
42
+ Column.new(name: "reason", width: 300, wrap: true),
43
+ Column.new(name: "verified", width: 80),
44
+ Column.new(name: "status", width: 100),
45
+ Column.new(name: "found_by", width: 110),
46
+ Column.new(name: "last_seen", type: :datetime, width: 150),
47
+ Column.new(name: "amount_seen", type: :integer, width: 90)
48
+ ].freeze
49
+ SUGGESTION_REQUIRED = %w[page language category effort original suggested reason verified status].freeze
62
50
 
63
- ANALYSIS_COLUMNS = [
64
- Column.new(name: "page_name", width: 220),
65
- Column.new(name: "address", width: 300),
66
- Column.new(name: "importance", type: :integer, width: 100),
67
- Column.new(name: "tier", type: :integer, width: 60),
68
- Column.new(name: "reach", type: :number, width: 80),
69
- Column.new(name: "presence", type: :number, width: 80),
70
- Column.new(name: "mentions", type: :number, width: 80),
71
- Column.new(name: "freshness", type: :number, width: 80),
72
- Column.new(name: "changed", width: 100),
73
- Column.new(name: "route", width: 260, wrap: true),
74
- Column.new(name: "ai_value", type: :integer, width: 80),
75
- Column.new(name: "ai_kind", width: 100),
76
- Column.new(name: "ai_place", width: 180),
77
- Column.new(name: "ai_certainty", width: 90),
78
- Column.new(name: "ai_reason", width: 320, wrap: true),
79
- Column.new(name: "ai_spread", type: :integer, width: 80),
80
- Column.new(name: "analyzed_at", type: :datetime, width: 150)
81
- ].freeze
51
+ # Written by the analysis, except boost, which a person fills in.
52
+ IMPORTANCE_COLUMNS = [
53
+ Column.new(name: "importance", type: :integer, width: 100),
54
+ Column.new(name: "tier", type: :integer, width: 60),
55
+ Column.new(name: "importance_notes", width: 320, wrap: true),
56
+ Column.new(name: "analyzed_at", type: :datetime, width: 150),
57
+ Column.new(name: "boost", type: :number, width: 70)
58
+ ].freeze
82
59
 
83
- module_function
60
+ ANALYSIS_COLUMNS = [
61
+ Column.new(name: "page_name", width: 220),
62
+ Column.new(name: "address", width: 300),
63
+ Column.new(name: "importance", type: :integer, width: 100),
64
+ Column.new(name: "tier", type: :integer, width: 60),
65
+ Column.new(name: "reach", type: :number, width: 80),
66
+ Column.new(name: "presence", type: :number, width: 80),
67
+ Column.new(name: "mentions", type: :number, width: 80),
68
+ Column.new(name: "freshness", type: :number, width: 80),
69
+ Column.new(name: "changed", width: 100),
70
+ Column.new(name: "route", width: 260, wrap: true),
71
+ Column.new(name: "ai_value", type: :integer, width: 80),
72
+ Column.new(name: "ai_kind", width: 100),
73
+ Column.new(name: "ai_place", width: 180),
74
+ Column.new(name: "ai_certainty", width: 90),
75
+ Column.new(name: "ai_reason", width: 320, wrap: true),
76
+ Column.new(name: "ai_spread", type: :integer, width: 80),
77
+ Column.new(name: "analyzed_at", type: :datetime, width: 150)
78
+ ].freeze
84
79
 
85
- def pages(languages)
86
- urls = languages.map { |code| Column.new(name: "#{code}_url", width: 320) }
87
- columns = [Column.new(name: "page_name", width: 220), *urls,
88
- Column.new(name: "last_audited", type: :datetime, width: 150),
89
- Column.new(name: "fingerprint", width: 130),
90
- Column.new(name: "note", width: 200),
91
- Column.new(name: "sitemap_lastmod", width: 130),
92
- *IMPORTANCE_COLUMNS]
93
- required = ["page_name", *urls.map(&:name), "last_audited"]
94
- TabSchema.new(title: "Pages", columns:, required:, dropdowns: {}, rows: 1_000)
95
- end
80
+ module_function
96
81
 
97
- def suggestions
98
- TabSchema.new(title: "Suggestions", columns: SUGGESTION_COLUMNS, required: SUGGESTION_REQUIRED,
99
- dropdowns: { "status" => STATUSES }, rows: 5_000)
100
- end
82
+ def pages(languages)
83
+ urls = languages.map { |code| Column.new(name: "#{code}_url", width: 320) }
84
+ columns = [Column.new(name: "page_name", width: 220), *urls,
85
+ Column.new(name: "last_audited", type: :datetime, width: 150),
86
+ Column.new(name: "fingerprint", width: 130),
87
+ Column.new(name: "note", width: 200),
88
+ Column.new(name: "sitemap_lastmod", width: 130),
89
+ *IMPORTANCE_COLUMNS]
90
+ required = ["page_name", *urls.map(&:name), "last_audited"]
91
+ TabSchema.new(title: "Pages", columns:, required:, dropdowns: {}, rows: 1_000)
92
+ end
93
+
94
+ def suggestions
95
+ TabSchema.new(title: "Suggestions", columns: SUGGESTION_COLUMNS, required: SUGGESTION_REQUIRED,
96
+ dropdowns: { "status" => STATUSES }, rows: 5_000)
97
+ end
101
98
 
102
- def analysis
103
- TabSchema.new(title: "Analysis", columns: ANALYSIS_COLUMNS, required: %w[page_name address], dropdowns: {},
104
- rows: 1_000)
105
- end
99
+ def analysis
100
+ TabSchema.new(title: "Analysis", columns: ANALYSIS_COLUMNS, required: %w[page_name address], dropdowns: {},
101
+ rows: 1_000)
106
102
  end
107
103
  end
108
104
  end
@@ -1,187 +1,174 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Store
6
- # Keeps the pages, the suggestions and the analysis in tabs, as the rows of a Google Sheet or as CSV files.
7
- class Sheets
8
- Table = Data.define(:index, :rows, :width)
9
- IMPORTANCE = %w[importance tier importance_notes analyzed_at].freeze
10
-
11
- def self.connect(config, site)
12
- require_relative "gateways/google"
13
- new(GoogleGateway.new(spreadsheet_id: config.spreadsheet_id), site)
14
- end
15
-
16
- # The same store on a folder of CSV files.
17
- def self.local(site, dir:)
18
- schemas = [Schema.pages(site.languages), Schema.suggestions, Schema.analysis]
19
- new(CsvGateway.new(dir:, schemas:), site)
20
- end
3
+ module Yuno::Proofreader
4
+ module Store
5
+ # Keeps the pages, the suggestions and the analysis in tabs
6
+ class Sheets
7
+ Table = Data.define(:index, :rows, :width)
8
+ IMPORTANCE = %w[importance tier importance_notes analyzed_at].freeze
9
+
10
+ def self.connect(config, site)
11
+ require_relative "gateways/google"
12
+ new(GoogleGateway.new(spreadsheet_id: config.spreadsheet_id), site)
13
+ end
21
14
 
22
- def initialize(gateway, site)
23
- @gateway = gateway
24
- @languages = site.languages
25
- @pages_schema = Schema.pages(@languages)
26
- @suggestions_schema = Schema.suggestions
27
- @analysis_schema = Schema.analysis
28
- end
15
+ def self.local(site, dir:)
16
+ schemas = [Schema.pages(site.languages), Schema.suggestions, Schema.analysis]
17
+ new(CsvGateway.new(dir:, schemas:), site)
18
+ end
29
19
 
30
- # Creates the tabs that are missing and returns a line for each thing it did or found.
31
- def prepare = TabSetup.new(@gateway, [@pages_schema, @suggestions_schema, @analysis_schema]).call
20
+ def initialize(gateway, site)
21
+ @gateway = gateway
22
+ @languages = site.languages
23
+ @pages_schema = Schema.pages(@languages)
24
+ @suggestions_schema = Schema.suggestions
25
+ @analysis_schema = Schema.analysis
26
+ end
32
27
 
33
- def pages
34
- table = read_table(@pages_schema)
35
- data_rows(table).map { |cells, row| page_record(table, cells, row) }
36
- end
28
+ def prepare = TabSetup.new(@gateway, [@pages_schema, @suggestions_schema, @analysis_schema]).call
37
29
 
38
- # pages: Sitemap::Page objects. Returns how many were added.
39
- def add_pages(pages)
40
- return 0 if pages.empty?
30
+ def pages
31
+ table = read_table(@pages_schema)
32
+ data_rows(table).map { |cells, row| page_record(table, cells, row) }
33
+ end
41
34
 
42
- table = read_table(@pages_schema)
43
- @gateway.append(@pages_schema.title, pages.map { |page| build_row(table, @pages_schema, page_values(page)) })
44
- pages.size
45
- end
35
+ def add_pages(pages)
36
+ return 0 if pages.empty?
46
37
 
47
- def record_audit(record, fingerprint:, at:)
48
- table = read_table(@pages_schema)
49
- change_rows(table, @pages_schema, { record.row => { last_audited: at, fingerprint: } })
50
- end
38
+ table = read_table(@pages_schema)
39
+ @gateway.append(@pages_schema.title, pages.map { |page| build_row(table, @pages_schema, page_values(page)) })
40
+ pages.size
41
+ end
51
42
 
52
- # updates: { row => { importance:, tier:, importance_notes:, analyzed_at: } }, sent in one request. The Pages
53
- # tab must have those columns.
54
- def record_importance(updates)
55
- return if updates.empty?
43
+ def record_audit(record, fingerprint:, at:)
44
+ table = read_table(@pages_schema)
45
+ change_rows(table, @pages_schema, { record.row => { last_audited: at, fingerprint: } })
46
+ end
56
47
 
57
- table = read_table(@pages_schema)
58
- missing = IMPORTANCE - table.index.keys
59
- unless missing.empty?
60
- raise StoreError, "Tab Pages lacks the column(s) #{missing.join(', ')}. Type them at the end of its " \
61
- "header row, with exactly these names. Run prepare to see every column it lacks."
62
- end
48
+ def record_importance(updates)
49
+ return if updates.empty?
63
50
 
64
- change_rows(table, @pages_schema, updates)
51
+ table = read_table(@pages_schema)
52
+ missing = IMPORTANCE - table.index.keys
53
+ unless missing.empty?
54
+ raise StoreError, "Tab Pages lacks the column(s) #{missing.join(', ')}. Type them at the end of its " \
55
+ "header row, with exactly these names. Run prepare to see every column it lacks."
65
56
  end
66
57
 
67
- def suggestions(page_name = nil)
68
- table = read_table(@suggestions_schema)
69
- records = data_rows(table).map { |cells, row| suggestion_record(table, cells, row) }
70
- page_name ? records.select { |record| record.page == page_name } : records
71
- end
58
+ change_rows(table, @pages_schema, updates)
59
+ end
72
60
 
73
- def add_suggestions(records)
74
- return 0 if records.empty?
61
+ def suggestions(page_name = nil)
62
+ table = read_table(@suggestions_schema)
63
+ records = data_rows(table).map { |cells, row| suggestion_record(table, cells, row) }
64
+ page_name ? records.select { |record| record.page == page_name } : records
65
+ end
75
66
 
76
- table = read_table(@suggestions_schema)
77
- rows = records.map { |record| build_row(table, @suggestions_schema, record.to_h.except(:row)) }
78
- @gateway.append(@suggestions_schema.title, rows)
79
- records.size
80
- end
67
+ def add_suggestions(records)
68
+ return 0 if records.empty?
81
69
 
82
- # updates: { row => { field => value } }. All changes are sent in one request.
83
- def update_suggestions(updates)
84
- return if updates.empty?
70
+ table = read_table(@suggestions_schema)
71
+ rows = records.map { |record| build_row(table, @suggestions_schema, record.to_h.except(:row)) }
72
+ @gateway.append(@suggestions_schema.title, rows)
73
+ records.size
74
+ end
85
75
 
86
- change_rows(read_table(@suggestions_schema), @suggestions_schema, updates)
87
- end
76
+ def update_suggestions(updates)
77
+ return if updates.empty?
88
78
 
89
- # The rows of the Analysis tab as hashes by column name, each with its row number.
90
- def analysis_rows
91
- table = read_table(@analysis_schema)
92
- data_rows(table).map { |cells, row| { row:, **attributes(table, @analysis_schema, cells) } }
93
- end
79
+ change_rows(read_table(@suggestions_schema), @suggestions_schema, updates)
80
+ end
94
81
 
95
- # rows: hashes by column name. The rows the tab already has are overwritten in place, the rest are appended,
96
- # and rows left over from a longer earlier run are emptied.
97
- def replace_analysis(rows)
98
- table = read_table(@analysis_schema)
99
- built = rows.map { |values| build_row(table, @analysis_schema, values) }
100
- height = table.rows.size
101
- blank = Array.new(table.width, "")
102
- kept = (0...height).map { |index| built[index] || blank }
103
- @gateway.update_cells(@analysis_schema.title,
104
- kept.each_with_index.flat_map { |values, index| row_cells(index + 2, values) })
105
- @gateway.append(@analysis_schema.title, built.drop(height)) if built.size > height
106
- end
82
+ def analysis_rows
83
+ table = read_table(@analysis_schema)
84
+ data_rows(table).map { |cells, row| { row:, **attributes(table, @analysis_schema, cells) } }
85
+ end
107
86
 
108
- private
87
+ def replace_analysis(rows)
88
+ table = read_table(@analysis_schema)
89
+ built = rows.map { |values| build_row(table, @analysis_schema, values) }
90
+ height = table.rows.size
91
+ blank = Array.new(table.width, "")
92
+ kept = (0...height).map { |index| built[index] || blank }
93
+ @gateway.update_cells(@analysis_schema.title,
94
+ kept.each_with_index.flat_map { |values, index| row_cells(index + 2, values) })
95
+ @gateway.append(@analysis_schema.title, built.drop(height)) if built.size > height
96
+ end
109
97
 
110
- # ---- reading ----
98
+ private
111
99
 
112
- def read_table(schema)
113
- values = @gateway.read(schema.title)
114
- header = values.first.to_a.map { |name| name.to_s.strip.downcase }
115
- schema.check!(header)
116
- Table.new(index: header.each_with_index.to_h, rows: values.drop(1), width: header.size)
117
- end
100
+ # ---- reading ----
118
101
 
119
- # The data rows with their row numbers in the sheet. Rows with nothing in them are skipped.
120
- def data_rows(table)
121
- table.rows.each_with_index.filter_map do |cells, index|
122
- [cells, index + 2] unless cells.all? { |cell| cell.to_s.strip.empty? }
123
- end
124
- end
102
+ def read_table(schema)
103
+ values = @gateway.read(schema.title)
104
+ header = values.first.to_a.map { |name| name.to_s.strip.downcase }
105
+ schema.check!(header)
106
+ Table.new(index: header.each_with_index.to_h, rows: values.drop(1), width: header.size)
107
+ end
125
108
 
126
- def attributes(table, schema, cells)
127
- schema.columns.to_h do |column|
128
- position = table.index[column.name]
129
- [column.name.to_sym, Cells.from_cell(position ? cells[position] : "", column.type)]
130
- end
109
+ def data_rows(table)
110
+ table.rows.each_with_index.filter_map do |cells, index|
111
+ [cells, index + 2] unless cells.all? { |cell| cell.to_s.strip.empty? }
131
112
  end
113
+ end
132
114
 
133
- def page_record(table, cells, row)
134
- attrs = attributes(table, @pages_schema, cells)
135
- urls = @languages.to_h { |code| [code, attrs[:"#{code}_url"].strip] }.reject { |_code, url| url.empty? }
136
- PageRecord.new(row:, name: attrs[:page_name], urls:, last_audited: attrs[:last_audited],
137
- fingerprint: attrs[:fingerprint], note: attrs[:note], sitemap_lastmod: attrs[:sitemap_lastmod],
138
- importance: attrs[:importance], tier: attrs[:tier], boost: attrs[:boost])
115
+ def attributes(table, schema, cells)
116
+ schema.columns.to_h do |column|
117
+ position = table.index[column.name]
118
+ [column.name.to_sym, Cells.from_cell(position ? cells[position] : "", column.type)]
139
119
  end
120
+ end
140
121
 
141
- def suggestion_record(table, cells, row)
142
- SuggestionRecord.build(row:, **attributes(table, @suggestions_schema, cells))
143
- end
122
+ def page_record(table, cells, row)
123
+ attrs = attributes(table, @pages_schema, cells)
124
+ urls = @languages.to_h { |code| [code, attrs[:"#{code}_url"].strip] }.reject { |_code, url| url.empty? }
125
+ PageRecord.new(row:, name: attrs[:page_name], urls:, last_audited: attrs[:last_audited],
126
+ fingerprint: attrs[:fingerprint], note: attrs[:note], sitemap_lastmod: attrs[:sitemap_lastmod],
127
+ importance: attrs[:importance], tier: attrs[:tier], boost: attrs[:boost])
128
+ end
144
129
 
145
- # ---- writing ----
130
+ def suggestion_record(table, cells, row)
131
+ SuggestionRecord.build(row:, **attributes(table, @suggestions_schema, cells))
132
+ end
146
133
 
147
- def page_values(page)
148
- values = { "page_name" => page.name, "note" => page.notes.join("; "), "sitemap_lastmod" => page.lastmod }
149
- @languages.each { |code| values["#{code}_url"] = page.urls[code] }
150
- values
151
- end
134
+ # ---- writing ----
135
+
136
+ def page_values(page)
137
+ values = { "page_name" => page.name, "note" => page.notes.join("; "), "sitemap_lastmod" => page.lastmod }
138
+ @languages.each { |code| values["#{code}_url"] = page.urls[code] }
139
+ values
140
+ end
152
141
 
153
- # A row as wide as the header, with each value in the column of its name. Columns the sheet lacks are skipped.
154
- def build_row(table, schema, values)
155
- cells = Array.new(table.width, "")
156
- values.each do |name, value|
157
- position = table.index[name.to_s]
158
- cells[position] = Cells.to_cell(value, schema.column(name.to_s).type) if position
159
- end
160
- cells
142
+ def build_row(table, schema, values)
143
+ cells = Array.new(table.width, "")
144
+ values.each do |name, value|
145
+ position = table.index[name.to_s]
146
+ cells[position] = Cells.to_cell(value, schema.column(name.to_s).type) if position
161
147
  end
148
+ cells
149
+ end
162
150
 
163
- def change_rows(table, schema, changes)
164
- last_row = table.rows.size + 1
165
- cells = changes.flat_map do |row, values|
166
- raise StoreError, "There is no row #{row} in #{schema.title}" unless row.to_i.between?(2, last_row)
151
+ def change_rows(table, schema, changes)
152
+ last_row = table.rows.size + 1
153
+ cells = changes.flat_map do |row, values|
154
+ raise StoreError, "There is no row #{row} in #{schema.title}" unless row.to_i.between?(2, last_row)
167
155
 
168
- changed_cells(table, schema, row, values)
169
- end
170
- @gateway.update_cells(schema.title, cells)
156
+ changed_cells(table, schema, row, values)
171
157
  end
158
+ @gateway.update_cells(schema.title, cells)
159
+ end
172
160
 
173
- def changed_cells(table, schema, row, values)
174
- values.filter_map do |name, value|
175
- column = schema.column(name.to_s)
176
- raise StoreError, "Unknown field #{name} for #{schema.title}" unless column
161
+ def changed_cells(table, schema, row, values)
162
+ values.filter_map do |name, value|
163
+ column = schema.column(name.to_s)
164
+ raise StoreError, "Unknown field #{name} for #{schema.title}" unless column
177
165
 
178
- position = table.index[column.name]
179
- [row, position + 1, Cells.to_cell(value, column.type)] if position
180
- end
166
+ position = table.index[column.name]
167
+ [row, position + 1, Cells.to_cell(value, column.type)] if position
181
168
  end
182
-
183
- def row_cells(row, values) = values.each_with_index.map { |value, index| [row, index + 1, value] }
184
169
  end
170
+
171
+ def row_cells(row, values) = values.each_with_index.map { |value, index| [row, index + 1, value] }
185
172
  end
186
173
  end
187
174
  end
@@ -1,72 +1,69 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Yuno
4
- module Proofreader
5
- module Store
6
- # Sets up the tabs of the sheet: creates the ones that are missing, sets up empty ones, checks the others and
7
- # names the columns they lack, and removes an empty default tab. Returns a line for each thing it did or found.
8
- class TabSetup
9
- DEFAULT_TABS = %w[Sheet1 Tabellenblatt1].freeze
3
+ module Yuno::Proofreader
4
+ module Store
5
+ # Sets up the tabs of the sheet
6
+ class TabSetup
7
+ DEFAULT_TABS = %w[Sheet1 Tabellenblatt1].freeze
10
8
 
11
- def initialize(gateway, schemas)
12
- @gateway = gateway
13
- @schemas = schemas
14
- end
9
+ def initialize(gateway, schemas)
10
+ @gateway = gateway
11
+ @schemas = schemas
12
+ end
15
13
 
16
- def call
17
- tabs = @gateway.tabs
18
- @schemas.flat_map { |schema| prepare_tab(schema, tabs) } + remove_default_tab
19
- end
14
+ def call
15
+ tabs = @gateway.tabs
16
+ @schemas.flat_map { |schema| prepare_tab(schema, tabs) } + remove_default_tab
17
+ end
20
18
 
21
- private
19
+ private
22
20
 
23
- def prepare_tab(schema, tabs)
24
- title = schema.title
25
- return create_tab(schema) unless tabs.key?(title)
21
+ def prepare_tab(schema, tabs)
22
+ title = schema.title
23
+ return create_tab(schema) unless tabs.key?(title)
26
24
 
27
- header = @gateway.read(title).first.to_a
28
- return fill_tab(schema, tabs[title]) if header.empty?
25
+ header = @gateway.read(title).first.to_a
26
+ return fill_tab(schema, tabs[title]) if header.empty?
29
27
 
30
- schema.check!(header)
31
- existing(schema, header)
32
- end
28
+ schema.check!(header)
29
+ existing(schema, header)
30
+ end
33
31
 
34
- # A tab that exists keeps its columns as they are. Columns a newer version of the tool added are named, so
35
- # they can be typed at the end of the header row.
36
- def existing(schema, header)
37
- absent = schema.absent(header)
38
- return ["Tab #{schema.title} already exists."] if absent.empty?
32
+ # A tab that exists keeps its columns as they are. Columns a newer version of the tool added are named, so
33
+ # they can be typed at the end of the header row.
34
+ def existing(schema, header)
35
+ absent = schema.absent(header)
36
+ return ["Tab #{schema.title} already exists."] if absent.empty?
39
37
 
40
- ["Tab #{schema.title} already exists, but lacks the column(s) #{absent.join(', ')}. Type them at the end " \
41
- "of its header row, with exactly these names."]
42
- end
38
+ ["Tab #{schema.title} already exists, but lacks the column(s) #{absent.join(', ')}. Type them at the end " \
39
+ "of its header row, with exactly these names."]
40
+ end
43
41
 
44
- def create_tab(schema)
45
- sheet_id = @gateway.add_tab(schema.title, rows: schema.rows, columns: schema.columns.size)
46
- write_header(schema)
47
- @gateway.format_tab(sheet_id, schema)
48
- ["Created tab #{schema.title}."]
49
- end
42
+ def create_tab(schema)
43
+ sheet_id = @gateway.add_tab(schema.title, rows: schema.rows, columns: schema.columns.size)
44
+ write_header(schema)
45
+ @gateway.format_tab(sheet_id, schema)
46
+ ["Created tab #{schema.title}."]
47
+ end
50
48
 
51
- def fill_tab(schema, sheet_id)
52
- write_header(schema)
53
- @gateway.format_tab(sheet_id, schema)
54
- ["Set up the empty tab #{schema.title}."]
55
- end
49
+ def fill_tab(schema, sheet_id)
50
+ write_header(schema)
51
+ @gateway.format_tab(sheet_id, schema)
52
+ ["Set up the empty tab #{schema.title}."]
53
+ end
56
54
 
57
- def write_header(schema)
58
- cells = schema.names.each_with_index.map { |name, index| [1, index + 1, name] }
59
- @gateway.update_cells(schema.title, cells)
60
- end
55
+ def write_header(schema)
56
+ cells = schema.names.each_with_index.map { |name, index| [1, index + 1, name] }
57
+ @gateway.update_cells(schema.title, cells)
58
+ end
61
59
 
62
- def remove_default_tab
63
- ours = @schemas.map(&:title)
64
- @gateway.tabs.filter_map do |title, sheet_id|
65
- next unless DEFAULT_TABS.include?(title) && !ours.include?(title) && @gateway.read(title).empty?
60
+ def remove_default_tab
61
+ ours = @schemas.map(&:title)
62
+ @gateway.tabs.filter_map do |title, sheet_id|
63
+ next unless DEFAULT_TABS.include?(title) && !ours.include?(title) && @gateway.read(title).empty?
66
64
 
67
- @gateway.delete_tab(sheet_id)
68
- "Removed the empty tab #{title}."
69
- end
65
+ @gateway.delete_tab(sheet_id)
66
+ "Removed the empty tab #{title}."
70
67
  end
71
68
  end
72
69
  end