Class: Todoist::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/todoist/task.rb

Overview

Todoist Task

A todoist task.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, project_id, d = {}) ⇒ Task

Returns a new instance of Task.



143
144
145
146
147
148
149
150
# File 'lib/todoist/task.rb', line 143

def initialize(content, project_id, d={})
  @content    = content
  @project_id = project_id.to_i
  @date       = d.delete('date') || d.delete('date_string')
  @priority   = d.delete('priority')
  @id         = d.delete('id')
  @task_details = d
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



219
220
221
222
223
224
225
226
227
# File 'lib/todoist/task.rb', line 219

def method_missing(*args, &block)
  # the method name
  m = args.shift
  if @task_details.has_key?(m.to_s)
    return @task_details[m.to_s]
  else
    raise NoMethodError, "undefined method `#{m}' for #{self.inspect}"
  end
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



8
9
10
# File 'lib/todoist/task.rb', line 8

def content
  @content
end

#dateObject (readonly)

Returns the value of attribute date.



9
10
11
# File 'lib/todoist/task.rb', line 9

def date
  @date
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/todoist/task.rb', line 9

def id
  @id
end

#priorityObject

Returns the value of attribute priority.



8
9
10
# File 'lib/todoist/task.rb', line 8

def priority
  @priority
end

#project_idObject

Returns the value of attribute project_id.



8
9
10
# File 'lib/todoist/task.rb', line 8

def project_id
  @project_id
end

#task_detailsObject

Returns the value of attribute task_details.



8
9
10
# File 'lib/todoist/task.rb', line 8

def task_details
  @task_details
end

Class Method Details

.allArray

Get all tasks

Retrieves all the uncompleted tasks

Returns:

  • (Array)

    An array of Todist::Tasks



62
63
64
# File 'lib/todoist/task.rb', line 62

def self.all
  query('viewall')['viewall']
end

.complete(*ids) ⇒ Object

Complete tasks

Completes a list of tasks

Parameters:

  • ids (Array, Integer, Todist::Task)

    A list of tasks to complete.



40
41
42
# File 'lib/todoist/task.rb', line 40

def self.complete(*ids)
  make_tasks(Base.get('/completeItems', :query => {:ids => id_array(ids)}))
end

.completed(project) ⇒ Array

Get completed tasks for project

Retrieves completed tasks from the todoist service for the project id.

Parameters:

Returns:

  • (Array)

    An array of todoist tasks.



30
31
32
# File 'lib/todoist/task.rb', line 30

def self.completed(project)
  make_tasks(Base.get('/getCompletedItems', :query => { :project_id => project.to_i }))
end

.create(content, project, opts = {}) ⇒ Object

Create a new task

Parameters:

  • content (String)

    The content of the new task.

  • project (Integer, Todoist::Project)

    The project to create the new task in

  • opts (Hash) (defaults to: {})

    Optional priority and due date for creation.



117
118
119
120
121
122
123
# File 'lib/todoist/task.rb', line 117

def self.create(content, project, opts={})
  query = {:project_id => project.to_i, :content => content.to_s }
  query['priority'] = opts.delete('priority') if opts.has_key?('priority')
  query['date_string'] = opts.delete('date_string') if opts.has_key?('date_string')

  new_from_api(Base.get('/addItem', :query => query))
end

.get(*ids) ⇒ Array

Get Tasks by ID

Retrives a list of tasks from the todoist service.

Parameters:

  • ids (Array, Integer, Todist::Task)

    A list of tasks to retrieve

Returns:

  • (Array)

    An array of Todist::Tasks



52
53
54
# File 'lib/todoist/task.rb', line 52

def self.get(*ids)
  make_tasks(Base.get('/getItemsById', :query => {:ids => id_array(ids)}))
end

.overdueArray

Get overdue tasks

Retrieves all the overdue tasks

Returns:

  • (Array)

    An array of overdue Todist::Tasks



72
73
74
# File 'lib/todoist/task.rb', line 72

