Skip to content

Instantly share code, notes, and snippets.

@Pcushing
Created June 24, 2012 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pcushing/2982298 to your computer and use it in GitHub Desktop.
Save Pcushing/2982298 to your computer and use it in GitHub Desktop.
Todo app that writes to a google spreadsheet... this is a work in progress
require 'rubygems'
require 'google_drive'
######################################################################
# Receive some input (not done or next) and update the task object -- this should be in /lib on its own
######################################################################
class Task
attr_accessor :description, :created_at, :created_by, :status, :completed_at
def initialize(description)
@description = description
@created_at = Time.new.to_s
@created_by = "anonymous"
@status = "not complete"
@completed_at = ""
end
def in_array
[@description, @created_at, @created_by, @status, @completed_at]
end
# def complete
# @completed_at = Time.new.to_s
# @status = "completed!"
# end
end
######################################################################
# Receive done or next, update the todo list object -- this should be in /lib on its own
######################################################################
class Todo
def initialize(todo_array)
@tasks = []
#This is messy and likely shouldn't be in initialize, but let's just get it to work.
num_cells = todo_array.length
num_columns = 5
(0..num_cells-1).each do |cell|
if ((cell+1) % num_columns) == 0 #&& (cell != (num_columns-1)) This is currently creates the headers as tasks, too. Need to clean this by removing commented an dealing with headers separately
task = Task.new(todo_array[cell-4])
task.created_at = todo_array[cell-3]
task.created_by = todo_array[cell-2]
task.status = todo_array[cell-1]
task.completed_at = todo_array[cell]
# ary = [] From robert as a better way of doing the above
# 4.downto(0) { |i| ary << todo_array[cell-i] }
@tasks << task.in_array
end
end
# puts @tasks.inspect
end
def add(description)
task = Task.new(description)
@tasks << task.in_array
end
# def list(order_by)
# if order_by == "completion time"
# puts @tasks.sort_by!{ |task| task.completion_time }.reverse!
# @tasks.each { |task| puts task.in_words }
# else
# @tasks.sort_by!{ |task| task.creation_time }.reverse!
# @tasks.each { |task| puts task.in_words }
# end
# end
#
# def delete(line_number)
# @tasks.delete_at(line_number.to_i - 1)
# write_to_file(@tasks)
# end
#
# def complete(line_number)
# @tasks[line_number.to_i - 1].complete
# write_to_file(@tasks)
# end
#
# # Should this be private? It's kind of dangerous.
# def write_to_file(task_objects_array)
# words = []
# task_objects_array.each do |line|
# words << line.in_words
# end
# File.open(@filename, "w").puts(words)
# end
end
######################################################################
# Create a new googe client object to login, write to file -- this should be in /lib on its own
######################################################################
class GoogleSession
def initialize
@consumer_key = 'AIzaSyDsgIpNfwL_9HX2s847Rr6lnfDEhXZYHBE'
@client_secret = 'rY-AeYFxixiqCeRltFUOLzUZ'
@client_id = '945885806701.apps.googleusercontent.com'
log_in
end
def client
client ||= OAuth2::Client.new(
@client_id, @client_secret,
:site => "https://accounts.google.com",
:token_url => "/o/oauth2/token",
:authorize_url => "/o/oauth2/auth")
end
def log_in
# client = OAuth2::Client.new(
# @client_id, @client_secret,
# :site => "https://accounts.google.com",
# :token_url => "/o/oauth2/token",
# :authorize_url => "/o/oauth2/auth")
#
auth_url = client.auth_code.authorize_url(
:redirect_uri => "urn:ietf:wg:oauth:2.0:oob",
:scope =>
"https://docs.google.com/feeds/ " +
"https://docs.googleusercontent.com/ " +
"https://spreadsheets.google.com/feeds/")
# how can we automate this ish by hitting auth_url and parsing the output? It kind of sucks that we have to do this manual step.
# If we can maintain the session, saving the auth_url and authorization_code, we can just do it for one user? Then it's just a one-time step
puts "We've got ourselves an auth_url #{ auth_url } so why don't you go get
us a PIN:"
authorization_code = STDIN.readline.chomp
auth_token = client.auth_code.get_token(
authorization_code,
:redirect_uri => "urn:ietf:wg:oauth:2.0:oob")
@session = GoogleDrive.login_with_oauth(auth_token)
end
def write_to_file(todo_list)
(0..(todo_list.length-1)).each do |row|
(0..(todo_list[0].length-1)).each do |column|
@ws[row + 1,column + 1] = todo_list[row][column]
end
end
@ws.save()
end
def read_from_file
# Here's our windup list spreadsheet https://docs.google.com/spreadsheet/ccc?key=0AnKNM54CEzCZdEJtMmF3a0VNOUdybVlDdDlmUzFFclE
@ws = @session.spreadsheet_by_key("0AnKNM54CEzCZdEJtMmF3a0VNOUdybVlDdDlmUzFFclE").worksheets[0]
todo_array = []
for row in 1..@ws.num_rows
for col in 1..@ws.num_cols
todo_array << @ws[row, col]
end
end
todo_array
end
end
######################################################################
# Parse the command line inputs -- this should be in /bin
######################################################################
if __FILE__ == $0
command = ARGV[0]
command_arg = ARGV[1]
# if command == "-r"
# new_session = GoogleSession.new
# new_session.read_from_file
if command == "-w" && !command_arg.nil?
new_session = GoogleSession.new
todo = Todo.new(new_session.read_from_file)
new_session.write_to_file(todo.add(command_arg))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment