Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added option to destroy a task

  • Loading branch information...
commit 2e4dd2f980ac736a149d8ca964124ea511162df1 1 parent 59b40fd
@atog authored
View
60 lib/toggl.rb
@@ -6,55 +6,59 @@ class Toggl
include HTTParty
base_uri "https://toggl.com"
format :json
-
+
attr_reader :name, :api_token
-
+
def initialize(token, name="toggl-gem", debug=false)
self.class.default_params :output => 'json'
@api_token = token
@name = name
self.class.debug_output if debug
end
-
+
+ def delete_task(task_id)
+ delete 'tasks', task_id
+ end
+
def create_task(params={})
workspace = params[:workspace] || default_workspace_id
project_id = find_project_id(params[:project]) || create_project(params, workspace)
params[:billable] = true
-
- params.merge!({ :created_with => name,
- :workspace => {:id => workspace},
- :project => {:id => project_id},
- :tag_names => [name],
- :start => start(params[:start]),
+
+ params.merge!({ :created_with => name,
+ :workspace => {:id => workspace},
+ :project => {:id => project_id},
+ :tag_names => [name],
+ :start => start(params[:start]),
:duration => duration(params[:duration])})
-
+
post 'tasks', {:task => params}
end
-
+
def create_project(params={}, workspace=nil)
workspace ||= default_workspace_id
- if project = post("projects",
- :project => {:name => params[:project],
- :workspace => {:id => workspace},
+ if project = post("projects",
+ :project => {:name => params[:project],
+ :workspace => {:id => workspace},
:billable => (params[:billable] || true)})
project["id"]
end
end
-
+
def default_workspace_id
self.workspaces.first["id"]
end
-
+
def find_project_id(str)
if project = self.projects.find{|project| project["client_project_name"].downcase =~ /#{str}/}
project["id"]
end
end
-
+
def duration(str)
str ? ChronicDuration.parse(str) : 1800
end
-
+
def start(value)
if value
case value
@@ -68,31 +72,35 @@ def start(value)
DateTime.now
end
end
-
+
def workspaces
get 'workspaces'
end
-
+
def tasks(params={})
get 'tasks', params
end
-
+
def projects
get 'projects'
end
-
+
private
-
+
def get(resource_name, data={})
self.class.get("/api/v1/#{resource_name}.json", :basic_auth => basic_auth, :query => data)
end
-
+
def post(resource_name, data)
self.class.post("/api/v1/#{resource_name}.json", :body => data, :basic_auth => basic_auth)
end
-
+
+ def delete(resource_name, id)
+ self.class.delete("/api/v1/#{resource_name}/#{id}.json", :basic_auth => basic_auth)
+ end
+
def basic_auth
{:username => self.api_token, :password => "api_token"}
end
-
+
end
View
4 lib/toggl_cmd/runner.rb
@@ -16,6 +16,10 @@ def self.toggl(args)
prettify_tasks(Toggl.new(token, NAME).tasks)
elsif options[:projects]
prettify_projects(Toggl.new(token, NAME).projects)
+ elsif options[:delete]
+ toggl = Toggl.new(token, NAME)
+ toggl.delete_task(options[:delete])
+ prettify_tasks(toggl.tasks)
elsif options.any?
prettify_tasks(Toggl.new(token, NAME, options.delete(:debug)).create_task(options))
else
View
18 lib/toggl_cmd/runner_options.rb
@@ -1,5 +1,5 @@
module TogglCmd
-
+
class RunnerOptions < Hash
attr_reader :opts
@@ -24,7 +24,7 @@ def initialize(args)
o.on('-d', '--date DATE', 'When exactly did it happen?') do |date|
self[:start] = date
end
-
+
o.on('--tasks', 'Show tasks') do |tasks|
self[:tasks] = tasks
end
@@ -32,15 +32,19 @@ def initialize(args)
o.on('--projects', 'Show projects') do |projects|
self[:projects] = projects
end
-
+
+ o.on('--delete TASK_ID', 'Delete tasks with id') do |task_id|
+ self[:delete] = task_id
+ end
+
o.on('-v', '--verbose', 'What\'s happening?') do |debug|
self[:debug] = debug
end
-
+
o.on_tail('-h', '--help', 'Display this help and exit') do
puts @opts
exit
- end
+ end
end
@@ -50,6 +54,6 @@ def initialize(args)
self[:invalid_argument] = e.message
end
end
-
+
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.