Class: Mobile::SecretSanta

Inherits:
Object
  • Object
show all
Defined in:
lib/mobile/secret_santa.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SecretSanta

Returns a new instance of SecretSanta.



10
11
12
13
14
15
# File 'lib/mobile/secret_santa.rb', line 10

def initialize(options)
  @list = options[:list]
  @logger = options[:logger] || NoopLogger.new
  @host_number = options[:twilio_config][:host_number]
  @twilio_client = Twilio::REST::Client.new(options[:twilio_config][:account_sid], options[:twilio_config][:auth_token])
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/mobile/secret_santa.rb', line 8

def logger
  @logger
end

#namesObject (readonly)

Returns the value of attribute names.



8
9
10
# File 'lib/mobile/secret_santa.rb', line 8

def names
  @names
end

#pairsObject (readonly)

Returns the value of attribute pairs.



8
9
10
# File 'lib/mobile/secret_santa.rb', line 8

def pairs
  @pairs
end

Instance Method Details

#pair_listObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mobile/secret_santa.rb', line 17

def pair_list
  shuffled_names = names.shuffle
  @pairs = shuffled_names.each.with_index.reduce([]) do |pair, (person, index)|
    pair << {
      :santa => {
        :name => person, :number => @list[person]
      },
      :person => shuffled_names[next_person(index)]
    }
    pair
  end
end

#send(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mobile/secret_santa.rb', line 30

def send(options = {})
  pairs = @pairs.clone
  pairs.each { |pair| pair[:santa][:name] = Digest::SHA256.hexdigest(pair[:santa][:name]) }
  @logger.info(pairs.to_json)

  pairs.each { |pair|
    @twilio_client
      .
      .messages
      .create(payload(pair[:santa][:number], pair[:person], options[:text_body]))
  }
end