class Twitter2Mastodon::Store

Attributes

store[R]

Public Class Methods

new() click to toggle source
# File lib/twitter2mastodon/store.rb, line 8
def initialize
  @store_file = find_or_create_store
  @store = JSON.parse(File.read(@store_file))
end

Public Instance Methods

add_to_store(tweet_object) click to toggle source
# File lib/twitter2mastodon/store.rb, line 17
def add_to_store(tweet_object)
  status = tweet_object
  id = make_hash(tweet_object)

  return nil unless never_been_published?(tweet_object)

  clean_store

  @store[id] = { name: status.name, message: status.message, uri: status.url.to_str }
  File.write(@store_file, @store.to_json)

  tweet_object
end
never_been_published?(twitt) click to toggle source
# File lib/twitter2mastodon/store.rb, line 13
def never_been_published?(twitt)
  @store.none? { |tweet| tweet[0] == make_hash(twitt) }
end

Private Instance Methods

clean_store() click to toggle source
# File lib/twitter2mastodon/store.rb, line 33
def clean_store
  return unless @store.size > 100

  @store.shift
end
find_or_create_store() click to toggle source
# File lib/twitter2mastodon/store.rb, line 39
def find_or_create_store
  file = File.join(Etc.getpwuid.dir, ".twitter2mastodon.json")
  return file if File.exist?(file)

  FileUtils.touch file
  File.write(file, "{}")
end
make_hash(tweet_object) click to toggle source
# File lib/twitter2mastodon/store.rb, line 47
def make_hash(tweet_object)
  Digest::MD5.hexdigest tweet_object.message + tweet_object.id.to_s
end