Class: Checkoff::Sections

Inherits:
Object
  • Object
show all
Extended by:
CacheMethod::ClassMethods, Forwardable
Defined in:
lib/checkoff/sections.rb

Overview

Query different sections of Asana 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), workspaces: Checkoff::Workspaces.new(config: config, client: client), time: Time) ⇒ Sections

Returns a new instance of Sections.

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))
  • workspaces (Checkoff::Workspaces) (defaults to: Checkoff::Workspaces.new(config: config, client: client))
  • time (Class<Time>) (defaults to: Time)


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/checkoff/sections.rb', line 38

def initialize(config: Checkoff::Internal::ConfigLoader.load(:asana),
               client: Checkoff::Clients.new(config: config).client,
               projects: Checkoff::Projects.new(config: config,
                                                client: client),
               workspaces: Checkoff::Workspaces.new(config: config,
                                                    client: client),
               time: Time)
  @projects = projects
  @workspaces = workspaces
  @my_tasks = Checkoff::MyTasks.new(config: config, projects: projects, client: client)
  @client = client
  @time = time
end

Instance Attribute Details

#my_tasksCheckoff::MyTasks (readonly)

Returns:



31
32
33
# File 'lib/checkoff/sections.rb', line 31

def my_tasks
  @my_tasks
end

#projectsCheckoff::Projects (readonly)

Returns:



22
23
24
# File 'lib/checkoff/sections.rb', line 22

def projects
  @projects
end

#timeClass<Time> (readonly)

Returns:

  • (Class<Time>)


28
29
30
# File 'lib/checkoff/sections.rb', line 28

def time
  @time
end

#workspacesCheckoff::Workspaces (readonly)



25
26
27
# File 'lib/checkoff/sections.rb', line 25

def workspaces
  @workspaces
end

Instance Method Details

#previous_section(section) ⇒ Asana::Resources::Section?

Parameters:

  • section (Asana::Resources::Section)

Returns:

  • (Asana::Resources::Section, nil)


180
181
182
183
184
185
186
187
188
189
190
# File 'lib/checkoff/sections.rb', line 180

def previous_section(section)
  sections = sections_by_project_gid(section.project.fetch('gid'))

  # @type [Array<Asana::Resources::Section>]
  sections = sections.to_a

  index = sections.find_index { |s| s.gid == section.gid }
  return nil if index.nil? || index.zero?

  sections[index - 1]
end

#section_by_gid(gid) ⇒ Asana::Resources::Section

Parameters:

  • gid (String)

Returns:

  • (Asana::Resources::Section)


196
197
198
199
200
# File 'lib/checkoff/sections.rb', line 196

def section_by_gid(gid)
  options = {}
  Asana::Resources::Section.new(parse(client.get("/sections/#{gid}", options: options)).first,
                                client: client)
end

#section_key(name) ⇒ String?

Parameters:

  • name (String)

Returns:

  • (String, nil)


170
171
172
173
174
175
# File 'lib/checkoff/sections.rb', line 170

def section_key(name)
  inbox_section_names = ['(no section)', 'Untitled section', 'Inbox', 'Recently assigned']
  return nil if inbox_section_names.include?(name)

  name
end

#section_or_raise(workspace_name, project_name, section_name, extra_section_fields: []) ⇒ Asana::Resources::Section

@sg-ignore

Parameters:

  • workspace_name (String)
  • project_name (String, Symbol)
  • section_name (String, nil)
  • extra_section_fields (Array<String>) (defaults to: [])

Returns:

  • (Asana::Resources::Section)


154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/checkoff/sections.rb', line 154

def section_or_raise(workspace_name, project_name, section_name, extra_section_fields: [])
  s = section(workspace_name, project_name, section_name,
              extra_section_fields: extra_section_fields)
  if s.nil?
    valid_sections = sections_or_raise(workspace_name, project_name,
                                       extra_fields: extra_section_fields).map(&:name)

    raise "Could not find section #{section_name.inspect} under project #{project_name.inspect} " \
          "under workspace #{workspace_name.inspect}.  Valid sections: #{valid_sections.inspect}"
  end
  s
end

#section_task_names(workspace_name, project_name, section_name) ⇒ Array<String>

Pulls just names of tasks from a given section.

Parameters:

  • workspace_name (String)
  • project_name (String, Symbol)
  • section_name (String, nil)

Returns:

  • (Array<String>)


141
142
143
144
# File 'lib/checkoff/sections.rb', line 141

