Class: Tweep::Index

Inherits:
Object show all
Defined in:
lib/tweep/index.rb

Constant Summary collapse

FILE_NAME =
'tweeping.idx'

Instance Method Summary collapse

Constructor Details

#initializeIndex

Returns a new instance of Index.



10
11
12
13
# File 'lib/tweep/index.rb', line 10

def initialize
  @states = {}
  @states = YAML::load(File.read(FILE_NAME)) if File.exists?(FILE_NAME)
end

Instance Method Details

#next_retweet_in!(nick, retweeter, wait) ⇒ Object



15
16
17
# File 'lib/tweep/index.rb', line 15

def next_retweet_in!(nick, retweeter, wait)
  ((@states[nick] ||= {})[:waits] ||= {})[retweeter] = wait
end

#next_tweet_index(nick) ⇒ Object



19
20
21
# File 'lib/tweep/index.rb', line 19

def next_tweet_index(nick)
  (@states[nick] ||= {})[:next].to_i
end

#retweet_timely?(nick, retweeter) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/tweep/index.rb', line 23

def retweet_timely?(nick, retweeter)
  ((@states[nick] ||= {})[:waits] ||= {})[retweeter].to_i.zero?
end

#retweet_will_wait!(nick, retweeter) ⇒ Object



27
28
29
30
# File 'lib/tweep/index.rb', line 27

def retweet_will_wait!(nick, retweeter)
  wait = ((@states[nick] ||= {})[:waits] ||= {})[retweeter].to_i
  @states[nick][:waits][retweeter] = [wait - 1, 0].max
end

#save!Object



32
33
34
35
36
# File 'lib/tweep/index.rb', line 32

def save!
  File.open(FILE_NAME, 'w') { |f| f.write(YAML::dump(@states)) }
rescue Exception => e
  Tweep.error "Could not save index to #{FILE_NAME} (#{e.class.name}: #{e.message})"
end

#tweeted!(nick, idx) ⇒ Object



38
39
40
# File 'lib/tweep/index.rb', line 38

def tweeted!(nick, idx)
  (@states[nick] ||= {})[:next] = idx + 1
end