class PairingShuffler::Shuffler

Constants

DAY

Public Class Methods

new(config) click to toggle source
# File lib/pairing_shuffler.rb, line 13
def initialize(config)
  @config = config
end

Public Instance Methods

send_emails() click to toggle source
# File lib/pairing_shuffler.rb, line 17
def send_emails
  Mailer.new(@config).session do |mailer|
    list.each do |emails|
      mailer.notify(emails)
    end
  end
end

Private Instance Methods

access_token() click to toggle source
# File lib/pairing_shuffler.rb, line 40
def access_token
  params = {
    :client_id => @config.fetch(:client_id),
    :client_secret => @config.fetch(:client_secret),
    :refresh_token => @config.fetch(:refresh_token),
    :grant_type => "refresh_token"
  }.map { |k, v| "-d '#{k}=#{v}'" }.join(" ")
  response = `curl https://accounts.google.com/o/oauth2/token --silent -X POST #{params}`
  raise "FAILED: #{response}" unless $?.success?
  JSON.load(response).fetch("access_token")
end
content() click to toggle source
# File lib/pairing_shuffler.rb, line 27
def content
  @content ||= begin
    session = GoogleDrive.login_with_oauth(access_token)
    cells = session.spreadsheet_by_key(@config[:doc]).worksheets[0].cells
    data = []
    cells.each do |(row, column), value|
      data[row - 1] ||= []
      data[row - 1][column - 1] = value
    end
    data.compact
  end
end
list() click to toggle source
# File lib/pairing_shuffler.rb, line 52
def list
  emails = content.compact.select { |row| row.first.include?("@") && present?(row) }.map(&:first)
  emails.sort_by{ rand }.each_slice(2).to_a.reject { |group| group.size == 1 }
end
parse_time(time) click to toggle source
# File lib/pairing_shuffler.rb, line 63
def parse_time(time)
  return unless date = time.to_s[/\d+\/\d+\/\d+/]
  Time.strptime(date, "%m/%d/%Y")
end
present?(row) click to toggle source
# File lib/pairing_shuffler.rb, line 57
def present?(row)
  return true unless away_until = content.map { |row| row.index("Away until") }.compact.first
  return true unless time = parse_time(row[away_until])
  time + DAY < Time.now
end