Class: Nin::Integration::Service::Todoist::Item

Inherits:
Nin::Integration::Service show all
Defined in:
lib/nin/integration/service/todoist.rb

Instance Method Summary collapse

Methods inherited from Nin::Integration::Service

#initialize

Constructor Details

This class inherits a constructor from Nin::Integration::Service

Instance Method Details

#add(item) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nin/integration/service/todoist.rb', line 54

def add(item)
  commands = [
    {
      "type": "item_add",
      "temp_id": SecureRandom.uuid,
      "uuid": SecureRandom.uuid,
      "args": item
    }
  ].to_json

  @client.sync.write_resources(commands).fetch('temp_id_mapping').values.first
end

#allObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nin/integration/service/todoist.rb', line 36

def all
  data = @client.sync.read_resources(['projects', 'items'])

  projects = data.fetch('projects').reduce({}) do |projects, p|
    projects.update(p["id"] => p["name"])
  end

  data.fetch('items').reduce([]) do |tasks, t|
    tasks << [
      t["content"],
      (t["due"] || {}).fetch('date', nil),
      projects.fetch(t["project_id"]),
      t["id"],
      (t["checked"].to_i == 1)
    ]
  end
end

#delete(ids) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/nin/integration/service/todoist.rb', line 79

def delete(ids)
  commands = ids.ensure_array.map do |id|
    {
      "type": "item_delete",
      "uuid": SecureRandom.uuid,
      "args": { 'id': id }
    }
  end.to_json

  @client.sync.write_resources(commands)
end

#update(items) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/nin/integration/service/todoist.rb', line 67

def update(items)
  commands = items.ensure_array.map do |item|
    {
      "type": "item_update",
      "uuid": SecureRandom.uuid,
      "args": item
    }
  end.to_json

  @client.sync.write_resources(commands)
end