class TransmissionRSS::SeenFile

Persist seen torrent URLs

Constants

DEFAULT_LEGACY_PATH
DEFAULT_PATH

Public Class Methods

new(path = nil, legacy_path = nil) click to toggle source
# File lib/transmission-rss/seen_file.rb, line 19
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 29
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 41
def clear!
  @seen.clear
  open(@path, 'w') {}
end
include?(url) click to toggle source
# File lib/transmission-rss/seen_file.rb, line 46
def include?(url)
  @seen.include?(digest(url))
end

Private Instance Methods

digest(s) click to toggle source
# File lib/transmission-rss/seen_file.rb, line 52
def digest(s)
  Digest::SHA256.hexdigest(s)
end
file_to_array(path) click to toggle source
# File lib/transmission-rss/seen_file.rb, line 56
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 60
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 67
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