Class: Checkoff::MyTasks

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

Overview

Query different sections of Asana ‘My Tasks’ projects

Constant Summary collapse

MINUTE =
60
LONG_CACHE_TIME =
MINUTE * 15
SHORT_CACHE_TIME =
MINUTE * 5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Checkoff::Internal::ConfigLoader.load(:asana), client: Checkoff::Clients.new(config: config).client, projects: Checkoff::Projects.new(config: config, client: client)) ⇒ MyTasks

Returns a new instance of MyTasks.

Parameters:

  • config (Hash<Symbol, Object>) (defaults to: Checkoff::Internal::ConfigLoader.load(:asana))
  • client (Asana::Client) (defaults to: Checkoff::Clients.new(config: config).client)
  • projects (Checkoff::Projects) (defaults to: Checkoff::Projects.new(config: config, client: client))


21
22
23
24
25
26
27
28
# File 'lib/checkoff/my_tasks.rb', line 21

def initialize(config: Checkoff::Internal::ConfigLoader.load(:asana),
               client: Checkoff::Clients.new(config: config).client,
               projects: Checkoff::Projects.new(config: config,
                                                client: client))
  @config = config
  @client = client
  @projects = projects
end

Instance Attribute Details

#projectsCheckoff::Projects (readonly)

Returns:



16
17
18
# File 'lib/checkoff/my_tasks.rb', line 16

def projects
  @projects
end

Instance Method Details

#by_my_tasks_section(tasks, project_gid) ⇒ Hash<String, Enumerable<Asana::Resources::Task>>

Given a list of tasks in ‘My Tasks’, pull a Hash of tasks with section name -> task list

Parameters:

  • tasks (Enumerable<Asana::Resources::Task>)
  • project_gid (String)

Returns:

  • (Hash<String, Enumerable<Asana::Resources::Task>>)


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/checkoff/my_tasks.rb', line 62

def by_my_tasks_section(tasks, project_gid)
  by_section = {}
  sections = client.sections.get_sections_for_project(project_gid: project_gid,
                                                      options: { fields: ['name'] })
  sections.each_entry { |section| by_section[section_key(section.name)] = [] }
  tasks.each do |task|
    assignee_section = task.assignee_section
    current_section = section_key(assignee_section.name)
    by_section[current_section] ||= []
    by_section[current_section] << task
  end
  by_section
end

#section_key(name) ⇒ String?

Parameters:

  • name (String)

Returns:

  • (String, nil)


50
51
52
53
54
# File 'lib/checkoff/my_tasks.rb', line 50

def section_key(name)
  return nil if name == 'Recently assigned'

  name
end

#tasks_by_section_for_my_tasks(project, only_uncompleted: true, extra_fields: []) ⇒ Hash<String, Enumerable<Asana::Resources::Task>>

Given a ‘My Tasks’ project object, pull all tasks, then provide a Hash of tasks with section name -> task list of the uncompleted tasks.

Parameters:

  • project (Asana::Resources::Project)
  • only_uncompleted (Boolean) (defaults to: true)
  • extra_fields (Array<String>) (defaults to: [])

Returns:

  • (Hash<String, Enumerable<Asana::Resources::Task>>)


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

def tasks_by_section_for_my_tasks(project,
                                  only_uncompleted: true,
                                  extra_fields: [])
  raw_tasks = projects.tasks_from_project(project,
                                          only_uncompleted: only_uncompleted,
                                          extra_fields: extra_fields + ['assignee_section.name'])
  active_tasks = projects.active_tasks(raw_tasks)
  by_my_tasks_section(active_tasks, project.gid)
end