Class: SlimTimer

Inherits:
Object
  • Object
show all
Defined in:
lib/slimtimer4r.rb

Defined Under Namespace

Classes: Record

Constant Summary collapse

VERSION =
'0.2.4'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, password, api_key, connection_timeout = 60) ⇒ SlimTimer

Creates a new SlimTimer object and obtains the access_token and user_id from the SlimTimer API by sending your email, password, and api_key. Raises a RuntimeError if it can’t authenticate. You can also optionally supply a connection_timeout for the SlimTimer servers (the default is 60 seconds).

slim_timer = SlimTimer.new("[email protected]", "password", "12345")
  => #<SlimTimer:0x68bca8 @password="password"...>

slim_timer = SlimTimer.new("[email protected]", "badpassword", "12345")
  => RuntimeError: Error occurred (500)


87
88
89
90
91
# File 'lib/slimtimer4r.rb', line 87

def initialize(email, password, api_key, connection_timeout=60)
  @email, @password, @api_key = email, password, api_key
  connect(connection_timeout)
  get_token
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



78
79
80
# File 'lib/slimtimer4r.rb', line 78

def access_token
  @access_token
end

#api_keyObject

Returns the value of attribute api_key.



78
79
80
# File 'lib/slimtimer4r.rb', line 78

def api_key
  @api_key
end

#emailObject

Returns the value of attribute email.



78
79
80
# File 'lib/slimtimer4r.rb', line 78

def email
  @email
end

#passwordObject

Returns the value of attribute password.



78
79
80
# File 'lib/slimtimer4r.rb', line 78

def password
  @password
end

#request=(value) ⇒ Object

Sets the attribute request

Parameters:

  • value

    the value to set the attribute request to.



78
79
80
# File 'lib/slimtimer4r.rb', line 78

def request=(value)
  @request = value
end

#user_idObject

Returns the value of attribute user_id.



78
79
80
# File 'lib/slimtimer4r.rb', line 78

def user_id
  @user_id
end

Instance Method Details

#create_task(name, tags = nil, coworker_emails = nil, reporter_emails = nil, completed_on = nil) ⇒ Object



114
115
116
# File 'lib/slimtimer4r.rb', line 114

def create_task(name, tags=nil, coworker_emails=nil, reporter_emails=nil, completed_on=nil)
  request("post", "#{@user_id}/tasks", {"access_token" => @access_token, "api_key" => @api_key, "task" => {"name" => name, "tags" => tags, "coworker_emails" => coworker_emails, "reporter_emails" => reporter_emails, "completed_on" => completed_on}}, "Task")
end

#create_timeentry(start_time, duration_in_seconds, task_id, end_time, tags = nil, comments = nil, in_progress = nil) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/slimtimer4r.rb', line 168

def create_timeentry(start_time, duration_in_seconds, task_id, end_time, tags=nil, comments=nil, in_progress=nil)
  start_time = start_time.strftime("%Y-%m-%dT%H:%M:%SZ")
  end_time = end_time.strftime("%Y-%m-%dT%H:%M:%SZ") unless end_time.nil?

  # add the default params
  params = {
    "start_time" => start_time,
    "duration_in_seconds" => duration_in_seconds,
    "task_id" => task_id,
    "end_time" => end_time,
  }

  # only add the applicable params
  params.merge!({"tags" => tags}) unless tags.nil?
  params.merge!({"comments" => comments}) unless comments.nil?
  params.merge!({"in_progress" => in_progress}) unless in_progress.nil?


  request("post", "#{@user_id}/time_entries", {"access_token" => @access_token, "api_key" => @api_key, "time_entry" => params}, "TimeEntry")
end

#delete_task(task_id) ⇒ Object



110
111
112
# File 'lib/slimtimer4r.rb', line 110

def delete_task(task_id)
  request("delete", "#{@user_id}/tasks/#{task_id}?api_key=#{@api_key}&access_token=#{@access_token}", "Task")
end

#delete_timeentry(timeentry_id) ⇒ Object



193
194
195
# File 'lib/slimtimer4r.rb', line 193

def delete_timeentry(timeentry_id)
  request("delete", "#{@user_id}/time_entries/#{timeentry_id}?api_key=#{@api_key}&access_token=#{@access_token}", "TimeEntry")
end

#list_tasks(show_completed = "yes", role = "owner,coworker") ⇒ Object

Returns an array of Record objects, each representing a task. Returns an empty array if nothing is found.

