todoist-ruby 0.1.3 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,14 +5,14 @@
5
5
 
6
6
  module Todoist
7
7
  module Misc
8
- class Query
8
+ class Query < Todoist::Service
9
9
  include Todoist::Util
10
10
 
11
11
  # Given an array of queries, return multiple results with key being the
12
12
  # query results. Query results have three key elements: query, type,
13
13
  # and data. Data is where the items are stored.
14
14
  def queries(queries)
15
- result = NetworkHelper.getResponse(Config::TODOIST_QUERY_COMMAND,
15
+ result = @client.api_helper.get_response(Config::TODOIST_QUERY_COMMAND,
16
16
  queries: queries.to_json)
17
17
  return ParseHelper.make_objects_as_hash(result, "query")
18
18
  end
@@ -1,12 +1,12 @@
1
1
  module Todoist
2
2
  module Misc
3
- class Quick
3
+ class Quick < Todoist::Service
4
4
  include Todoist::Util
5
5
 
6
6
  # Implementation of the Quick Add Task available in the official
7
7
  # clients.
8
8
  def add_item(text)
9
- result = NetworkHelper.getResponse(Config::TODOIST_QUICK_ADD_COMMAND, {text: text})
9
+ result = @client.api_helper.get_response(Config::TODOIST_QUICK_ADD_COMMAND, {text: text})
10
10
  return ParseHelper.make_object(result)
11
11
  end
12
12
 
@@ -1,6 +1,6 @@
1
1
  module Todoist
2
2
  module Misc
3
- class Templates
3
+ class Templates < Todoist::Service
4
4
  include Todoist::Util
5
5
 
6
6
  # Given a project and a File object (Ruby) imports the content onto the
@@ -8,21 +8,21 @@ module Todoist
8
8
  # suffix is CSV. Otherwise, the file will be parsed as one item per line
9
9
  # and ignore the formatting altogether.
10
10
  def import_into_project(project, file)
11
- multipart_file = NetworkHelper.multipart_file(file)
11
+ multipart_file = @client.api_helper.multipart_file(file)
12
12
  params = {project_id: project.id, file: multipart_file}
13
- NetworkHelper.getMultipartResponse(Config::TODOIST_TEMPLATES_IMPORT_INTO_PROJECT_COMMAND, params)
13
+ @client.api_helper.get_multipart_response(Config::TODOIST_TEMPLATES_IMPORT_INTO_PROJECT_COMMAND, params)
14
14
  end
15
15
 
16
16
  # Export the project as a CSV string
17
17
  def export_as_file(project)
18
18
  params = {project_id: project.id}
19
- NetworkHelper.getResponse(Config::TODOIST_TEMPLATES_EXPORT_AS_FILE_COMMAND, params)
19
+ @client.api_helper.get_response(Config::TODOIST_TEMPLATES_EXPORT_AS_FILE_COMMAND, params)
20
20
  end
21
21
 
22
22
  # Export the project as a url that can be accessed over HTTP
23
23
  def export_as_url(project)
24
24
  params = {project_id: project.id}
25
- NetworkHelper.getResponse(Config::TODOIST_TEMPLATES_EXPORT_AS_URL_COMMAND, params)
25
+ @client.api_helper.get_response(Config::TODOIST_TEMPLATES_EXPORT_AS_URL_COMMAND, params)
26
26
  end
27
27
  end
28
28
  end
@@ -1,13 +1,13 @@
1
1
  module Todoist
2
2
  module Misc
3
- class Uploads
3
+ class Uploads < Todoist::Service
4
4
  include Todoist::Util
5
5
 
6
6
  # Uploads a file given a Ruby File.
7
7
  def add(file)
8
- multipart_file = NetworkHelper.multipart_file(file)
8
+ multipart_file = @client.api_helper.multipart_file(file)
9
9
  params = {file_name: File.basename(file), file: multipart_file}
10
- NetworkHelper.getMultipartResponse(Config::TODOIST_UPLOADS_ADD_COMMAND, params)
10
+ @client.api_helper.get_multipart_response(Config::TODOIST_UPLOADS_ADD_COMMAND, params)
11
11
  end
12
12
 
13
13
  # Get uploads up to limit. If last_id is entered, then the results list
@@ -15,13 +15,13 @@ module Todoist
15
15
  def get(limit = 30, last_id = 0)
16
16
  params = {limit: limit}
17
17
  params["last_id"] = last_id if last_id
18
- NetworkHelper.getResponse(Config::TODOIST_UPLOADS_GET_COMMAND, params)
18
+ @client.api_helper.get_response(Config::TODOIST_UPLOADS_GET_COMMAND, params)
19
19
  end
20
20
 
21
21
  # Deletes an upload given a file URL.
22
22
  def delete(file_url)
23
23
  params = {file_url: file_url}
24
- NetworkHelper.getResponse(Config::TODOIST_UPLOADS_DELETE_COMMAND, params)
24
+ @client.api_helper.get_response(Config::TODOIST_UPLOADS_DELETE_COMMAND, params)
25
25
  end
26
26
  end
27
27
  end
@@ -0,0 +1,10 @@
1
+ module Todoist
2
+ class Service
3
+ include Todoist::Util
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ end
10
+ end
@@ -2,30 +2,30 @@
2
2
 
3
3
  module Todoist
4
4
  module Sync
5
- class Filters
5
+ class Filters < Todoist::Service
6
6
  include Todoist::Util
7
7
 
8
8
  # Return a Hash of filters where key is the id of a filter and value is a filter
9
9
  def collection
10
- return ApiHelper.collection("filters")
10
+ return @client.api_helper.collection("filters")
11
11
  end
12
12
 
13
13
  # Add a filter with a given hash of attributes and returns the filter id.
14
14
  # Please note that item_id is required as is a date as specific in the
15
15
  # documentation. This method can be tricky to all.
16
16
  def add(args)
17
- return ApiHelper.add(args, "filter_add")
17
+ return @client.api_helper.add(args, "filter_add")
18
18
  end
19
19
 
20
20
  # Update a filter given a hash of attributes
21
21
  def update(args)
22
- return ApiHelper.command(args, "filter_update")
22
+ return @client.api_helper.command(args, "filter_update")
23
23
  end
24
24
 
25
25
  # Delete filter given an array of filters
26
26
  def delete(filter)
27
27
  args = {id: filter.id}
28
- return ApiHelper.command(args, "filter_delete")
28
+ return @client.api_helper.command(args, "filter_delete")
29
29
  end
30
30
 
31
31
  # Update orders for an array of filters
@@ -35,7 +35,7 @@ module Todoist
35
35
  args[filter.id] = filter.item_order
36
36
  end
37
37
  args = {id_order_mapping: args.to_json}
38
- return ApiHelper.command(args, "filter_update_orders")
38
+ return @client.api_helper.command(args, "filter_update_orders")
39
39
  end
40
40
 
41
41
 
@@ -1,28 +1,28 @@
1
1
  module Todoist
2
2
  module Sync
3
- class Items
3
+ class Items < Todoist::Service
4
4
  include Todoist::Util
5
5
 
6
6
  # Return a Hash of items where key is the id of a item and value is a item
7
7
  def collection
8
- return ApiHelper.collection("items")
8
+ return @client.api_helper.collection("items")
9
9
  end
10
10
 
11
11
  # Add a item with a given hash of attributes and returns the item id
12
12
  def add(args)
13
- return ApiHelper.add(args, "item_add")
13
+ return @client.api_helper.add(args, "item_add")
14
14
  end
15
15
 
16
16
  # Update item given a hash of attributes
17
17
  def update(args)
18
- return ApiHelper.command(args, "item_update")
18
+ return @client.api_helper.command(args, "item_update")
19
19
  end
20
20
 
21
21
  # Delete items given an array of items
22
22
  def delete(items)
23
23
  item_ids = items.collect { |item| item.id }
24
24
  args = {ids: item_ids.to_json}
25
- return ApiHelper.command(args, "item_delete")
25
+ return @client.api_helper.command(args, "item_delete")
26
26
  end
27
27
 
28
28
  # Move an item from one project to another project given an item and a project.
@@ -31,7 +31,7 @@ module Todoist
31
31
  def move(item, project)
32
32
  project_items = {item.project_id => [item.id]}
33
33
  args = {project_items: project_items, to_project: project.id}
34
- return ApiHelper.command(args, "item_move")
34
+ return @client.api_helper.command(args, "item_move")
35
35
  end
36
36
 
37
37
  # Complete items and optionally move them to history given an array of items. When force_history = 1, items should be moved to history (where 1 is true and 0 is false, and the default is 1) This is useful when checking off sub items.
@@ -39,7 +39,7 @@ module Todoist
39
39
  def complete(items, force_history=1)
40
40
  item_ids = items.collect { |item| item.id }
41
41
  args = {ids: item_ids.to_json, force_history: force_history}
42
- return ApiHelper.command(args, "item_complete")
42
+ return @client.api_helper.command(args, "item_complete")
43
43
  end
44
44
 
45
45
  # Uncomplete items and move them to the active projects given an array
@@ -48,7 +48,7 @@ module Todoist
48
48
  def uncomplete(items)
49
49
  item_ids = items.collect { |item| item.id }
50
50
  args = {ids: item_ids.to_json}
51
- return ApiHelper.command(args, "item_uncomplete")
51
+ return @client.api_helper.command(args, "item_uncomplete")
52
52
  end
53
53
 
54
54
  # Complete a recurring item given the id of the recurring item.
@@ -62,14 +62,14 @@ module Todoist
62
62
  args = {id: item.id, is_forward: is_forward}
63
63
  if new_date_utc
64
64
  # Reformat DateTime to the following string: YYYY-MM-DDTHH:MM
65
- args["new_date_utc"] = ParseHelper.formatTime(new_date_utc)
65
+ args["new_date_utc"] = ParseHelper.format_time(new_date_utc)
66
66
  end
67
67
 
68
68
  if date_string
69
69
  args["date_string"] = date_string
70
70
  end
71
71
 
72
- return ApiHelper.command(args, "item_update_date_complete")
72
+ return @client.api_helper.command(args, "item_update_date_complete")
73
73
  end
74
74
 
75
75
  # A simplified version of item_complete / item_update_date_complete.
@@ -78,7 +78,7 @@ module Todoist
78
78
 
79
79
  def close(item)
80
80
  args = {id: item.id}
81
- return ApiHelper.command(args, "item_close")
81
+ return @client.api_helper.command(args, "item_close")
82
82
  end
83
83
 
84
84
  # Update the day orders of multiple items at once given an array of
@@ -89,7 +89,7 @@ module Todoist
89
89
  ids_to_orders[item.id] = item.day_order
90
90
  end
91
91
  args = {ids_to_orders: ids_to_orders.to_json}
92
- return ApiHelper.command(args, "item_update_day_orders")
92
+ return @client.api_helper.command(args, "item_update_day_orders")
93
93
  end
94
94
 
95
95
  # Update orders and indents for an array of items
@@ -99,7 +99,7 @@ module Todoist
99
99
  tuples[item.id] = [item.item_order, item.indent]
100
100
  end
101
101
  args = {ids_to_orders_indents: tuples.to_json}
102
- return ApiHelper.command(args, "item_update_orders_indents")
102
+ return @client.api_helper.command(args, "item_update_orders_indents")
103
103
  end
104
104
 
105
105
  end
@@ -1,27 +1,27 @@
1
1
  module Todoist
2
2
  module Sync
3
- class Labels
3
+ class Labels < Todoist::Service
4
4
  include Todoist::Util
5
5
 
6
6
  # Return a Hash of labels where key is the id of a label and value is a label
7
7
  def collection
8
- return ApiHelper.collection("labels")
8
+ return @client.api_helper.collection("labels")
9
9
  end
10
10
 
11
11
  # Add a label with a given hash of attributes and returns the label id
12
12
  def add(args)
13
- return ApiHelper.add(args, "label_add")
13
+ return @client.api_helper.add(args, "label_add")
14
14
  end
15
15
 
16
16
  # Update label given a hash of attributes
17
17
  def update(args)
18
- return ApiHelper.command(args, "label_update")
18
+ return @client.api_helper.command(args, "label_update")
19
19
  end
20
20
 
21
21
  # Delete a label given a label
22
22
  def delete(label)
23
23
  args = {id: label.id}
24
- return ApiHelper.command(args, "label_delete")
24
+ return @client.api_helper.command(args, "label_delete")
25
25
  end
26
26
 
27
27
  # Update orders for an array of labels
@@ -31,7 +31,7 @@ module Todoist
31
31
  args[label.id] = label.item_order
32
32
  end
33
33
  args = {id_order_mapping: args.to_json}
34
- return ApiHelper.command(args, "label_update_orders")
34
+ return @client.api_helper.command(args, "label_update_orders")
35
35
  end
36
36
 
37
37
  end
@@ -1,29 +1,29 @@
1
1
  module Todoist
2
2
  module Sync
3
- class Notes
3
+ class Notes < Todoist::Service
4
4
  include Todoist::Util
5
5
 
6
6
  # Return a Hash of notes where key is the id of a note and value is a note
7
7
  def collection
8
- return ApiHelper.collection("notes")
8
+ return @client.api_helper.collection("notes")
9
9
  end
10
10
 
11
11
  # Add a note with a given hash of attributes and returns the note id.
12
12
  # Please note that item_id or project_id key is required. In addition,
13
13
  # content is also a required key in the hash.
14
14
  def add(args)
15
- return ApiHelper.add(args, "note_add")
15
+ return @client.api_helper.add(args, "note_add")
16
16
  end
17
17
 
18
18
  # Update a note given a hash of attributes
19
19
  def update(args)
20
- return ApiHelper.command(args, "note_update")
20
+ return @client.api_helper.command(args, "note_update")
21
21
  end
22
22
 
23
23
  # Delete notes given an a note
24
24
  def delete(note)
25
25
  args = {id: note.id}
26
- return ApiHelper.command(args, "note_delete")
26
+ return @client.api_helper.command(args, "note_delete")
27
27
  end
28
28
 
29
29
 
@@ -2,43 +2,43 @@ module Todoist
2
2
  module Sync
3
3
 
4
4
 
5
- class Projects
5
+ class Projects < Todoist::Service
6
6
  include Todoist::Util
7
7
 
8
8
  # Return a Hash of projects where key is the id of a project and value is a project
9
9
  def collection
10
- return ApiHelper.collection("projects")
10
+ return @client.api_helper.collection("projects")
11
11
  end
12
12
 
13
13
  # Add a project with a given hash of attributes and returns the project id
14
14
  def add(args)
15
- return ApiHelper.add(args, "project_add")
15
+ return @client.api_helper.add(args, "project_add")
16
16
  end
17
17
 
18
18
  # Delete projects given an array of projects
19
19
  def delete(projects)
20
20
  project_ids = projects.collect { |project| project.id }
21
21
  args = {ids: project_ids.to_json}
22
- return ApiHelper.command(args, "project_delete")
22
+ return @client.api_helper.command(args, "project_delete")
23
23
  end
24
24
 
25
25
  # Archive projects given an array of projects
26
26
  def archive(projects)
27
27
  project_ids = projects.collect { |project| project.id }
28
28
  args = {ids: project_ids.to_json}
29
- return ApiHelper.command(args, "project_archive")
29
+ return @client.api_helper.command(args, "project_archive")
30
30
  end
31
31
 
32
32
  # Unarchive projects given an array of projects
33
33
  def unarchive(projects)
34
34
  project_ids = projects.collect { |project| project.id }
35
35
  args = {ids: project_ids.to_json}
36
- return ApiHelper.command(args, "project_unarchive")
36
+ return @client.api_helper.command(args, "project_unarchive")
37
37
  end
38
38
 
39
39
  # Update project given a hash of attributes
40
40
  def update(args)
41
- return ApiHelper.command(args, "project_update")
41
+ return @client.api_helper.command(args, "project_update")
42
42
  end
43
43
 
44
44
  # Update orders and indents for an array of projects
@@ -48,7 +48,7 @@ module Todoist
48
48
  tuples[project.id] = [project.item_order, project.indent]
49
49
  end
50
50
  args = {ids_to_orders_indents: tuples.to_json}
51
- return ApiHelper.command(args, "project_update_orders_indents")
51
+ return @client.api_helper.command(args, "project_update_orders_indents")
52
52
  end
53
53
 
54
54
  end
@@ -1,35 +1,35 @@
1
1
  module Todoist
2
2
  module Sync
3
- class Reminders
3
+ class Reminders < Todoist::Service
4
4
  include Todoist::Util
5
5
 
6
6
  # Return a Hash of reminders where key is the id of a reminder and value is a reminder
7
7
  def collection
8
- return ApiHelper.collection("reminders")
8
+ return @client.api_helper.collection("reminders")
9
9
  end
10
10
 
11
11
  # Add a reminder with a given hash of attributes and returns the reminder id.
12
12
  # Please note that item_id is required as is a date as specific in the
13
13
  # documentation. This method can be tricky to all.
14
14
  def add(args)
15
- return ApiHelper.add(args, "reminder_add")
15
+ return @client.api_helper.add(args, "reminder_add")
16
16
  end
17
17
 
18
18
  # Update a reminder given a hash of attributes
19
19
  def update(args)
20
- return ApiHelper.command(args, "reminder_update")
20
+ return @client.api_helper.command(args, "reminder_update")
21
21
  end
22
22
 
23
23
  # Delete reminder given an array of reminders
24
24
  def delete(reminder)
25
25
  args = {id: reminder.id}
26
- return ApiHelper.command(args, "reminder_delete")
26
+ return @client.api_helper.command(args, "reminder_delete")
27
27
  end
28
28
 
29
29
  # Clear locations which is used for location reminders
30
30
  def clear_locations
31
31
  args = {}
32
- return ApiHelper.command(args, "clear_locations")
32
+ return @client.api_helper.command(args, "clear_locations")
33
33
  end
34
34
  end
35
35
  end
@@ -1,10 +1,9 @@
1
1
  require "net/http"
2
2
  require "json"
3
- require "todoist/util/config"
3
+ require "todoist/config"
4
4
  require "todoist/util/network_helper"
5
5
  require "todoist/util/parse_helper"
6
6
  require "todoist/util/uuid"
7
- require "todoist/util/command_synchronizer"
8
7
  require "ostruct"
9
8
  require 'concurrent'
10
9
 
@@ -15,18 +14,21 @@ module Todoist
15
14
  module Util
16
15
 
17
16
  class ApiHelper
18
-
19
- @@object_cache = {"projects" => Concurrent::Hash.new({}), "labels" => Concurrent::Hash.new({}),
17
+ def initialize(client)
18
+ @client = client
19
+ @object_cache = {"projects" => Concurrent::Hash.new({}), "labels" => Concurrent::Hash.new({}),
20
20
  "items" => Concurrent::Hash.new({}), "notes" => Concurrent::Hash.new({}),
21
21
  "reminders" => Concurrent::Hash.new({}), "filters" => Concurrent::Hash.new({})
22
- }
23
- @@sync_token_cache = Concurrent::Hash.new({"projects" => "*", "labels" => "*",
24
- "items" => "*", "notes" => "*", "reminders" => "*", "filters" => "*"})
25
-
26
- def self.collection(type)
27
- CommandSynchronizer.sync
22
+ }
23
+ @sync_token_cache = Concurrent::Hash.new({"projects" => "*", "labels" => "*",
24
+ "items" => "*", "notes" => "*", "reminders" => "*", "filters" => "*"})
25
+ @network_helper = NetworkHelper.new(client)
26
+ end
27
+
28
+ def collection(type)
29
+ @network_helper.sync
28
30
 
29
- response = getSyncResponse({sync_token: sync_token(type), resource_types: "[\"#{type}\"]"})
31
+ response = @network_helper.get_sync_response({sync_token: sync_token(type), resource_types: "[\"#{type}\"]"})
30
32
  response[type].each do |object_data|
31
33
  object = OpenStruct.new(object_data)
32
34
  objects(type)[object.id] = object
@@ -35,23 +37,24 @@ module Todoist
35
37
  return objects(type)
36
38
  end
37
39
 
38
- def self.exec(args, command, temporary_resource_id)
40
+ def exec(args, command, temporary_resource_id)
39
41
  command_uuid = Uuid.command_uuid
40
42
  commands = {type: command, temp_id: temporary_resource_id, uuid: command_uuid, args: args}
41
- response = getSyncResponse({commands: "[#{commands.to_json}]"})
43
+ response = @network_helper.get_sync_response({commands: "[#{commands.to_json}]"})
42
44
  raise RuntimeError, "Response returned is not ok" unless response["sync_status"][command_uuid] == "ok"
43
45
  return response
44
46
  end
45
47
 
46
- def self.command(args, command)
48
+ def command(args, command)
47
49
  temporary_resource_id = Uuid.temporary_resource_id
48
50
  command_uuid = Uuid.command_uuid
49
51
  command = {type: command, temp_id: temporary_resource_id, uuid: command_uuid, args: args}
50
- CommandSynchronizer.queue(command)
52
+
53
+ @network_helper.queue(command)
51
54
  return true
52
55
  end
53
56
 
54
- def self.add(args, command)
57
+ def add(args, command)
55
58
  temporary_resource_id = Uuid.temporary_resource_id
56
59
  command_uuid = Uuid.command_uuid
57
60
  command = {type: command, temp_id: temporary_resource_id, uuid: command_uuid, args: args}
@@ -60,28 +63,38 @@ module Todoist
60
63
  object.id = temp_id_mappings[temporary_resource_id] if temp_id_mappings[temporary_resource_id]
61
64
  end
62
65
 
63
- CommandSynchronizer.queue(command, temp_id_callback)
66
+ @network_helper.queue(command, temp_id_callback)
64
67
  return object
65
68
  end
66
69
 
67
- def self.getSyncResponse(params)
68
- NetworkHelper.getResponse(Config::TODOIST_SYNC_COMMAND, params)
70
+ def get_response(command, params = {}, token = true)
71
+ @network_helper.get_response(command, params, token)
69
72
  end
70
73
 
71
- protected
74
+ def get_multipart_response(command, params)
75
+ @network_helper.get_multipart_response(command, params)
76
+ end
72
77
 
78
+ def multipart_file(file)
79
+ @network_helper.multipart_file(file)
80
+ end
73
81
 
82
+ def sync
83
+ @network_helper.sync
84
+ end
85
+
86
+ protected
74
87
 
75
- def self.objects(type)
76
- @@object_cache[type]
88
+ def objects(type)
89
+ @object_cache[type]
77
90
  end
78
91
 
79
- def self.sync_token(type)
80
- @@sync_token_cache[type]
92
+ def sync_token(type)
93
+ @sync_token_cache[type]
81
94
  end
82
95
 
83
- def self.set_sync_token(type, value)
84
- @@sync_token_cache[type] = value
96
+ def set_sync_token(type, value)
97
+ @sync_token_cache[type] = value
85
98
  end
86
99
 
87
100
  end