Class: Checkoff::Sections

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

Overview

Query different sections of Asana projects

Constant Summary collapse

MINUTE =

Returns:

  • (Object)
60
HOUR =

Returns:

  • (Object)
MINUTE * 60
REALLY_LONG_CACHE_TIME =

Returns:

  • (Object)
MINUTE * 30
LONG_CACHE_TIME =

Returns:

  • (Object)
MINUTE * 15
SHORT_CACHE_TIME =

Returns:

  • (Object)
MINUTE * 5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Checkoff::Internal::ConfigLoader.load(:asana), client: Checkoff::Clients.new(config:).client, projects: Checkoff::Projects.new(config:, client:), workspaces: Checkoff::Workspaces.new(config:, client:), time: Time) ⇒ Object

sord warn - Asana::Client wasn't able to be resolved to a constant in this project @param config

@param client

@param projects

@param workspaces

@param time



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

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

Instance Attribute Details

#clientAsana::Client (readonly)

sord warn - Asana::Client wasn't able to be resolved to a constant in this project

Returns:

  • (Asana::Client)


248
249
250
# File 'lib/checkoff/sections.rb', line 248

def client
  @client
end

#my_tasksCheckoff::MyTasks (readonly)

Returns:



35
36
37
# File 'lib/checkoff/sections.rb', line 35

def my_tasks
  @my_tasks
end

#projectsCheckoff::Projects (readonly)

Returns:



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

def projects
  @projects
end

#timeClass<Time> (readonly)

Returns:

  • (Class<Time>)


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

def time
  @time
end

#workspacesCheckoff::Workspaces (readonly)



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

def workspaces
  @workspaces
end

Instance Method Details

#as_cache_keyHash

Returns:

  • (Hash)


215
216
217
# File 'lib/checkoff/sections.rb', line 215

def as_cache_key
  {}
end

#by_section(tasks, project_gid) ⇒ ::Hash[String?, ::Enumerable[Asana::Resources::Task]]

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project Given a list of tasks, pull a Hash of tasks with section name -> task list

@param tasks

@param project_gid

Parameters:

  • tasks (::Enumerable[Asana::Resources::Task])
  • project_gid (String)

Returns:

  • (::Hash[String?, ::Enumerable[Asana::Resources::Task]])


270
271
272
273
274
275
276
277
# File 'lib/checkoff/sections.rb', line 270

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

#debugvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1223
# File 'sig/checkoff.rbs', line 1223

def debug: (?Object? message) -> void

#errorvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1214
# File 'sig/checkoff.rbs', line 1214

def error: (?Object? message) -> void

#file_task_by_section(by_section, task, project_gid) ⇒ void

This method returns an undefined value.

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project @param by_section

@param task

@param project_gid

Parameters:

  • by_section (::Hash[String?, ::Array[Asana::Resources::Task]])
  • task (Asana::Resources::Task)
  • project_gid (String)


284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/checkoff/sections.rb', line 284

def file_task_by_section(by_section, task, project_gid)
  membership = task.memberships.find do |m|
    membership_hash = T.unsafe(m)
    T.cast(membership_hash['project'], T::Hash[String, T.untyped])['gid'] == project_gid
  end
  raise "Could not find task in project_gid #{project_gid}: #{task}" if membership.nil?

  membership_data = T.unsafe(membership)
  section = T.cast(membership_data['section'], T::Hash[String, T.untyped])
  section_name = T.cast(section['name'], String)

  # @type [String, nil]
  current_section = section_key(section_name)

  # @sg-ignore
  by_section.fetch(current_section) << task
end

#finervoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1226
# File 'sig/checkoff.rbs', line 1226

def finer: (?Object? message) -> void

#infovoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1220
# File 'sig/checkoff.rbs', line 1220

def info: (?Object? message) -> void

#log_levelSymbol

@sg-ignore

Returns:

  • (Symbol)


1229
# File 'sig/checkoff.rbs', line 1229

def log_level: () -> Symbol

#logger::Logger

Returns:

  • (::Logger)


1211
# File 'sig/checkoff.rbs', line 1211

def logger: () -> ::Logger

#parse(response) ⇒ ::Array[::Hash[untyped, untyped]]

sord warn - Asana::HttpClient::Response wasn't able to be resolved to a constant in this project https://github.com/Asana/ruby-asana/blob/master/lib/asana/resource_includes/response_helper.rb#L7

@param response

Parameters:

  • response (Asana::HttpClient::Response)

Returns:

  • (::Array[::Hash[untyped, untyped]])


239
240
241
242
243
244
245
# File 'lib/checkoff/sections.rb', line 239

def parse(response)
  data = response.body.fetch('data') do
    raise("Unexpected response body: #{response.body}")
  end
  extra = response.body.except('data')
  [data, extra]
end

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

sord warn - Asana::Resources::Section wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Section wasn't able to be resolved to a constant in this project @param section

Parameters:

  • section (Asana::Resources::Section)

Returns:

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


186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/checkoff/sections.rb', line 186

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

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

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

  # @sg-ignore
  sections[index - 1]
end

#project_or_raise(workspace_name, project_name) ⇒ Asana::Resources::Project

sord warn - Asana::Resources::Project wasn't able to be resolved to a constant in this project @sg-ignore projects.project may be nil but is raised below

@param workspace_name

@param project_name

Parameters:

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

Returns:

  • (Asana::Resources::Project)


306
307
308
309
310
311
312
313
314
315
# File 'lib/checkoff/sections.rb', line 306

def project_or_raise(workspace_name, project_name)
  raise ArgumentError, 'Provide nil project_name' if T.unsafe(project_name).nil?

  project = projects.project(workspace_name, project_name)
  if project.nil?
    raise "Could not find project #{project_name.inspect} " \
          "under workspace #{workspace_name}"
  end
  project
