secretsanta 1.0.3 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3173eabef8c6f996fbc0ff484b24c304b1a3e63dc4259d281c7b61ce1c967996
4
- data.tar.gz: 4a49421bd59a8738b9e74d83fc103fbcabde68aab3c108b8366df450f512552e
3
+ metadata.gz: 4d303454bdb8a9514390fca95bf5345fb4130594911d1c743e39b0e554ccb2ce
4
+ data.tar.gz: f95b26a7fa26c01f1c935f9bc38abdd6d0039d6bee49b02094e03a639b55fe46
5
5
  SHA512:
6
- metadata.gz: 69b46346b916c79c48053e957300bee61900da35fe9331dbb4a09cdf1c98767ad658d031946dbe578cb705f421c82a52850aeefbcb863e7bcf1eb6606f869727
7
- data.tar.gz: c040c42729d270688ee4a366f81a7ab64fa0f5d1dac5ca55f5ea1bb617946770d1c435e7468d9a3b691e22e5f54bc8572ec34a890aaefb6a1bb36480872a0bb1
6
+ metadata.gz: 8a7897f0595fefe1cf45e7cb7617948673db4f5d778c753279e31a34feda9d1a3418e7cdaba867be2873599ede680ee9c23318b9c132c766d861f65ee24406f0
7
+ data.tar.gz: 458aacc92f2bda8efbaf0d4f392283d4eedc71ac5143a1e46a395aa2907de1506f4a9f37df76d482a2a705a0b0e5553de205b5c21eef3c38baecea87d7880c7c
data/.rubocop.yml CHANGED
@@ -5,7 +5,7 @@ Metrics/AbcSize:
5
5
  Max: 20
6
6
 
7
7
  Metrics/BlockLength:
8
- Max: 50
8
+ Max: 100
9
9
 
10
10
  Metrics/LineLength:
11
11
  Max: 120
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- secretsanta (1.0.3)
4
+ secretsanta (1.1.0)
5
5
  dotenv (~> 2.5)
6
6
  twilio-ruby (~> 5.17)
7
7
 
data/README.md CHANGED
@@ -49,10 +49,9 @@ follows:
49
49
  "number": "+1 (123) 456-7890",
50
50
  "disallow": [
51
51
  "Jane Doe"
52
- ],
53
- "is_assigned": false,
54
- "has_assignment": false
52
+ ]
55
53
  }
54
+ ]
56
55
  }
