Module: WTTH

Defined in:
lib/wtth.rb,
lib/wtth/config.rb,
lib/wtth/umassd.rb,
lib/wtth/twitter.rb,
lib/wtth/hvzsource.rb

Constant Summary collapse

VERSION =
'1.2.1'

Class Method Summary collapse

Class Method Details

.initObject



14
15
16
17
# File 'lib/wtth.rb', line 14

def self.init
  load_config
  twitter_init
end

.updateObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wtth.rb', line 19

def self.update
  load_config

  unless twitter_is_authorized?
    puts 'You have not yet authorized WTTH to access Twitter.'
    puts 'Please run `wtth init` first'
    exit 1
  end

  # get new zombies and zombies that weren't reported last time
  to_welcome = CONFIG[:backlog]
  if to_welcome == nil
    to_welcome = get_new_zombies
  elsif to_welcome.join(', ').length < 140
    to_welcome = get_new_zombies + to_welcome
  end

  if to_welcome != []
    this_batch = []
    recovery = []
    size = 0

    # get the least recent kills, no more than 140 characters worth
    until size > 140 or to_welcome.empty?
      zombie = to_welcome.last
      size += zombie.length
      size += 2 if this_batch.length > 0
      if size <= 140
        this_batch << zombie
        recovery << to_welcome.pop
      end
    end

    # remember who we haven't reported
    CONFIG[:backlog] = to_welcome

    # welcome with the most recent first so things are in order on twitter
    this_batch.reverse!

    begin
      tweet(this_batch.join(', '))
    rescue Twitter::TwitterUnavailable
      puts "TwitterUnavailable #{$!}"
      # throw the message onto the backlog so we can try again next time
      CONFIG[:backlog] = to_welcome + recovery.reverse
    rescue Twitter::TwitterError
      puts "TwitterError #{$!}"
      # throw the message onto the backlog so we can try again next time
      CONFIG[:backlog] = to_welcome + recovery.reverse
    end
  end

  save_config
end