Class: Todoist::Util::ApiHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/todoist/util/api_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ ApiHelper

Returns a new instance of ApiHelper.



17
18
19
20
21
22
23
24
25
26
# 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

Instance Method Details

#add(args, command) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



48
49
50
51
52
53
54
55
# 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) ⇒ Object

Raises:

  • (RuntimeError)


40
41
42
43
44
45
46
# 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) ⇒ Object



74
75
76
# 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) ⇒ Object



70
71
72
# 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) ⇒ Object



78
79
80
# File 'lib/todoist/util/api_helper.rb', line 78

def multipart_file(file) 
  @network_helper.multipart_file(file)
end

#syncObject



82
83
84
# File 'lib/todoist/util/api_helper.rb', line 82

def sync
  @network_helper.sync  
end