Class: Tickrb::Client

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tickrb/client.rb

Constant Summary collapse

BASE_URL =
"https://api.ticktick.com/open/v1"

Instance Method Summary collapse

Constructor Details

#initialize(token: nil) ⇒ Client

Returns a new instance of Client.

Raises:



16
17
18
19
20
21
22
# File 'lib/tickrb/client.rb', line 16

def initialize(token: nil)
  @token = token || TokenStore.load_token
  raise Error, "No authentication token available. Please run authentication first." unless @token
  @projects_cache = T.let(nil, T.nilable(T::Array[T::Hash[String, T.untyped]]))
  @tasks_cache = T.let(nil, T.nilable(T::Array[T::Hash[String, T.untyped]]))
  @cache_timestamp = T.let(nil, T.nilable(Time))
end

Instance Method Details

#complete_task(task_id, project_id) ⇒ Object



66
67
68
69
70
# File 'lib/tickrb/client.rb', line 66

def complete_task(task_id, project_id)
  result = T.cast(make_request("POST", "/project/#{project_id}/task/#{task_id}/complete"), T::Hash[String, T.untyped])
  invalidate_cache
  result
end

#create_task(title:, content: nil, project_id: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tickrb/client.rb', line 53

def create_task(title:, content: nil, project_id: nil)
  task_data = {
    title: title,
    content: content,
    projectId: project_id
  }.compact

  result = T.cast(make_request("POST", "/task", task_data), T::Hash[String, T.untyped])
  invalidate_cache
  result
end

#delete_task(task_id, project_id) ⇒ Object



73
74
75
76
77
# File 'lib/tickrb/client.rb', line 73

def delete_task(task_id, project_id)
  result = T.cast(make_request("DELETE", "/project/#{project_id}/task/#{task_id}"), T::Hash[String, T.untyped])
  invalidate_cache
  result
end

#get_projectsObject



80
81
82
83
84
85
86
87
# File 'lib/tickrb/client.rb', line 80

def get_projects
  return @projects_cache if cache_valid? && @projects_cache

  response = make_request("GET", "/project")
  @projects_cache = response.is_a?(Array) ? response : []
  @cache_timestamp = Time.now.utc
  @projects_cache
end

#get_tasksObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tickrb/client.rb', line 25

def get_tasks
  return @tasks_cache if cache_valid? && @tasks_cache

  all_tasks = []
  projects = get_projects

  projects.each do |project|
    project_tasks = get_tasks_for_project(project["id"])
    project_tasks.each { |task| task["projectId"] = project["id"] }
    all_tasks.concat(project_tasks)
  end

  @tasks_cache = all_tasks
  @cache_timestamp = Time.now.utc
  all_tasks
end

#get_tasks_for_project(project_id) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/tickrb/client.rb', line 43

def get_tasks_for_project(project_id)
  response = make_request("GET", "/project/#{project_id}/data")
  if response.is_a?(Hash) && response["tasks"]
    response["tasks"]
  else
    []
  end
end