end

#section(workspace_name, project_name, section_name, extra_section_fields: []) ⇒ Object

sord warn - Asana::Resources::Section wasn't able to be resolved to a constant in this project @param workspace_name

@param project_name

@param section_name

@param extra_section_fields



225
226
227
228
229
# File 'lib/checkoff/sections.rb', line 225

def section(workspace_name, project_name, section_name, extra_section_fields: [])
  sections = sections_or_raise(workspace_name, project_name,
                               extra_fields: extra_section_fields)
  sections.find { |section| section_key(T.cast(section.name, String))&.chomp(':') == section_name&.chomp(':') }
end

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

sord warn - Asana::Resources::Section wasn't able to be resolved to a constant in this project @param gid

Parameters:

  • gid (String)

Returns:

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


204
205
206
207
208
209
210
211
# File 'lib/checkoff/sections.rb', line 204

def section_by_gid(gid)
  options = {}
  Asana::Resources::Section.new(parse(client.get("/sections/#{gid}", options:)).first,
                                client:)
rescue Asana::Errors::NotFound => e
  debug e
  nil
end

#section_key(name) ⇒ String?

@param name

Parameters:

  • name (String, nil)

Returns:

  • (String, nil)


176
177
178
179
180
181
# File 'lib/checkoff/sections.rb', line 176

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: []) ⇒ Object

sord warn - Asana::Resources::Section wasn't able to be resolved to a constant in this project @sg-ignore section() is nil-checked below

@param workspace_name

@param project_name

@param section_name

@param extra_section_fields



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/checkoff/sections.rb', line 160

def section_or_raise(workspace_name, project_name, section_name, extra_section_fields: [])
  s = section(workspace_name, project_name, section_name,
              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. @sg-ignore

@param workspace_name

@param project_name

@param section_name

Parameters:

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

Returns:

  • (::Array[String])


146
147
148
149
150
# File 'lib/checkoff/sections.rb', line 146

def section_task_names(workspace_name, project_name, section_name)
  task_array = tasks(workspace_name, project_name, section_name)
  # @type [Array<String>]
  T.cast(task_array.map(&:name), T::Array[String])
end

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

sord warn - Asana::Resources::Section wasn't able to be resolved to a constant in this project Returns a list of Asana API section objects for a given project GID

@param project_gid

@param extra_fields

Parameters:

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

Returns:

  • (::Enumerable[Asana::Resources::Section])


74
75
76
77
78
# File 'lib/checkoff/sections.rb', line 74

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

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

sord warn - Asana::Resources::Section wasn't able to be resolved to a constant in this project Returns a list of Asana API section objects for a given project

@param workspace_name

@param project_name

@param extra_fields

Parameters:

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

Returns:

  • (::Enumerable[Asana::Resources::Section])


63
64
65
66
# File 'lib/checkoff/sections.rb', line 63

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:)
end

#tasks(workspace_name, project_name, section_name, only_uncompleted: true, extra_fields: []) ⇒ Object

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project Pulls task objects from a specified section

@param workspace_name

@param project_name

@param section_name

@param only_uncompleted

@param extra_fields



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/checkoff/sections.rb', line 125

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(extra_fields:,
                                  only_uncompleted:)
  # Note: 30 minute cache time on a raw Enumerable from SDK gives
  # 'Your pagination token has expired' errors.  So we go ahead
  # and eagerly evaluate here so we can enjoy the cache.
  client.tasks.get_tasks(section: section.gid,
                         **options).to_a
end

#tasks_by_section(workspace_name, project_name, only_uncompleted: true, extra_fields: []) ⇒ Object

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project Given a workspace name and project name, then provide a Hash of tasks with section name -> task list of the uncompleted tasks

@param workspace_name

@param project_name

@param only_uncompleted

@param extra_fields



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/checkoff/sections.rb', line 88

def tasks_by_section(workspace_name,
                     project_name,
                     only_uncompleted: true,
                     extra_fields: [])
  raise ArgumentError, 'Provided nil workspace name' if T.unsafe(workspace_name).nil?
  raise ArgumentError, 'Provided nil project name' if T.unsafe(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:, extra_fields:)
  else
    tasks_by_section_for_project(project, only_uncompleted:, extra_fields:)
  end
end

#tasks_by_section_for_project(project, only_uncompleted: true, extra_fields: []) ⇒ ::Hash[String?, ::Enumerable[Asana::Resources::Task]]

sord warn - Asana::Resources::Project wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project Given a project object, pull all tasks, then provide a Hash of tasks with section name -> task list of the uncompleted tasks

@param project

@param only_uncompleted

@param extra_fields

Parameters:

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

Returns:

  • (::Hash[String?, ::Enumerable[Asana::Resources::Task]])


256
257
258
259
260
261
262
263
264
# File 'lib/checkoff/sections.rb', line 256

def tasks_by_section_for_project(project,
                                 only_uncompleted: true,
                                 extra_fields: [])
  raw_tasks = projects.tasks_from_project(project,
                                          only_uncompleted:,
                                          extra_fields:)
  active_tasks = projects.active_tasks(raw_tasks)
  by_section(active_tasks, project.gid)
end

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

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project @param section_gid

@param only_uncompleted

@param extra_fields

Parameters:

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

Returns:

  • (::Enumerable[Asana::Resources::Task])


108
109
110
111
112
113
114
# File 'lib/checkoff/sections.rb', line 108

def tasks_by_section_gid(section_gid,
                         only_uncompleted: true,
                         extra_fields: [])
  options = projects.task_options(extra_fields:,
                                  only_uncompleted:)
  client.tasks.get_tasks(section: section_gid, **options)
end

#warnvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1217
# File 'sig/checkoff.rbs', line 1217

def warn: (?Object? message) -> void