require 'sinatra'
require 'net/http'
require 'redis'

set :server, 'thin'
base_url = ''
redis = Redis.new(:url => ENV['REDISTOGO_URL'])

get '/' do

    logger.info "got request"

    if params.key?('username')
        logger.info "adding user to redis"
        redis.sadd('people', params['username'])

        # never mind this coffee session if nobody responds in time
        redis.expire('people', 60)

    end

    # return name or count depending on card
    headcount = redis.scard('people')
    subject =
        if headcount < 2
            logger.info "personal subject"
            redis.srandmember('people')
        else
            logger.info "cardinal subject"
            headcount.to_s + "people"
        end

    text = "#{subject} would like coffee!"
    logger.info text

    # do a YoAll
    status = Net::HTTP.post_form(URI.parse(
        'https://api.justyo.co/yoall/'),
        {
            'api_token' => ENV['YO_KEY'],
            'link' => "http://yotext.co/show/?text=" + text,
        }
    )

    # terse status response
    if status
        logger.info 'success'
        'success'
    else
        logger.info 'failed to YoAll'
        'failed to YoAll'
    end

end

require 'sinatra'
require 'net/http'
require 'redis'

set :server, 'thin'
base_url = ''
redis = Redis.new(:url => ENV['REDISTOGO_URL'])

get '/' do

    logger.info "got request"

    if params.key?('username')
        logger.info "adding user to redis"
        redis.sadd('people', params['username'])

        # never mind this coffee session if nobody responds in time
        redis.expire('people', 60)

    end

    # return name or count depending on card
    headcount = redis.scard('people')
    subject =
        if headcount < 2
            redis.srandmember('people')
        elsif headcount < 3
            redis.srandmember('people', 2).join(' and ')
        else
            logger.info "cardinal subject"
            "#{headcount} people"
        end

    text = "#{subject} would like coffee!"
    logger.info text

    # do a YoAll
    status = Net::HTTP.post_form(URI.parse(
        'https://api.justyo.co/yoall/'),
        {
            'api_token' => ENV['YO_KEY'],
            'link' => "http://yotext.co/show/?text=" + text,
        }
    )

    # terse status response
    if status
        logger.info 'success'
        'success'
    else
        logger.info 'failed to YoAll'
        'failed to YoAll'
    end

end

Support personal output for 2 people
Support personal output for 2 people

Now, when there are two people, you get a response like 'Bob and Alice'
instead of '2 people'.  It'd be nice to know who you're going with if
you're a pair, so 3 is the limit for cardinal output.
