Class: Asana::Resources::Task

Inherits:
Resource
  • Object
show all
Includes:
AttachmentUploading, EventSubscription
Defined in:
lib/asana/resources/task.rb

Overview

The task is the basic object around which many operations in Asana are centered. In the Asana application, multiple tasks populate the middle pane according to some view parameters, and the set of selected tasks determines the more detailed information presented in the details pane.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventSubscription

#events

Methods included from AttachmentUploading

#attach

Methods inherited from Resource

inherited, #initialize, #method_missing, #refresh, #respond_to_missing?, #to_h, #to_s

Methods included from ResponseHelper

#parse

Constructor Details

This class inherits a constructor from Asana::Resources::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Asana::Resources::Resource

Instance Attribute Details

#assigneeObject (readonly)



17
18
19
# File 'lib/asana/resources/task.rb', line 17

def assignee
  @assignee
end

#assignee_statusObject (readonly)



19
20
21
# File 'lib/asana/resources/task.rb', line 19

def assignee_status
  @assignee_status
end

Class Method Details

.create(client, options: {}, **data) ⇒ Object

Creating a new task is as easy as POSTing to the ‘/tasks` endpoint with a data block containing the fields you’d like to set on the task. Any unspecified fields will take on default values.

Every task is required to be created in a specific workspace, and this workspace cannot be changed once set. The workspace need not be set explicitly if you specify a ‘project` or a `parent` task instead.

Parameters:

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



37
38
39
40
# File 'lib/asana/resources/task.rb', line 37

def create(client, options: {}, **data)

  self.new(parse(client.post("/tasks", body: data, options: options)).first, client: client)
end

.create_in_workspace(client, workspace: required("workspace"), options: {}, **data) ⇒ Object

