Class: Powerball::Lottery

Inherits:
Object
  • Object
show all
Defined in:
lib/powerball/lottery.rb

Instance Method Summary collapse

Constructor Details

#initialize(attendees = 'attendees.csv', winners = 'winners.csv') ⇒ Lottery

phone = item



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/powerball/lottery.rb', line 17

def initialize(attendees = 'attendees.csv', winners = 'winners.csv')
  @winners   = CSV.read(winners) rescue []
  @attendees = CSV.read(attendees) rescue []
  @header    = @attendees.shift # remove header

  @attendees.reject! do |attendee|
    email   = attendee[4].strip.downcase
    company = attendee[16].strip.downcase

    (company == 'puppet'                \
      || email.end_with?('@puppet.com') \
      || email.end_with?('@puppetlabs.com'))
  end

  @attendees.map! do |attendee|
    attendee[13].strip!
    attendee[13].slice!(0) if attendee[13].start_with?('@')

    attendee
  end

  puts "INFO: #{@attendees.size} eligible attendees"
end

Instance Method Details

#chatroom=(chatroom) ⇒ Object



41
42
43
# File 'lib/powerball/lottery.rb', line 41

def chatroom=(chatroom)
  @chatroom = chatroom
end

#drawingObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/powerball/lottery.rb', line 45

def drawing
  @chatroom.start_drawing

  active = @chatroom.active_members
  pool   = @attendees.select do |attendee|
    active.include? attendee[13]
  end

  puts "INFO: eligible and active attendees: #{pool.map {|a| "#{a[2]} #{a[3]} (#{a[13]})" }.inspect}"

  if pool.size > 0
    winner = pool.sample
    @winners << winner
    @attendees.delete winner

    @chatroom.post_winner(winner[13])
    @chatroom.alert_admins("New winner: #{winner[2]} #{winner[3]} (#{winner[13]})")
    write_winners
  else
    @chatroom.alert_admins("WARNING: there are no currently eligible active members.")
  end
end

#write_winnersObject



68
69
70
71
72
73
74
75
# File 'lib/powerball/lottery.rb', line 68

def write_winners
  CSV.open('winners.csv', "wb") do |csv|
    csv << @header # copy the input header
    @winners.each do |row|
      csv << row
    end
  end
end