diff options
author | mms <git@sapka.me> | 2025-01-06 22:17:43 +0100 |
---|---|---|
committer | mms <git@sapka.me> | 2025-01-06 22:17:43 +0100 |
commit | b50e4a5f40bc64fa1893d249d83c05ecd217de1d (patch) | |
tree | d98b5c3a378975d095aa3dad8d1a642c3cf0f3d2 /new/lib/crys/pages_db_manager.rb | |
parent | bfd8209ad0d80b5027bea8a1a095dadc8bffdc61 (diff) |
feat(new): rss + batch
Diffstat (limited to 'new/lib/crys/pages_db_manager.rb')
-rw-r--r-- | new/lib/crys/pages_db_manager.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/new/lib/crys/pages_db_manager.rb b/new/lib/crys/pages_db_manager.rb new file mode 100644 index 0000000..6a5e71c --- /dev/null +++ b/new/lib/crys/pages_db_manager.rb @@ -0,0 +1,62 @@ +module Crys + class PagesDbManager + PAGES_KEY = :pages + PAGE = Struct.new(:filename, :file_path, :last_update, :in_rss, keyword_init: true) + + def db_path + end + + def add_page(processor) + @processor = processor + hash = Digest::MD5.hexdigest(processor.file_path) + known_page = known_page(hash) + + upsert_db(hash, known_page) + save_db + end + + def pages + db.fetch(:pages).map do | _, data| + PAGE.new(data) + end + end + + private + + attr_reader :hash, :processor + + + def db + @db ||= YAML.load_file( + db_path, + permitted_classes: [Time, Symbol] + ) + end + + def upsert_db(hash, known_page) + if known_page + record = PAGE.new(known_page) + else + record = PAGE.new( + filename: processor.filename, + file_path: processor.file_path, + in_rss: true + ) + end + + record.last_update = Time.now + + @db[PAGES_KEY][hash] = record.to_h + end + + def save_db + File.open(db_path, 'w') do |f| + f.write db.to_yaml + end + end + + def known_page(hash) + db[PAGES_KEY][hash] + end + end +end |