Creating a new task is as easy as POSTing to the ‘/tasks` endpoint with a data block containing the fields you’d like to set on the task. Any unspecified fields will take on default values.

Every task is required to be created in a specific workspace, and this workspace cannot be changed once set. The workspace need not be set explicitly if you specify a project or a parent task instead.

Parameters:

  • workspace (Id) (defaults to: required("workspace"))

    The workspace to create a task in.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



53
54
55
56
# File 'lib/asana/resources/task.rb', line 53

def create_in_workspace(client, workspace: required("workspace"), options: {}, **data)

  self.new(parse(client.post("/workspaces/#{workspace}/tasks", body: data, options: options)).first, client: client)
end

.find_all(client, assignee: nil, workspace: nil, completed_since: nil, modified_since: nil, per_page: 20, options: {}) ⇒ Object

Returns the compact task records for some filtered set of tasks. Use one or more of the parameters provided to filter the tasks returned.

Parameters:

  • assignee (Id) (defaults to: nil)

    The assignee to filter tasks on.

  • workspace (Id) (defaults to: nil)

    The workspace or organization to filter tasks on.

  • completed_since (String) (defaults to: nil)

    Only return tasks that are either incomplete or that have been

  • completed

    since this time.

  • modified_since (String) (defaults to: nil)

    Only return tasks that have been modified since the given time.

  • per_page (Integer) (defaults to: 20)

    the number of records to fetch per page.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • Notes:
  • If

    you specify ‘assignee`, you must also specify the `workspace` to filter on.

  • If

    you specify ‘workspace`, you must also specify the `assignee` to filter on.

  • A

    task is considered “modified” if any of its properties change,

  • or

    associations between it and other objects are modified (e.g.

  • a

    task being added to a project). A task is not considered modified

  • just

    because another object it is associated with (e.g. a subtask)

  • is

    modified. Actions that count as modifying the task include

  • assigning,

    renaming, completing, and adding stories.



112
113
114
115
# File 'lib/asana/resources/task.rb', line 112

def find_all(client, assignee: nil, workspace: nil, completed_since: nil, modified_since: nil, per_page: 20, options: {})
  params = { assignee: assignee, workspace: workspace, completed_since: completed_since, modified_since: modified_since, limit: per_page }.reject { |_,v| v.nil? || Array(v).empty? }
  Collection.new(parse(client.get("/tasks", params: params, options: options)), type: self, client: client)
end

.find_by_id(client, id, options: {}) ⇒ Object

Returns the complete task record for a single task.

Parameters:

  • id (Id)

    The task to get.

  • options (Hash) (defaults to: {})

    the request I/O options.



62
63
64
65
# File 'lib/asana/resources/task.rb', line 62

def find_by_id(client, id, options: {})

  self.new(parse(client.get("/tasks/#{id}", options: options)).first, client: client)
end

.find_by_project(client, projectId: required("projectId"), per_page: 20, options: {}) ⇒ Object

Returns the compact task records for all tasks within the given project, ordered by their priority within the project.

Parameters:

  • projectId (Id) (defaults to: required("projectId"))

    The project in which to search for tasks.

  • per_page (Integer) (defaults to: 20)

    the number of records to fetch per page.

  • options (Hash) (defaults to: {})

    the request I/O options.



73
74
75
76
# File 'lib/asana/resources/task.rb', line 73

def find_by_project(client, projectId: required("projectId"), per_page: 20, options: {})
  params = { limit: per_page }.reject { |_,v| v.nil? || Array(v).empty? }
  Collection.new(parse(client.get("/projects/#{projectId}/tasks", params: params, options: options)), type: self, client: client)
end

.find_by_tag(client, tag: required("tag"), per_page: 20, options: {}) ⇒ Object

Returns the compact task records for all tasks with the given tag.

Parameters:

  • tag (Id) (defaults to: required("tag"))

    The tag in which to search for tasks.

  • per_page (Integer) (defaults to: 20)

    the number of records to fetch per page.

  • options (Hash) (defaults to: {})

    the request I/O options.



83
84
85
86
# File 'lib/asana/resources/task.rb', line 83

def find_by_tag(client, tag: required("tag"), per_page: 20, options: {})
  params = { limit: per_page }.reject { |_,v| v.nil? || Array(v).empty? }
  Collection.new(parse(client.get("/tags/#{tag}/tasks", params: params, options: options)), type: self, client: client)
end

.plural_nameObject

Returns the plural name of the resource.



23
24
25
# File 'lib/asana/resources/task.rb', line 23

def plural_name
  'tasks'
end

Instance Method Details

#add_comment(text: required("text"), options: {}, **data) ⇒ Object

Adds a comment to a task. The comment will be authored by the currently authenticated user, and timestamped when the server receives the request.

Parameters:

  • Returns

    the full record for the new story added to the task.

  • text (String) (defaults to: required("text"))

    The plain text of the comment to add.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



293
294
295
296
# File 'lib/asana/resources/task.rb', line 293

def add_comment(text: required("text"), options: {}, **data)
  with_params = data.merge(text: text).reject { |_,v| v.nil? || Array(v).empty? }
  Story.new(parse(client.post("/tasks/#{id}/stories", body: with_params, options: options)).first, client: client)
end

#add_followers(followers: required("followers"), options: {}, **data) ⇒ Object

Adds each of the specified followers to the task, if they are not already following. Returns the complete, updated record for the affected task.

Parameters:

  • followers (Array) (defaults to: required("followers"))

    An array of followers to add to the task.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



152
153
154
155
# File 'lib/asana/resources/task.rb', line 152

def add_followers(followers: required("followers"), options: {}, **data)
  with_params = data.merge(followers: followers).reject { |_,v| v.nil? || Array(v).empty? }
  refresh_with(parse(client.post("/tasks/#{id}/addFollowers", body: with_params, options: options)).first)
end

#add_project(project: required("project"), insertAfter: nil, insertBefore: nil, section: nil, options: {}, **data) ⇒ Object

Adds the task to the specified project, in the optional location specified. If no location arguments are given, the task will be added to the beginning of the project.

‘addProject` can also be used to reorder a task within a project that already contains it.

