class Whatzapper::Zapper

Constants

WTFS

Attributes

path[RW]

Public Class Methods

new(path) click to toggle source
# File lib/whatzapper/zapper.rb, line 12
def initialize(path)
  @path = Pathname.new path
end

Public Instance Methods

clean_db() click to toggle source
# File lib/whatzapper/zapper.rb, line 16
def clean_db
  work_in_writable_space do |working_db|
    zap(working_db)
  end
end

Protected Instance Methods

zap(path) click to toggle source
# File lib/whatzapper/zapper.rb, line 23
def zap(path)
  open_db(path) do |db|
    open_chats(db) do |message|
      pk, text = message['Z_PK'], message['ZTEXT']
      puts "reading #{text}"
      next unless text =~ WTFS
      puts "culprit: #{text}"
      clean_message(db, pk, text)
    end
  end
end

Private Instance Methods

clean_message(db, pk, text) click to toggle source
# File lib/whatzapper/zapper.rb, line 61
def clean_message(db, pk, text)
  cleaned_text = text.sub(WTFS) do |match|
    "#{match[0]}*"
  end
  puts "cleaned: #{cleaned_text}"
  puts db.execute("UPDATE ZWAMESSAGE SET ZTEXT = ? WHERE Z_PK = ?;", cleaned_text, pk)
end
open_chats(db) { |row| ... } click to toggle source
# File lib/whatzapper/zapper.rb, line 44
def open_chats(db)
  stmnt = db.prepare("select * from ZWAMESSAGE")
    stmnt.execute.each_hash do |row|
      yield row
    end
  stmnt.close
end
open_db(path) { |db| ... } click to toggle source
# File lib/whatzapper/zapper.rb, line 36
def open_db(path)
  SQLite3::Database.new(path) do |db|
    puts "Database.readonly? #{db.readonly?}"
    yield db
    puts "Total Changed: #{db.total_changes}"
  end
end
work_in_writable_space() { |working_db| ... } click to toggle source
# File lib/whatzapper/zapper.rb, line 52
def work_in_writable_space
  Dir.mktmpdir do |tmp_dir|
    FileUtils.cp self.path.realpath, tmp_dir
    working_db = (Pathname.new(tmp_dir) + self.path.basename).to_s
    yield working_db
    FileUtils.cp working_db, self.path
  end
end