def self.overdue
  query('overdue')['overdue']
end

.query(*queries) ⇒ Hash

Query

Use the task query API to get back several arrays of tasks.

Allowed queries

viewall:: All tasks
overdue:: All overdue tasks
p[123]::      All tasks of priority 1, 2 ,3

Parameters:

  • query (String, Array)

    A query or a list of queries to perform

Returns:

  • (Hash)

    a hash keyed on query, containing arrays of tasks.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/todoist/task.rb', line 90

def self.query(*queries)
  query = '["' + queries.flatten.map { |q| q.to_s }.join('","') + '"]'
  results = {}
  response = Base.get('/query', :query => { :queries => query })

  response.each do |q|
    if q['type'] == 'viewall'
      tasks = []
      q['data'].each do |stuff|
        tasks << make_tasks(stuff['uncompleted'])
      end
      results[q['type']] = tasks.flatten
    else
      results[q['type']] = make_tasks(q['data'])
    end
  end
  results
end

.uncompleted(project) ⇒ Array

Get tasks for a project

Retrieves all uncompleted tasks from the todoist service for the project id.

Parameters:

Returns:

  • (Array)

    An array of todoist tasks.



18
19
20
# File 'lib/todoist/task.rb', line 18

def self.uncompleted(project)
  make_tasks(Base.get('/getUncompletedItems', :query => { :project_id => project.to_i }))
end

.update(content, id, opts = {}) ⇒ Object

Updates a task

Parameters:

  • content (String)

    The new content of the task.

  • id (Integer, Todoist::Task)

    The task to update

  • opts (Hash) (defaults to: {})

    Optional priority and due date for creation.



133
134
135
136
137
138
139
# File 'lib/todoist/task.rb', line 133

def self.update(content, id, opts={})
  query = {:id => project.to_i, :content => content.to_s }
  query['priority'] = opts.delete('priority') if opts.has_key?('priority')
  query['date_string'] = opts.delete('date_string') if opts.has_key?('date_string')

  new_from_api(Base.get('/updateItem', :query => query))
end

Instance Method Details

#completeObject

Complete

Completes the todoist task



163
164
165
166
# File 'lib/todoist/task.rb', line 163

def complete
  self.class.complete(self) unless complete?
  @task_details['in_history'] = 1
end

#complete?Boolean

Is the task complete

Returns:

  • (Boolean)


154
155
156
# File 'lib/todoist/task.rb', line 154

def complete?
  (@task_details['in_history'] == 1) ? true : false
end

#inspectObject



215
216
217
# File 'lib/todoist/task.rb', line 215

def inspect
  "<Todoist::Task:#{content}:#{id}:#{project_id}:#{task_details.inspect}>"
end

#overdue?Boolean

Is the task overdue?

Returns:

  • (Boolean)


170
171
172
173
# File 'lib/todoist/task.rb', line 170

def overdue?
  return false unless due_date
  Time.now > Time.parse(due_date)
end

#projectObject

Project

Retreives the project for the task



179
180
181
# File 'lib/todoist/task.rb', line 179

def project
  Project.get(project_id)
end

#saveObject

Saves the task

Save the task, either creating a new task on the todoist service, or updating a previously retrieved task with new content, priority etc.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/todoist/task.rb', line 188

def save
  opts = {}
  opts['priority'] = priority if priority
  opts['date_string'] = date if date
  # if we don't have an id, then we can assume this is a new task.
  unless (task_details.has_key?('id'))
    result = self.class.create(self.content, self.project_id, opts)
  else
    result = self.class.update(self.content, self.id, opts)
  end

  self.content      = result.content
  self.project_id   = result.project_id
  self.task_details = result.task_details
  self
end

#to_iObject



211
212
213
# File 'lib/todoist/task.rb', line 211

def to_i
  id
end

#to_sObject



207
208
209
# File 'lib/todoist/task.rb', line 207

def to_s
  @content
end