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.



14
15
16
17
18
19
20
# File 'lib/toggl.rb', line 14

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
  auth
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



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

def api_token
  @api_token
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#authObject



22
23
24
25
# File 'lib/toggl.rb', line 22

def auth
  session_response = get "sessions"
  self.class.default_cookies.add_cookies(session_response.headers["set-cookie"][0])
end

#clientsObject



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

def clients
  get 'clients'
end

#create_client(params = {}) ⇒ Object



96
97
98
# File 'lib/toggl.rb', line 96

def create_client(params={})
  post "clients", MultiJson.encode({:client => params})
end

#create_project(params = {}) ⇒ Object



112
113
114
# File 'lib/toggl.rb', line 112

def create_project(params={})
  post "projects", MultiJson.encode({:project => params})
end

#create_project_user(params = {}) ⇒ Object



120
121
122
# File 'lib/toggl.rb', line 120

def create_project_user(params={})
  post "project_users", MultiJson.encode({:project_user => params})
end

#create_task(params = {}) ⇒ Object



128
129
130
# File 'lib/toggl.rb', line 128

def create_task(params={})
  post "tasks", MultiJson.encode({:task => params})
end

#create_time_entry(params = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/toggl.rb', line 70

def create_time_entry(params={})
  workspace   = params[:workspace] || default_workspace_id
  project_id  = find_project_id(params[:project]) || create_project(params, workspace)
  params[:billable] = true
  params[:start] = Time.now if params[:start].nil?
  params[:start] = params[:start].iso8601
  params.merge!({ :created_with => name,
                  :workspace => {:id => workspace},
                  :project => {:id => project_id},
                  :tag_names => [name]})

  post 'time_entries', MultiJson.encode({:time_entry => params})
end

#default_workspace_idObject



27
28
29
# File 'lib/toggl.rb', line 27

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

#delete_client(id) ⇒ Object



104
105
106
# File 'lib/toggl.rb', line 104

def delete_client(id)
  delete "clients/#{id}"
end

#delete_task(id) ⇒ Object



136
137
138
# File 'lib/toggl.rb', line 136

def delete_task(id)
  delete "tasks/#{id}"
end

#delete_time_entry(id) ⇒ Object



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

def delete_time_entry(id)
  self.class.delete("/api/v6/time_entries/#{id}.json", :basic_auth => basic_auth)
end

#duration(str) ⇒ Object



37
38
39
# File 'lib/toggl.rb', line 37

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

#find_project_id(str) ⇒ Object



31
32
33
34
35
# File 'lib/toggl.rb', line 31

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

#get_time_entry(id) ⇒ Object



66
67
68
# File 'lib/toggl.rb', line 66

def get_time_entry(id)
  get "time_entries/#{id}"
end

#projectsObject



108
109
110
# File 'lib/toggl.rb', line 108

def projects
  get 'projects'
end

#start(value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/toggl.rb', line 41

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

#tagsObject



140
141
142
# File 'lib/toggl.rb', line 140

def tags
  get 'tags'
end

#tasksObject



124
125
126
# File 'lib/toggl.rb', line 124

def tasks
  get 'tasks'
end

#time_entries(params = {}) ⇒ Object



62
63
64
# File 'lib/toggl.rb', line 62

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

#update_client(params = {}) ⇒ Object



100
101
102
# File 'lib/toggl.rb', line 100

def update_client(params={})
  put "clients/#{params['id']}", MultiJson.encode({:client => params})
end

#update_project(params = {}) ⇒ Object



116
117
118
# File 'lib/toggl.rb', line 116

def update_project(params={})
  put "projects/#{params['id']}", MultiJson.encode({:project => params})
end

#update_task(params = {}) ⇒ Object



132
133
134
# File 'lib/toggl.rb', line 132

def update_task(params={})
  put "tasks/#{params['id']}", MultiJson.encode({:task => params})
end

#update_time_entry(params = {}) ⇒ Object



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

def update_time_entry(params={})
  put "time_entries/#{params['id']}", MultiJson.encode({:time_entry => params})
end

#workspacesObject



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

def workspaces
  get 'workspaces'
end