Parameters:

  • Returns

    an empty data block.

  • project (Id) (defaults to: required("project"))

    The project to add the task to.

  • insertAfter (Id) (defaults to: nil)

    A task in the project to insert the task after, or ‘null` to

  • insert

    at the beginning of the list.

  • insertBefore (Id) (defaults to: nil)

    A task in the project to insert the task before, or ‘null` to

  • insert

    at the end of the list.

  • section (Id) (defaults to: nil)

    A section in the project to insert the task into. The task will be

  • inserted

    at the top of the section.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



198
199
200
201
# File 'lib/asana/resources/task.rb', line 198

def add_project(project: required("project"), insertAfter: nil, insertBefore: nil, section: nil, options: {}, **data)
  with_params = data.merge(project: project, insertAfter: insertAfter, insertBefore: insertBefore, section: section).reject { |_,v| v.nil? || Array(v).empty? }
  client.post("/tasks/#{id}/addProject", body: with_params, options: options) && true
end

#add_subtask(subtask: required("subtask"), options: {}, **data) ⇒ Object

Makes an existing task a subtask of another. Returns an empty data block.

Parameters:

  • subtask (Id) (defaults to: required("subtask"))

    The subtask to add to the task.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



259
260
261
262
# File 'lib/asana/resources/task.rb', line 259

def add_subtask(subtask: required("subtask"), options: {}, **data)
  with_params = data.merge(subtask: subtask).reject { |_,v| v.nil? || Array(v).empty? }
  client.post("/tasks/#{id}/subtasks", body: with_params, options: options) && true
end

#add_tag(tag: required("tag"), options: {}, **data) ⇒ Object

Adds a tag to a task. Returns an empty data block.

Parameters:

  • tag (Id) (defaults to: required("tag"))

    The tag to add to the task.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



230
231
232
233
# File 'lib/asana/resources/task.rb', line 230

def add_tag(tag: required("tag"), options: {}, **data)
  with_params = data.merge(tag: tag).reject { |_,v| v.nil? || Array(v).empty? }
  client.post("/tasks/#{id}/addTag", body: with_params, options: options) && true
end

#deleteObject

A specific, existing task can be deleted by making a DELETE request on the URL for that task. Deleted tasks go into the “trash” of the user making the delete request. Tasks can be recovered from the trash within a period of 30 days; afterward they are completely removed from the system.

Returns:

  • an empty data record.



141
142
143
144
# File 'lib/asana/resources/task.rb', line 141

def delete()

  client.delete("/tasks/#{id}") && true
end

#projects(per_page: 20, options: {}) ⇒ Object

Returns a compact representation of all of the projects the task is in.

Parameters:

  • per_page (Integer) (defaults to: 20)

    the number of records to fetch per page.

  • options (Hash) (defaults to: {})

    the request I/O options.



172
173
174
175
# File 'lib/asana/resources/task.rb', line 172

def projects(per_page: 20, options: {})
  params = { limit: per_page }.reject { |_,v| v.nil? || Array(v).empty? }
  Collection.new(parse(client.get("/tasks/#{id}/projects", params: params, options: options)), type: Project, client: client)
end

#remove_followers(followers: required("followers"), options: {}, **data) ⇒ Object

Removes each of the specified followers from the task if they are following. Returns the complete, updated record for the affected task.

Parameters:

  • followers (Array) (defaults to: required("followers"))

    An array of followers to remove from the task.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



163
164
165
166
# File 'lib/asana/resources/task.rb', line 163

def remove_followers(followers: required("followers"), options: {}, **data)
  with_params = data.merge(followers: followers).reject { |_,v| v.nil? || Array(v).empty? }
  refresh_with(parse(client.post("/tasks/#{id}/removeFollowers", body: with_params, options: options)).first)
end

#remove_project(project: required("project"), options: {}, **data) ⇒ Object

Removes the task from the specified project. The task will still exist in the system, but it will not be in the project anymore.

Parameters:

  • Returns

    an empty data block.

  • project (Id) (defaults to: required("project"))

    The project to remove the task from.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