57
56
  ```
58
57
 
data/bin/secretsanta CHANGED
@@ -52,7 +52,7 @@ begin
52
52
  raise OptionParser::MissingArgument if options.to_h.values.any?(&:nil?)
53
53
 
54
54
  begin
55
- puts SecretSanta.notify_participants!(options)
55
+ puts SecretSanta.new(options).notify_participants!
56
56
  rescue SecretSanta::Error => e
57
57
  puts e
58
58
  exit 1
data/lib/secretsanta.rb CHANGED
@@ -2,63 +2,71 @@
2
2
 
3
3
  require 'twilio-ruby'
4
4
 
5
- require_relative 'secretsanta/version'
6
-
7
- module SecretSanta
5
+ class SecretSanta
8
6
  class Error < StandardError; end
9
7
 
10
- module_function
8
+ VERSION = '1.1.0'
9
+
10
+ def initialize(options)
11
+ @dry_run = options.dry_run
12
+ @from_number = options.from_number
13
+ @participants = options.participants
14
+ @sms = Twilio::REST::Client.new(options.twilio_account_sid, options.twilio_auth_token) unless @dry_run
15
+ end
11
16
 
12
- def notify_participants!(options)
13
- client = Twilio::REST::Client.new(options.twilio_account_sid, options.twilio_auth_token)
14
- pairs = generate_pairs(options.participants)
17
+ def notify_participants!
18
+ pairs = generate_pairs
15
19
 
16
- return pairs if options.dry_run
20
+ return pairs if @dry_run
17
21
 
18
22
  pairs.each do |pair|
19
- client.messages.create(
23
+ @sms.messages.create(
20
24
  body: "Happy holidays! You have been assigned to give a gift to #{pair.recipient_name} this year.",
21
- from: options.from_number,
25
+ from: @from_number,
22
26
  to: pair.sender_number
23
27
  )
24
28
  rescue Twilio::Rest::TwilioError => e
25
29
  puts e.message
26
30
  end
27
31
 
28
- "\nAll participants have been notified - happy holidays! 🎅 🎄 🎁\n\n"
32
+ "\nAll participants have been notified. Happy holidays! 🎅 🎄 🎁\n\n"
29
33
  end
30
34
 
31
- def generate_pairs(participants)
35
+ def generate_pairs
36
+ raise Error, SecretSanta.error_message if impossible?
37
+
32
38
  [].tap do |list|
33
39
  loop do
34
- participants.each do |participant|
35
- index = rand 0..participants.length - 1
36
- recipient = participants[index]
40
+ @participants.each do |sender|
41
+ recipient = @participants.sample
37
42
 
38
- next if try_again?(participant, recipient)
43
+ next if try_again?(sender, recipient)
39
44
 
40
- list << { sender_number: participant[:number], recipient_name: recipient[:name] }
45
+ list << { sender_number: sender[:number], recipient_name: recipient[:name] }
41
46
 
42
- participant[:has_assignment] = true
43
- participants[index][:is_assigned] = true
47
+ sender[:has_assignment] = true
48
+ recipient[:is_assigned] = true
44
49
  end
45
50
 
46
- break if done?(participants)
51
+ break if done?
47
52
  end
48
53
  end
49
54
  end
50
55
 
51
- def try_again?(sender, recipient)
52
- disallowed = !sender[:disallow].nil? && sender[:disallow].include?(recipient[:name])
56
+ def done?
57
+ @participants.all? { |p| p[:has_assignment] && p[:is_assigned] }
58
+ end
53
59
 
54
- return true if disallowed || sender[:has_assignment] || recipient[:is_assigned]
60
+ def impossible?
61
+ @participants.one? || invalid_hash?
55
62
  end
56
63
 
57
- def done?(participants)
58
- participants.all? { |p| p[:has_assignment] && p[:is_assigned] }
64
+ def try_again?(sender, recipient)
65
+ sender[:has_assignment] || recipient[:is_assigned] || sender[:number] == recipient[:number] ||
66
+ !sender[:disallow].nil? && sender[:disallow].include?(recipient[:name])
59
67
  end
60
68
 
61
- def help_text
69
+ def self.help_text
62
70
  <<~HEREDOC
63
71
  SecretSanta, version #{VERSION}
64
72
 
@@ -72,4 +80,24 @@ module SecretSanta
72
80
 
73
81
  HEREDOC
74
82
  end
83
+
84
+ def self.error_message
85
+ <<~HEREDOC
86
+ Looks like something is wrong with your participants file. Make sure that it:
87
+
88
+ 1. Follows the format specified in README.md
89
+ 2. Contains enough participants so that everyone can be matched
90
+ 3. Doesn't have too many blacklisted participants (see "disallow" array in README.md)
91
+ HEREDOC
92
+ end
93
+
94
+ private
95
+
96
+ def invalid_hash?
97
+ @participants.each do |participant|
98
+ return true unless %i[name number].all? { |k| participant.key? k }
99
+ end
100
+
101
+ false
102
+ end
75
103
  end
data/secretsanta.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  lib = File.expand_path('lib', __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require 'secretsanta/version'
5
+ require 'secretsanta'
6
6
 
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = 'secretsanta'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: secretsanta
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ty-Lucas Kelley
@@ -143,7 +143,6 @@ files:
143
143
  - bin/secretsanta
144
144
  - bin/setup
145
145
  - lib/secretsanta.rb
146
- - lib/secretsanta/version.rb
147
146
  - secretsanta.gemspec
148
147
  homepage: https://github.com/tylucaskelley/secret-santa
149
148
  licenses:
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SecretSanta
4
- VERSION = '1.0.3'
5
- end