forked from nathanl/secret_santa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret_santa.rb
111 lines (91 loc) · 3.25 KB
/
secret_santa.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env ruby
# Choose type of sender, :email or :twilio
SENDER_TYPE = :twilio
$LOAD_PATH << '.'
require 'yaml'
require 'person'
require 'emailer'
require 'email'
require 'twilio_sender' if SENDER_TYPE == :twilio
require 'santa_logger'
require 'pry'
# Do some testing, then set this to true when ready to send
# Will cause logging output to be shushed and mails to be sent
REALLY_SENDING = false
logger = SantaLogger.new
people_config = YAML.load_file('config/people_peer.yml')
people = people_config['people'].map do |attrs|
Person.new(attrs)
end
santas = people.dup.shuffle
people.each do |person|
person.santa = santas.delete_at(rand(santas.size))
end
logger.log "Initial Santa assignments:"
people.each do |person|
logger.log person.with_santa
end
# This is the nice part of Dennis's solution: if there are any invalid
# assignments, they are corrected in as few passes as possible. (I
# originally said "a single pass", but that's not true because the
# `select` that looks for someone to swap santas with is just a
# way of saying "loop through the list of people looking for a match.")
# This works because corrections are made in a way that ensures no new
# invalid assignments are created.
logger.log "Checking assignments for validity"
people.each do |person|
unless person.santa.can_be_santa_of?(person)
logger.log "\n#{person} can't get a gift from #{person.santa}! Let's try to fix that..."
swap_candidates = people.select {|p| person.can_swap_santas_with?(p) }
raise "Failure! No one can swap santas with #{person}" if swap_candidates.empty?
logger.log "Any of these can swap santas with #{person}: #{swap_candidates}"
swapper = swap_candidates.sample
logger.log "Chose #{swapper} to swap santas with #{person}"
misplaced_santa = person.santa
person.santa = swapper.santa
swapper.santa = misplaced_santa
end
end
logger.log "\n\nFinal Santa assignments:"
people.each do |person|
logger.log person.with_santa
end
case SENDER_TYPE
when :email
smtp_config = YAML.load_file('config/smtp.yml')
emailer = Emailer.new(
smtp_config['smtp_server'],
smtp_config['domain'],
smtp_config['account_address'],
smtp_config['account_password']
)
people.each do |person|
message = <<-MESSAGE
GREETINGS #{person.santa.name.upcase},
HATS ARE NOW OBSELETE.
SANTABOT 5000 HAS BEEN ACTIVATED. YOU HAVE BEEN CHOSEN AS A SECRET SANTA.
YOUR TARGET IS AS FOLLOWS:
#{person.name.upcase}
THIS INFORMATION HAS BEEN KEPT SECRET FROM ALL HUMANS BUT YOU.
IF I, SANTABOT, HAD EMOTIONS, I WOULD WISH YOU A MERRY CHRISTMAS,
BUT AS IT IS THAT WOULD NOT REALLY MAKE SENSE.
THAT IS ALL.
--SANTABOT 5000
MESSAGE
email = Email.new(person.santa.email, "SANTABOT 5000: #{Time.now.year} TARGETS", message)
emailer.send(email)
end
when :twilio
twilio_config = YAML.load_file('config/twilio.yml')
twilio_sender = TwilioSender.new(
twilio_config['twilio_account_sid'],
twilio_config['twilio_auth_token'],
twilio_config['twilio_from_number']
)
people.each do |person|
message = "Hey #{person.santa.name}, koop voor deze huskens-kerst een cadeau voor #{person.name.upcase}"
twilio_sender.send(person.santa.phone, message)
end
else
raise 'Unknown sender type'
end