Class: Checkoff::CLI

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

Overview

Provide ability for CLI to pull Asana items

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspaces: Checkoff::Workspaces.new, projects: Checkoff::Projects.new, sections: Checkoff::Sections.new(projects: projects), tasks: Checkoff::Tasks.new, stderr: STDERR, kernel: Kernel) ⇒ CLI

Returns a new instance of CLI.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/checkoff/cli.rb', line 18

def initialize(workspaces: Checkoff::Workspaces.new,
               projects: Checkoff::Projects.new,
               sections: Checkoff::Sections.new(projects: projects),
               tasks: Checkoff::Tasks.new,
               stderr: STDERR,
               kernel: Kernel)
  @workspaces = workspaces
  @projects = projects
  @sections = sections
  @tasks = tasks
  @kernel = kernel
  @stderr = stderr
end

Instance Attribute Details

#sectionsObject (readonly)

Returns the value of attribute sections.



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

def sections
  @sections
end

#stderrObject (readonly)

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

Instance Method Details

#output_helpObject



100
101
102
103
104
105
106
107
# File 'lib/checkoff/cli.rb', line 100

def output_help
  stderr.puts 'View tasks:'
  stderr.puts "  #{$PROGRAM_NAME} view workspace project [section]"
  stderr.puts "  #{$PROGRAM_NAME} quickadd workspace task_name"
  stderr.puts
  stderr.puts "'project' can be set to a project name, or :my_tasks, " \
              ':my_tasks_upcoming, :my_tasks_new, or :my_tasks_today'
end

#parse_args(args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/checkoff/cli.rb', line 87

def parse_args(args)
  mode = args[0]
  subargs = OpenStruct.new
  if mode == 'view'
    parse_view_args(subargs, args)
  elsif mode == 'quickadd'
    parse_quickadd_args(subargs, args)
  else
    raise
  end
  [mode, subargs]
end

#parse_quickadd_args(subargs, args) ⇒ Object



82
83
84
85
# File 'lib/checkoff/cli.rb', line 82

def parse_quickadd_args(subargs, args)
  subargs.workspace = args[1]
  subargs.task_name = args[2]
end

#parse_view_args(subargs, args) ⇒ Object



76
77
78
79
80
# File 'lib/checkoff/cli.rb', line 76

def parse_view_args(subargs, args)
  subargs.workspace = args[1]
  subargs.project = args[2]
  subargs.section = args[3]
end

#quickadd(workspace_name, task_name) ⇒ Object



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

def quickadd(workspace_name, task_name)
  workspace = @workspaces.workspace_by_name(workspace_name)
  @tasks.add_task(task_name,
                  workspace_id: workspace.id)
end

#run(args) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/checkoff/cli.rb', line 118

def run(args)
  validate_args!(args)
  command, subargs = parse_args(args)
  if command == 'view'
    view(subargs.workspace, subargs.project, subargs.section)
  elsif command == 'quickadd'
    quickadd(subargs.workspace, subargs.task_name)
  else
    raise
  end
end

#run_on_project(workspace, project) ⇒ Object



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

def run_on_project(workspace, project)
  tasks_by_section =
    sections.tasks_by_section(workspace, project)
  tasks_by_section.update(tasks_by_section) do |_key, tasks|
    tasks_to_hash(tasks)
  end
  tasks_by_section.to_json
end

#run_on_section(workspace, project, section) ⇒ Object



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

def run_on_section(workspace, project, section)
  section = nil if section == ''
  tasks = sections.tasks(workspace, project, section) || []
  tasks_to_hash(tasks).to_json
end

#task_to_hash(task) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/checkoff/cli.rb', line 32

def task_to_hash(task)
  task_out = {
    name: task.name,
  }
  if task.due_on
    task_out[:due] = task.due_on
  elsif task.due_at
    task_out[:due] = task.due_at
  end
  task_out
end

#tasks_to_hash(tasks) ⇒ Object



44
45
46
# File 'lib/checkoff/cli.rb', line 44

def tasks_to_hash(tasks)
  tasks.map { |task| task_to_hash(task) }
end

#validate_args!(args) ⇒ Object



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

def validate_args!(args)
  return unless args.length < 2 || !%w[view quickadd].include?(args[0])

  output_help
  exit(1)
end

#view(workspace_name, project_name, section_name) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/checkoff/cli.rb', line 109

def view(workspace_name, project_name, section_name)
  project_name = project_name[1..-1].to_sym if project_name.start_with? ':'
  if section_name.nil?
    run_on_project(workspace_name, project_name)
  else
    run_on_section(workspace_name, project_name, section_name)
  end
end