Skip to content

Instantly share code, notes, and snippets.

@noahc
Created September 6, 2011 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save noahc/1198881 to your computer and use it in GitHub Desktop.
Save noahc/1198881 to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'date'
class Doer
def makeDate(date)
return Date.strptime(date, "%m/%d/%Y")
end
def buildArray(dateObj)
array = frontpadding(dateObj)
month = dateObj.month
newMonth = month
day = dateObj.mday
date = dateObj
while month == newMonth
temp_array = [day,'day']
array.push(temp_array)
day = day + 1
date = date + 1
newMonth = date.month
end
endpadding(date, array)
array
end
def endpadding(dateObj, array)
dofw = dateObj.wday
filler = (dofw - 6).abs + 1
until filler == 0
temp_array = ['','day']
array.push(temp_array)
filler = filler - 1
end
end
def frontpadding(dateObj)
array = Array.new
dofw = dateObj.wday
day = dateObj.mday
filler = dofw
if day != 1
day += 1
until day == 0
temp_array = ['','day']
array.unshift(temp_array)
day = day - 1
end
end
until filler == 0
temp_array = ['','day']
array.unshift(temp_array)
filler = filler - 1
end
array
end
def makeCal(dateObj)
@cal = Array.new
months = 0
while months < 12
# #pass dateobj to build array
array = buildArray(dateObj)
# #save array to hash with month key
monthName = Date::MONTHNAMES[dateObj.mon]
holder = Array.new
holder.push monthName
holder.push array
@cal.push holder
# #create new date object using month and set it to the first
dateObj = Date.new(dateObj.year, dateObj.month)
dateObj >>= 1
months = months + 1
end
@cal = createCal(@cal, @on, @off)
@cal
end
def createCal(cal, on, off)
mod = on + off
# @daycount = 0
# cal.each do |month|
# month[1].each do |day|
# if day[0] == ''
# # do nothing
# else
# if @daycount % mod <= @on
# day[1] = 'dayOn'
# @daycount = @daycount + 1
# end
# end
# end
# end
cal
end
end
get '/' do
haml :ask
end
post '/calendar' do
@on = params["on"]
@off = params["off"]
@date = params["date"]
a = Doer.new
@var = a.makeDate(@date)
@on = @on.to_i
@off = @off.to_i
@ooo = @on + @off
@cal = a.makeCal(@var)
haml :feeling
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment