class Abt::Providers::Asana::Services::TaskPicker

Attributes

cli[R]
config[R]
path[R]
project[R]

Public Class Methods

call(**args) click to toggle source
# File lib/abt/providers/asana/services/task_picker.rb, line 17
def self.call(**args)
  new(**args).call
end
new(cli:, path:, config:, project:) click to toggle source
# File lib/abt/providers/asana/services/task_picker.rb, line 23
def initialize(cli:, path:, config:, project:)
  @cli = cli
  @path = path
  @config = config
  @project = project
end

Public Instance Methods

call() click to toggle source
# File lib/abt/providers/asana/services/task_picker.rb, line 30
def call
  task = select_task

  path_with_task = Path.from_gids(project_gid: path.project_gid, task_gid: task["gid"])

  Result.new(task: task, path: path_with_task)
end

Private Instance Methods

api() click to toggle source
# File lib/abt/providers/asana/services/task_picker.rb, line 76
def api
  Abt::Providers::Asana::Api.new(access_token: config.access_token)
end
prompt_section() click to toggle source
# File lib/abt/providers/asana/services/task_picker.rb, line 52
def prompt_section
  cli.prompt.choice("Which section in #{project['name']}?", sections)
end
sections() click to toggle source
# File lib/abt/providers/asana/services/task_picker.rb, line 69
def sections
  @sections ||= begin
    cli.warn("Fetching sections...")
    api.get_paged("projects/#{project['gid']}/sections", opt_fields: "name")
  end
end
select_task() click to toggle source
# File lib/abt/providers/asana/services/task_picker.rb, line 40
def select_task
  section = prompt_section
  tasks = tasks_in_section(section)

  if tasks.length.zero?
    cli.warn("Section is empty")
    select_task
  else
    cli.prompt.choice("Select a task", tasks, nil_option: true) || select_task
  end
end
tasks_in_section(section) click to toggle source
# File lib/abt/providers/asana/services/task_picker.rb, line 56
def tasks_in_section(section)
  cli.warn("Fetching tasks...")
  tasks = api.get_paged(
    "tasks",
    section: section["gid"],
    opt_fields: "name,completed,permalink_url"
  )

  # The below filtering is the best we can do with Asanas api, see this:
  # https://forum.asana.com/t/tasks-query-completed-since-is-broken-for-sections/21461
  tasks.reject { |task| task["completed"] }
end