211
212
213
214
# File 'lib/asana/resources/task.rb', line 211

def remove_project(project: required("project"), options: {}, **data)
  with_params = data.merge(project: project).reject { |_,v| v.nil? || Array(v).empty? }
  client.post("/tasks/#{id}/removeProject", body: with_params, options: options) && true
end

#remove_tag(tag: required("tag"), options: {}, **data) ⇒ Object

Removes a tag from the task. Returns an empty data block.

Parameters:

  • tag (Id) (defaults to: required("tag"))

    The tag to remove from the task.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



240
241
242
243
# File 'lib/asana/resources/task.rb', line 240

def remove_tag(tag: required("tag"), options: {}, **data)
  with_params = data.merge(tag: tag).reject { |_,v| v.nil? || Array(v).empty? }
  client.post("/tasks/#{id}/removeTag", body: with_params, options: options) && true
end

#set_parent(parent: required("parent"), options: {}, **data) ⇒ Object

Changes the parent of a task. Each task may only be a subtask of a single parent, or no parent task at all. Returns an empty data block.

Parameters:

  • parent (Id) (defaults to: required("parent"))

    The new parent of the task, or ‘null` for no parent.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



270
271
272
273
# File 'lib/asana/resources/task.rb', line 270

def set_parent(parent: required("parent"), options: {}, **data)
  with_params = data.merge(parent: parent).reject { |_,v| v.nil? || Array(v).empty? }
  client.post("/tasks/#{id}/setParent", body: with_params, options: options) && true
end

#stories(per_page: 20, options: {}) ⇒ Object

Returns a compact representation of all of the stories on the task.

Parameters:

  • per_page (Integer) (defaults to: 20)

    the number of records to fetch per page.

  • options (Hash) (defaults to: {})

    the request I/O options.



279
280
281
282
# File 'lib/asana/resources/task.rb', line 279

def stories(per_page: 20, options: {})
  params = { limit: per_page }.reject { |_,v| v.nil? || Array(v).empty? }
  Collection.new(parse(client.get("/tasks/#{id}/stories", params: params, options: options)), type: Story, client: client)
end

#subtasks(per_page: 20, options: {}) ⇒ Object

Returns a compact representation of all of the subtasks of a task.

Parameters:

  • per_page (Integer) (defaults to: 20)

    the number of records to fetch per page.

  • options (Hash) (defaults to: {})

    the request I/O options.



249
250
251
252
# File 'lib/asana/resources/task.rb', line 249

def subtasks(per_page: 20, options: {})
  params = { limit: per_page }.reject { |_,v| v.nil? || Array(v).empty? }
  Collection.new(parse(client.get("/tasks/#{id}/subtasks", params: params, options: options)), type: self.class, client: client)
end

#tags(per_page: 20, options: {}) ⇒ Object

Returns a compact representation of all of the tags the task has.

Parameters:

  • per_page (Integer) (defaults to: 20)

    the number of records to fetch per page.

  • options (Hash) (defaults to: {})

    the request I/O options.



220
221
222
223
# File 'lib/asana/resources/task.rb', line 220

def tags(per_page: 20, options: {})
  params = { limit: per_page }.reject { |_,v| v.nil? || Array(v).empty? }
  Collection.new(parse(client.get("/tasks/#{id}/tags", params: params, options: options)), type: Tag, client: client)
end

#update(options: {}, **data) ⇒ Object

A specific, existing task can be updated by making a PUT request on the URL for that task. Only the fields provided in the ‘data` block will be updated; any unspecified fields will remain unchanged.

When using this method, it is best to specify only those fields you wish to change, or else you may overwrite changes made by another user since you last retrieved the task.

Parameters:

  • Returns

    the complete updated task record.

  • options (Hash) (defaults to: {})

    the request I/O options.

  • data (Hash)

    the attributes to post.



130
131
132
133
# File 'lib/asana/resources/task.rb', line 130

def update(options: {}, **data)

  refresh_with(parse(client.put("/tasks/#{id}", body: data, options: options)).first)
end