class TransmissionRSS::SeenFile

Persist seen torrent URLs

Public Class Methods

new(path = nil, legacy_path = nil) click to toggle source
# File lib/transmission-rss/seen_file.rb, line 13
def initialize(path = nil, legacy_path = nil)
  @legacy_path = legacy_path || default_legacy_path
  @path        = path || default_path

  initialize_path!
  migrate!

  @seen = Set.new(file_to_array(@path))
end

Public Instance Methods

add(url) click to toggle source
# File lib/transmission-rss/seen_file.rb, line 23
def add(url)
  hash = digest(url)
  
  return if @seen.include?(hash)

  @seen << hash

  open(@path, 'a') do |f|
    f.write(hash + "\n")
  end
end
clear!() click to toggle source
# File lib/transmission-rss/seen_file.rb, line 35
def clear!
  @seen.clear
  open(@path, 'w') {}
end
include?(url) click to toggle source
# File lib/transmission-rss/seen_file.rb, line 40
def include?(url)
  @seen.include?(digest(url))
end

Private Instance Methods

default_legacy_path() click to toggle source
# File lib/transmission-rss/seen_file.rb, line 46
def default_legacy_path
  File.join(Etc.getpwuid.dir, '.config/transmission/seen-torrents.conf')
end
default_path() click to toggle source
# File lib/transmission-rss/seen_file.rb, line 50
def default_path
  File.join(Etc.getpwuid.dir, '.config/transmission/seen')
end
digest(s) click to toggle source
# File lib/transmission-rss/seen_file.rb, line 54
def digest(s)
  Digest::SHA256.hexdigest(s)
end
file_to_array(path) click to toggle source
# File lib/transmission-rss/seen_file.rb, line 58
def file_to_array(path)
  open(path, 'r').readlines.map(&:chomp)
end
initialize_path!() click to toggle source
# File lib/transmission-rss/seen_file.rb, line 62
def initialize_path!
  return if File.exist?(@path)

  FileUtils.mkdir_p(File.dirname(@path))
  FileUtils.touch(@path)
end
migrate!() click to toggle source
# File lib/transmission-rss/seen_file.rb, line 69
def migrate!
  return unless File.exist?(@legacy_path)

  legacy_seen = file_to_array(@legacy_path)
  hashes = legacy_seen.map { |url| digest(url) }

  open(@path, 'w') do |f|
    f.write(hashes.join("\n"))
    f.write("\n")
  end

  FileUtils.rm_f(@legacy_path)
end