Class: Todoist::Sync::Items

Inherits:
Todoist::Service show all
Includes:
Util
Defined in:
lib/todoist/sync/items.rb

Instance Method Summary collapse

Methods inherited from Todoist::Service

#initialize

Constructor Details

This class inherits a constructor from Todoist::Service

Instance Method Details

#add(args) ⇒ Object

Add a item with a given hash of attributes and returns the item id



12
13
14
# File 'lib/todoist/sync/items.rb', line 12

def add(args)
  return @client.api_helper.add(args, "item_add")
end

#close(item) ⇒ Object

A simplified version of item_complete / item_update_date_complete. The command does exactly what official clients do when you close a item given an item.



79
80
81
82
# File 'lib/todoist/sync/items.rb', line 79

def close(item)
  args = {id: item.id}
  return @client.api_helper.command(args, "item_close")
end

#collectionObject

Return a Hash of items where key is the id of a item and value is a item



7
8
9
# File 'lib/todoist/sync/items.rb', line 7

def collection
  return @client.api_helper.collection("items")
end

#complete(items, force_history = 1) ⇒ Object

Complete items and optionally move them to history given an array of items. When force_history = 1, items should be moved to history (where 1 is true and 0 is false, and the default is 1) This is useful when checking off sub items.



39
40
41
42
43
# File 'lib/todoist/sync/items.rb', line 39

def complete(items, force_history=1)
  item_ids = items.collect { |item| item.id }   
  args = {ids: item_ids.to_json, force_history: force_history}
  return @client.api_helper.command(args, "item_complete")
end

#complete_recurring(item, new_date_utc = nil, date_string = nil, is_forward = 1) ⇒ Object

Complete a recurring item given the id of the recurring item.

This method also accepts as optional a new DateTime in UTC, a date string to reset the object to, and whether or not the item is to be completed or not using the is_forward flag.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/todoist/sync/items.rb', line 59

def complete_recurring(item, new_date_utc = nil, date_string = nil, 
  is_forward = 1)
  
  args = {id: item.id, is_forward: is_forward}
  if new_date_utc
     # Reformat DateTime to the following string:  YYYY-MM-DDTHH:MM
    args["new_date_utc"] = ParseHelper.format_time(new_date_utc)
  end
  
  if date_string
    args["date_string"] = date_string
  end
  
  return @client.api_helper.command(args, "item_update_date_complete")
end

#delete(items) ⇒ Object

Delete items given an array of items



22
23
24
25
26
# File 'lib/todoist/sync/items.rb', line 22

def delete(items)
  item_ids = items.collect { |item| item.id }   
  args = {ids: item_ids.to_json}
  return @client.api_helper.command(args, "item_delete")
end

#move(item, project) ⇒ Object

Move an item from one project to another project given an item and a project. Note that move requires a fully inflated item object because it uses the project id in the item object.



31
32
33
34
35
# File 'lib/todoist/sync/items.rb', line 31

def move(item, project)
  project_items = {item.project_id => [item.id]}
  args = {project_items: project_items, to_project: project.id}
  return @client.api_helper.command(args, "item_move")
end

#uncomplete(items) ⇒ Object

Uncomplete items and move them to the active projects given an array of items.



48
49
50
51
52
# File 'lib/todoist/sync/items.rb', line 48

def uncomplete(items)
  item_ids = items.collect { |item| item.id }   
  args = {ids: item_ids.to_json}
  return @client.api_helper.command(args, "item_uncomplete")
end

#update(args) ⇒ Object

Update item given a hash of attributes



17
18
19
# File 'lib/todoist/sync/items.rb', line 17

def update(args)
  return @client.api_helper.command(args, "item_update")
end

#update_day_orders(items) ⇒ Object

Update the day orders of multiple items at once given an array of items



86
87
88
89
90
91
92
93
# File 'lib/todoist/sync/items.rb', line 86

def update_day_orders(items)
  ids_to_orders = {}
  items.each do |item|
    ids_to_orders[item.id] = item.day_order
  end
  args = {ids_to_orders: ids_to_orders.to_json}
  return @client.api_helper.command(args, "item_update_day_orders")
end

#update_multiple_orders_and_indents(items) ⇒ Object

Update orders and indents for an array of items



96
97
98
99
100
101
102
103
# File 'lib/todoist/sync/items.rb', line 96

def update_multiple_orders_and_indents(items)
  tuples = {}
  items.each do |item|
    tuples[item.id] = [item.item_order, item.indent]
  end
  args = {ids_to_orders_indents: tuples.to_json}
  return @client.api_helper.command(args, "item_update_orders_indents")
end