class Nin::Integration::Synchronizer::Todoist

Public Instance Methods

sync(op, params = {}) click to toggle source
# File lib/nin/integration/synchronizer/todoist.rb, line 5
def sync(op, params = {})
  @service = Integration::Service::Todoist.new(@client)

  case op
  when :read
    sync_read(params)
  when :add
    sync_add(params)
  when :edit
    sync_edit(params)
  when :edit_completed
    sync_edit(params, :checked)
  when :delete
    sync_delete(params)
  end
end

Private Instance Methods

_get_item_write_payload(item) click to toggle source
# File lib/nin/integration/synchronizer/todoist.rb, line 80
def _get_item_write_payload(item)
  payload = {
    content: item.desc,
    due: { date: item.date },
    checked: item.completed
  }

  if project_name = item.tags.first
    @projects      ||= @service.projects.all
    @project_names ||= @projects.values

    _new_project = false
    project_id = unless @project_names.include?(project_name)
                   @service.projects.add(name: project_name)
                   _new_project = true
                 else
                   @projects.find { |k, v| v == project_name }.first
                 end

    if _new_project
      @projects[project_id] = project_name
      @project_names.push(project_name)
    end

    payload[:project_id]  = project_id
  end

  payload
end
sync_add(params) click to toggle source
# File lib/nin/integration/synchronizer/todoist.rb, line 52
def sync_add(params)
  item = params.fetch(:item)

  payload = _get_item_write_payload(item)

  uid = @service.items.add(payload)
  item.uid = uid
end
sync_delete(params) click to toggle source
# File lib/nin/integration/synchronizer/todoist.rb, line 76
def sync_delete(params)
  @service.items.delete(params.fetch(:ids))
end
sync_edit(params, type = :full) click to toggle source
# File lib/nin/integration/synchronizer/todoist.rb, line 61
def sync_edit(params, type = :full)
  payload = params.fetch(:items).ensure_array.map do |item|
    item_payload = if type == :checked
                     { checked: item.completed }
                   else
                     _get_item_write_payload(item)
                   end
    item_payload[:id] = item.uid

    item_payload
  end

  @service.items.update(payload)
end
sync_read(params) click to toggle source
# File lib/nin/integration/synchronizer/todoist.rb, line 24
def sync_read(params)
  items   = params.fetch(:items)
  next_id = params.fetch(:next_id)

  id = next_id
  synced_uids = @service.items.all.map do |t|
    item = Item.new(id, *t)

    if existing_item = items.find_by(:uid, item.uid)
      existing_item.edit(item.desc, item.date, item.tags, item.completed)
    else
      items << item
      id += 1
    end

    item.uid
  end

  unsynced_uids = items.where(:uid) { |item_uid| !item_uid.nil? }.map(&:uid) - synced_uids
  unsynced_uids.each do |uid|
    item = items.find_by(:uid, uid)
    t    = @client.items.get(uid)

    items.delete(item) and next if t.nil? || t.fetch("is_deleted") == 1
    item.completed = (t.fetch("checked") == 1)
  end
end