Options:

show_completed

Include completed tasks. Specify only to only include the completed tasks. Valid options are yes, no, and only. Default is yes.

role

Include tasks where the user’s role is one of the roles given (comma delimited). Valid options include owner, coworker, and reporter. Default is owner,coworker.



98
99
100
# File 'lib/slimtimer4r.rb', line 98

def list_tasks(show_completed="yes", role="owner,coworker")
  request("get", "#{@user_id}/tasks?api_key=#{@api_key}&access_token=#{@access_token}&show_completed=#{show_completed}&role=#{role}", "Tasks")
end

#list_timeentries(range_start = nil, range_end = nil) ⇒ Object

Returns an array of Record objects, each representing a time entry. Returns an empty array if nothing is found.

Options:

range_start

Only include entries after this time. Takes either a Date or Time object as a parameter. If a Date object is used, it will append a time of 00:00:00 to the request. Default is nil, meaning there is no start range.

range_end

Only include entries before this time. Takes either a Date or Time object as a parameter. If a Date object is used, it will append a time of 23:59:59 to the request. Default is nil, meaning there is no end range.



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/slimtimer4r.rb', line 133

def list_timeentries(range_start=nil, range_end=nil)
  if range_start.is_a?(Date)
    range_start = range_start.strftime("%Y-%m-%dT00:00:00Z")
  else
    range_start = range_start.strftime("%Y-%m-%dT%H:%M:%SZ") unless range_start.nil?
  end
  if range_end.is_a?(Date)
    range_end = range_end.strftime("%Y-%m-%dT23:59:59Z")
  else
    range_end = range_end.strftime("%Y-%m-%dT%H:%M:%SZ") unless range_end.nil?
  end
  request("get", "#{@user_id}/time_entries?api_key=#{@api_key}&access_token=#{@access_token}&range_start=#{range_start}&range_end=#{range_end}", "TimeEntries")
end

#show_task(task_id) ⇒ Object

Returns a specific task as a Record object. Returns nil if the record is not found.

Options:

task_id

The id of the task you would like to retrieve.



106
107
108
# File 'lib/slimtimer4r.rb', line 106

def show_task(task_id)
  request("get", "#{@user_id}/tasks/#{task_id}?api_key=#{@api_key}&access_token=#{@access_token}", "Task")
end

#show_timeentry(timeentry_id) ⇒ Object



189
190
191
# File 'lib/slimtimer4r.rb', line 189

def show_timeentry(timeentry_id)
  request("get", "#{@user_id}/time_entries/#{timeentry_id}?api_key=#{@api_key}&access_token=#{@access_token}", "TimeEntry")
end

#update_task(task_id, name, tags = nil, coworker_emails = nil, reporter_emails = nil, completed_on = nil) ⇒ Object



118
119
120
# File 'lib/slimtimer4r.rb', line 118

def update_task(task_id, name, tags=nil, coworker_emails=nil, reporter_emails=nil, completed_on=nil)
  request("put", "#{@user_id}/tasks/#{task_id}", {"access_token" => @access_token, "api_key" => @api_key, "task" => {"name" => name, "tags" => tags, "coworker_emails" => coworker_emails, "reporter_emails" => reporter_emails, "completed_on" => completed_on}}, "Task")
end

#update_timeentry(timeentry_id, start_time, duration_in_seconds, task_id, end_time, tags = nil, comments = nil, in_progress = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/slimtimer4r.rb', line 147

def update_timeentry(timeentry_id, start_time, duration_in_seconds, task_id, end_time, tags=nil, comments=nil, in_progress=nil)
  start_time = start_time.strftime("%Y-%m-%dT%H:%M:%SZ")
  end_time = end_time.strftime("%Y-%m-%dT%H:%M:%SZ") unless end_time.nil?

  # add the default params
  params = {
    "start_time" => start_time,
    "duration_in_seconds" => duration_in_seconds,
    "task_id" => task_id,
    "end_time" => end_time,
  }

  # only add the applicable params
  params.merge!({"tags" => tags}) unless tags.nil?
  params.merge!({"comments" => comments}) unless comments.nil?
  params.merge!({"in_progress" => in_progress}) unless in_progress.nil?


  request("put", "#{@user_id}/time_entries/#{timeentry_id}", {"access_token" => @access_token, "api_key" => @api_key, "time_entry" => params}, "TimeEntry")
end