def section_task_names(workspace_name, project_name, section_name)
  task_array = tasks(workspace_name, project_name, section_name)
  task_array.map(&:name)
end

#sections_by_project_gid(project_gid, extra_fields: []) ⇒ Enumerable<Asana::Resources::Section>

Returns a list of Asana API section objects for a given project GID

Parameters:

  • project_gid (String)
  • extra_fields (Array<String>) (defaults to: [])

Returns:

  • (Enumerable<Asana::Resources::Section>)


69
70
71
72
73
# File 'lib/checkoff/sections.rb', line 69

def sections_by_project_gid(project_gid, extra_fields: [])
  fields = %w[name] + extra_fields
  client.sections.get_sections_for_project(project_gid: project_gid,
                                           options: { fields: fields })
end

#sections_or_raise(workspace_name, project_name, extra_fields: []) ⇒ Enumerable<Asana::Resources::Section>

Returns a list of Asana API section objects for a given project

Parameters:

  • workspace_name (String)
  • project_name (String, Symbol)
  • extra_fields (Array<String>) (defaults to: [])

Returns:

  • (Enumerable<Asana::Resources::Section>)


58
59
60
61
# File 'lib/checkoff/sections.rb', line 58

def sections_or_raise(workspace_name, project_name, extra_fields: [])
  project = project_or_raise(workspace_name, project_name)
  sections_by_project_gid(project.gid, extra_fields: extra_fields)
end

#tasks(workspace_name, project_name, section_name, only_uncompleted: true, extra_fields: []) ⇒ Enumerable<Asana::Resources::Task>

XXX: Rename to section_tasks

Pulls task objects from a specified section

Parameters:

  • workspace_name (String)
  • project_name (String, Symbol)
  • section_name (String, nil)
  • only_uncompleted (Boolean) (defaults to: true)
  • extra_fields (Array<String>) (defaults to: [])

Returns:

  • (Enumerable<Asana::Resources::Task>)


123
124
125
126
127
128
129
130
131
132
# File 'lib/checkoff/sections.rb', line 123

def tasks(workspace_name, project_name, section_name,
          only_uncompleted: true,
          extra_fields: [])
  section = section_or_raise(workspace_name, project_name, section_name)
  options = projects.task_options
  options[:options][:fields] += extra_fields
  options[:completed_since] = '9999-12-01' if only_uncompleted
  client.tasks.get_tasks(section: section.gid,
                         **options)
end

#tasks_by_section(workspace_name, project_name, only_uncompleted: true, extra_fields: []) ⇒ Hash{[String, nil] => Enumerable<Asana::Resources::Task>}

Given a workspace name and project name, then provide a Hash of tasks with section name -> task list of the uncompleted tasks

Parameters:

  • workspace_name (String)
  • project_name (String, Symbol)
  • only_uncompleted (Boolean) (defaults to: true)
  • extra_fields (Array<String>) (defaults to: [])

Returns:

  • (Hash{[String, nil] => Enumerable<Asana::Resources::Task>})

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/checkoff/sections.rb', line 83

def tasks_by_section(workspace_name,
                     project_name,
                     only_uncompleted: true,
                     extra_fields: [])
  raise ArgumentError, 'Provided nil workspace name' if workspace_name.nil?
  raise ArgumentError, 'Provided nil project name' if project_name.nil?

  project = project_or_raise(workspace_name, project_name)
  if project_name == :my_tasks
    my_tasks.tasks_by_section_for_my_tasks(project, only_uncompleted: only_uncompleted, extra_fields: extra_fields)
  else
    tasks_by_section_for_project(project, only_uncompleted: only_uncompleted, extra_fields: extra_fields)
  end
end

#tasks_by_section_gid(section_gid, only_uncompleted: true, extra_fields: []) ⇒ Enumerable<Asana::Resources::Task>

Parameters:

  • section_gid (String)
  • only_uncompleted (Boolean) (defaults to: true)
  • extra_fields (Array<String>) (defaults to: [])

Returns:

  • (Enumerable<Asana::Resources::Task>)


103
104
105
106
107
108
109
110
# File 'lib/checkoff/sections.rb', line 103

def tasks_by_section_gid(section_gid,
                         only_uncompleted: true,
                         extra_fields: [])
  options = projects.task_options
  options[:options][:fields] += extra_fields
  options[:completed_since] = '9999-12-01' if only_uncompleted
  client.tasks.get_tasks(section: section_gid, **options)
end