Class: Checkoff::Projects

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

Overview

Work with projects in Asana

Constant Summary collapse

MINUTE =
60
HOUR =
MINUTE * 60
DAY =
24 * HOUR
REALLY_LONG_CACHE_TIME =
HOUR * 1
LONG_CACHE_TIME =
MINUTE * 15
SHORT_CACHE_TIME =
MINUTE

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Projects.



26
27
28
29
30
31
32
33
# File 'lib/checkoff/projects.rb', line 26

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

Instance Method Details

#active_tasks(tasks) ⇒ Object

find uncompleted tasks in a list



68
69
70
# File 'lib/checkoff/projects.rb', line 68

def active_tasks(tasks)
  tasks.select { |task| task.completed_at.nil? }
end

#project(workspace_name, project_name) ⇒ Object

pulls an Asana API project class given a name



47
48
49
50
51
52
53
54
55
56
# File 'lib/checkoff/projects.rb', line 47

def project(workspace_name, project_name)
  if project_name.is_a?(Symbol) && project_name.to_s.start_with?('my_tasks')
    my_tasks(workspace_name)
  else
    projects = projects_by_workspace_name(workspace_name)
    projects.find do |project|
      project.name == project_name
    end
  end
end

#project_or_raise(workspace_name, project_name) ⇒ Object



59
60
61
62
63
64
# File 'lib/checkoff/projects.rb', line 59

def project_or_raise(workspace_name, project_name)
  project = project(workspace_name, project_name)
  raise "Could not find project #{project_name} under workspace #{workspace_name}." if project.nil?

  project
end

#task_optionsObject

Default options used in Asana API to pull taskso



36
37
38
39
40
41
42
43
44
# File 'lib/checkoff/projects.rb', line 36

def task_options
  {
    per_page: 100,
    options: {
      fields: %w[name completed_at due_at due_on tags
                 memberships.project.gid memberships.section.name dependencies],
    },
  }
end

#tasks_from_project(project, only_uncompleted: true, extra_fields: []) ⇒ Object

pull task objects from a named project



73
74
75
76
77
78
79
# File 'lib/checkoff/projects.rb', line 73

def tasks_from_project(project, only_uncompleted: true, extra_fields: [])
  options = task_options
  options[:completed_since] = '9999-12-01' if only_uncompleted
  options[:project] = project.gid
  options[:options][:fields] += extra_fields
  client.tasks.find_all(**options).to_a
end