require "sredder/arg_parser" require "sredder/sredderc" require "sredder/util" require "sredder/version" require "sredder/wrike_auth" require "sredder/wrike_request" module Sredder def self.run_command(argv) args_parser = ArgParser.new args_parser.parse(argv) args_parser.validate! options = args_parser.options access = get_access_token folder_ids = get_folder(access, options[:folder]) task_id = create_task(access, options[:title], options[:message], folder_ids) create_timelog(access, options[:date], task_id, options[:hours]) end # gets an authenticated OAuth access token by loading the # users .sredderc or prompting them to authenticate in their # browser def self.get_access_token auth = WrikeAuth.new auth.run_oauth_procedure unless auth.authorized? auth.oauth_access_token end # Fetches the ID of a wrike folder by name. Currently this is # limited to subfolders of "SRED Time Tracking/" def self.get_folder(access, folder) request = WrikeRequest.new(access) request.post('/api/json/v2/wrike.folder.tree', {}) regex = Regexp.new(Regexp.escape("SRED Time Tracking/#{folder}")) if request.success? request.json["foldersTree"]["folders"].each_with_object([]) do |folder, arr| arr << folder['id'].to_i if folder['fullPath'] =~ regex end end end # Create a Wrike task and return its ID def self.create_task(access, title, description, parent_folder_ids) request = WrikeRequest.new(access) request.post('/api/json/v2/wrike.task.add', { title: title, description: description, status: 1, parents: parent_folder_ids }) request.json["task"]["id"] if request.success? end # Attaches a timelog entry to a Wrike task def self.create_timelog(access, date, task_id, hours, comment='') request = WrikeRequest.new(access) request.post('/api/json/v2/wrike.timelog.add', { date: date, taskId: task_id, hours: hours, comment: comment }) end # def self.run_oauth # auth = WrikeAuth.new # auth.run_oauth_procedure unless auth.authorized? # parents = self.get_folder(auth.oauth_access_token) # req = WrikeRequest.new(auth.oauth_access_token) # puts req.post('/api/json/v2/wrike.task.add', {title: 'ermahgerd ruby', description: 'blah blah blah', parents: parents}).body # req2 = WrikeRequest.new(auth.oauth_access_token) # puts req2.post('/api/json/v2/wrike.timelog.add', { # date: Util.time_stamp, # taskId: req.json["task"]["id"], # hours: 3.5, # comment: 'lolz' # }) # end end