Class: Toggl

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/toggl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, name = "toggl-gem", debug = false) ⇒ Toggl

Returns a new instance of Toggl.



12
13
14
15
16
17
# File 'lib/toggl.rb', line 12

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

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



10
11
12
# File 'lib/toggl.rb', line 10

def api_token
  @api_token
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/toggl.rb', line 10

def name
  @name
end

Instance Method Details

#create_project(params = {}, workspace = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/toggl.rb', line 38

def create_project(params={}, workspace=nil)
  workspace ||= default_workspace_id
  if project = post("projects",
                    :project => {:name => params[:project],
                                 :workspace => {:id => workspace},
                                 :billable => (params[:billable] || true)})
     project["id"]
   end
end

#create_task(params = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/toggl.rb', line 23

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]),
                  :duration => duration(params[:duration])})

  post 'tasks', {:task => params}
end

#default_workspace_idObject



48
49
50
# File 'lib/toggl.rb', line 48

def default_workspace_id
  self.workspaces.first["id"]
end

#delete_task(task_id) ⇒ Object



19
20
21
# File 'lib/toggl.rb', line 19

def delete_task(task_id)
  delete 'tasks', task_id
end

#duration(str) ⇒ Object



58
59
60
# File 'lib/toggl.rb', line 58

def duration(str)
  str ? ChronicDuration.parse(str) : 1800
end

#find_project_id(str) ⇒ Object



52
53
54
55
56
# File 'lib/toggl.rb', line 52

def find_project_id(str)
  if project = self.projects.find{|project| project["client_project_name"].downcase =~ /#{str}/}
    project["id"]
  end
end

#projectsObject



87
88
89
# File 'lib/toggl.rb', line 87

def projects
  get 'projects'
end

#start(value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/toggl.rb', line 62

def start(value)
  if value
    case value
    when "today"
      Date.today
    when "yesterday"
      Date.today - 1
    when "tomorrow"
      Date.today + 1
    else
      DateTime.parse(value)
    end
  else
    DateTime.now
  end
end

#tasks(params = {}) ⇒ Object



83
84
85
# File 'lib/toggl.rb', line 83

def tasks(params={})
  get 'tasks', params
end

#user_dumpObject



91
92
93
# File 'lib/toggl.rb', line 91

def user_dump
  get 'me', {:with_related_data => true}
end

#workspacesObject



79
80
81
# File 'lib/toggl.rb', line 79

def workspaces
  get 'workspaces'
end