Class: Todoist::Task
- Inherits:
-
Object
- Object
- Todoist::Task
- Defined in:
- lib/todoist/task.rb
Overview
Todoist Task
A todoist task.
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#priority ⇒ Object
Returns the value of attribute priority.
-
#project_id ⇒ Object
Returns the value of attribute project_id.
-
#task_details ⇒ Object
Returns the value of attribute task_details.
Class Method Summary collapse
-
.all ⇒ Array
Get all tasks.
-
.complete(*ids) ⇒ Object
Complete tasks.
-
.completed(project) ⇒ Array
Get completed tasks for project.
-
.create(content, project, opts = {}) ⇒ Object
Create a new task.
-
.get(*ids) ⇒ Array
Get Tasks by ID.
-
.overdue ⇒ Array
Get overdue tasks.
-
.query(*queries) ⇒ Hash
Query.
-
.uncompleted(project) ⇒ Array
Get tasks for a project.
-
.update(content, id, opts = {}) ⇒ Object
Updates a task.
Instance Method Summary collapse
-
#complete ⇒ Object
Complete.
-
#complete? ⇒ Boolean
Is the task complete.
-
#initialize(content, project_id, d = {}) ⇒ Task
constructor
A new instance of Task.
- #inspect ⇒ Object
- #method_missing(*args, &block) ⇒ Object
-
#overdue? ⇒ Boolean
Is the task overdue?.
-
#project ⇒ Object
Project.
-
#save ⇒ Object
Saves the task.
- #to_i ⇒ Object
- #to_s ⇒ Object
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
#content ⇒ Object
Returns the value of attribute content.
8 9 10 |
# File 'lib/todoist/task.rb', line 8 def content @content end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
9 10 11 |
# File 'lib/todoist/task.rb', line 9 def date @date end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/todoist/task.rb', line 9 def id @id end |
#priority ⇒ Object
Returns the value of attribute priority.
8 9 10 |
# File 'lib/todoist/task.rb', line 8 def priority @priority end |
#project_id ⇒ Object
Returns the value of attribute project_id.
8 9 10 |
# File 'lib/todoist/task.rb', line 8 def project_id @project_id end |
#task_details ⇒ Object
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
.all ⇒ Array
Get all tasks
Retrieves all the uncompleted 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
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.
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
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.
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 |
.overdue ⇒ Array
Get overdue tasks
Retrieves all the overdue 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
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.
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
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
#complete ⇒ Object
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
154 155 156 |
# File 'lib/todoist/task.rb', line 154 def complete? (@task_details['in_history'] == 1) ? true : false end |
#inspect ⇒ Object
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?
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 |
#project ⇒ Object
Project
Retreives the project for the task
179 180 181 |
# File 'lib/todoist/task.rb', line 179 def project Project.get(project_id) end |
#save ⇒ Object
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_i ⇒ Object
211 212 213 |
# File 'lib/todoist/task.rb', line 211 def to_i id end |
#to_s ⇒ Object
207 208 209 |
# File 'lib/todoist/task.rb', line 207 def to_s @content end |