class Todoist::Util::ApiHelper

Public Class Methods

new(client) click to toggle source
# File lib/todoist/util/api_helper.rb, line 17
def initialize(client)
  @client = client
  @object_cache = {"projects" => Concurrent::Hash.new({}), "labels" => Concurrent::Hash.new({}), 
  "items" => Concurrent::Hash.new({}), "notes" => Concurrent::Hash.new({}),
  "reminders" => Concurrent::Hash.new({}), "filters" => Concurrent::Hash.new({})
  }
  @sync_token_cache = Concurrent::Hash.new({"projects" => "*", "labels" => "*", 
    "items" => "*", "notes" => "*", "reminders" => "*", "filters" => "*"})
  @network_helper = NetworkHelper.new(client)
end

Public Instance Methods

add(args, command) click to toggle source
# File lib/todoist/util/api_helper.rb, line 57
def add(args, command)
  temporary_resource_id = Uuid.temporary_resource_id
  command_uuid = Uuid.command_uuid
  command = {type: command, temp_id: temporary_resource_id, uuid: command_uuid, args: args}
  object = OpenStruct.new({temp_id: temporary_resource_id, id: temporary_resource_id})
  temp_id_callback = Proc.new do |temp_id_mappings|
      object.id = temp_id_mappings[temporary_resource_id] if temp_id_mappings[temporary_resource_id]
  end
  
  @network_helper.queue(command, temp_id_callback)
  return object
end
collection(type) click to toggle source
# File lib/todoist/util/api_helper.rb, line 28
def collection(type)
  @network_helper.sync
  
  response = @network_helper.get_sync_response({sync_token: sync_token(type), resource_types: "[\"#{type}\"]"})
  response[type].each do |object_data|
     object = OpenStruct.new(object_data)
     objects(type)[object.id] = object
  end
  set_sync_token(type, response["sync_token"])
  return objects(type)
end
command(args, command) click to toggle source
# File lib/todoist/util/api_helper.rb, line 48
def command(args, command)
  temporary_resource_id = Uuid.temporary_resource_id
  command_uuid = Uuid.command_uuid
  command = {type: command, temp_id: temporary_resource_id, uuid: command_uuid, args: args}
  
  @network_helper.queue(command)
  return true
end
exec(args, command, temporary_resource_id) click to toggle source
# File lib/todoist/util/api_helper.rb, line 40
def exec(args, command, temporary_resource_id)
  command_uuid = Uuid.command_uuid
  commands = {type: command, temp_id: temporary_resource_id, uuid: command_uuid, args: args}
  response = @network_helper.get_sync_response({commands: "[#{commands.to_json}]"})
  raise RuntimeError, "Response returned is not ok" unless response["sync_status"][command_uuid] == "ok"
  return response
end
get_multipart_response(command, params) click to toggle source
# File lib/todoist/util/api_helper.rb, line 74
def get_multipart_response(command, params)
  @network_helper.get_multipart_response(command, params)
end
get_response(command, params = {}, token = true) click to toggle source
# File lib/todoist/util/api_helper.rb, line 70
def get_response(command, params = {}, token = true)
  @network_helper.get_response(command, params, token)
end
multipart_file(file) click to toggle source
# File lib/todoist/util/api_helper.rb, line 78
def multipart_file(file) 
  @network_helper.multipart_file(file)
end
sync() click to toggle source
# File lib/todoist/util/api_helper.rb, line 82
def sync
  @network_helper.sync  
end

Protected Instance Methods

objects(type) click to toggle source
# File lib/todoist/util/api_helper.rb, line 88
def objects(type)
  @object_cache[type]
end
set_sync_token(type, value) click to toggle source
# File lib/todoist/util/api_helper.rb, line 96
def set_sync_token(type, value)
  @sync_token_cache[type] = value
end
sync_token(type) click to toggle source
# File lib/todoist/util/api_helper.rb, line 92
def sync_token(type)
  @sync_token_cache[type]
end