lib/todoist/util/api_helper.rb in todoist-ruby-0.1.3 vs lib/todoist/util/api_helper.rb in todoist-ruby-0.2.1

- old
+ new

@@ -1,88 +1,101 @@ require "net/http" require "json" -require "todoist/util/config" +require "todoist/config" require "todoist/util/network_helper" require "todoist/util/parse_helper" require "todoist/util/uuid" -require "todoist/util/command_synchronizer" require "ostruct" require 'concurrent' module Todoist module Util class ApiHelper - - @@object_cache = {"projects" => Concurrent::Hash.new({}), "labels" => Concurrent::Hash.new({}), + 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" => "*"}) - - def self.collection(type) - CommandSynchronizer.sync + } + @sync_token_cache = Concurrent::Hash.new({"projects" => "*", "labels" => "*", + "items" => "*", "notes" => "*", "reminders" => "*", "filters" => "*"}) + @network_helper = NetworkHelper.new(client) + end + + def collection(type) + @network_helper.sync - response = getSyncResponse({sync_token: sync_token(type), resource_types: "[\"#{type}\"]"}) + 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 - def self.exec(args, command, temporary_resource_id) + 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 = getSyncResponse({commands: "[#{commands.to_json}]"}) + 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 - def self.command(args, command) + 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} - CommandSynchronizer.queue(command) + + @network_helper.queue(command) return true end - def self.add(args, command) + 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 - CommandSynchronizer.queue(command, temp_id_callback) + @network_helper.queue(command, temp_id_callback) return object end - def self.getSyncResponse(params) - NetworkHelper.getResponse(Config::TODOIST_SYNC_COMMAND, params) + def get_response(command, params = {}, token = true) + @network_helper.get_response(command, params, token) end - protected + def get_multipart_response(command, params) + @network_helper.get_multipart_response(command, params) + end + def multipart_file(file) + @network_helper.multipart_file(file) + end + def sync + @network_helper.sync + end + + protected - def self.objects(type) - @@object_cache[type] + def objects(type) + @object_cache[type] end - def self.sync_token(type) - @@sync_token_cache[type] + def sync_token(type) + @sync_token_cache[type] end - def self.set_sync_token(type, value) - @@sync_token_cache[type] = value + def set_sync_token(type, value) + @sync_token_cache[type] = value end end end end