Class: Checkoff::TaskSearches

Inherits:
Object
  • Object
show all
Extended by:
CacheMethod::ClassMethods
Includes:
Asana::Resources::ResponseHelper, Logging
Defined in:
lib/checkoff/task_searches.rb,
sig/checkoff.rbs

Overview

Run task searches against the Asana API

Constant Summary collapse

MINUTE =

Returns:

  • (Object)
60
HOUR =

Returns:

  • (Object)
MINUTE * 60
DAY =

Returns:

  • (Object)
24 * HOUR
REALLY_LONG_CACHE_TIME =

Returns:

  • (Object)
HOUR * 1
LONG_CACHE_TIME =

Returns:

  • (Object)
MINUTE * 15
SHORT_CACHE_TIME =

Returns:

  • (Object)
MINUTE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Checkoff::Internal::ConfigLoader.load(:asana), workspaces: Checkoff::Workspaces.new(config:), task_selectors: Checkoff::TaskSelectors.new(config:), projects: Checkoff::Projects.new(config:), clients: Checkoff::Clients.new(config:), client: clients.client, search_url_parser: Checkoff::Internal::SearchUrl::Parser.new, asana_resources_collection_class: Asana::Resources::Collection) ⇒ Object

sord warn - Asana::Client wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Collection wasn't able to be resolved to a constant in this project @param config

@param workspaces

@param task_selectors

@param projects

@param clients

@param client

@param search_url_parser

@param asana_resources_collection_class



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/checkoff/task_searches.rb', line 44

def initialize(config: Checkoff::Internal::ConfigLoader.load(:asana),
               workspaces: Checkoff::Workspaces.new(config:),
               task_selectors: Checkoff::TaskSelectors.new(config:),
               projects: Checkoff::Projects.new(config:),
               clients: Checkoff::Clients.new(config:),
               client: clients.client,
               search_url_parser: Checkoff::Internal::SearchUrl::Parser.new,
               asana_resources_collection_class: Asana::Resources::Collection)
  @workspaces = workspaces
  @task_selectors = task_selectors
  @projects = projects
  @client = client
  @search_url_parser = search_url_parser
  @asana_resources_collection_class = asana_resources_collection_class
end

Instance Attribute Details

#clientAsana::Client (readonly)

sord warn - Asana::Client wasn't able to be resolved to a constant in this project

Returns:

  • (Asana::Client)


220
221
222
# File 'lib/checkoff/task_searches.rb', line 220

def client
  @client
end

#projectsCheckoff::Projects (readonly)

Returns:



216
217
218
# File 'lib/checkoff/task_searches.rb', line 216

def projects
  @projects
end

#task_selectorsCheckoff::TaskSelectors (readonly)



214
215
216
# File 'lib/checkoff/task_searches.rb', line 214

def task_selectors
  @task_selectors
end

#workspacesCheckoff::Workspaces (readonly)



218
219
220
# File 'lib/checkoff/task_searches.rb', line 218

def workspaces
  @workspaces
end

Class Method Details

.runvoid

This method returns an undefined value.



201
202
203
204
205
206
207
208
209
# File 'lib/checkoff/task_searches.rb', line 201

def run
  # @type [String]
  workspace_name = ARGV[0] || raise('Please pass workspace name as first argument')
  # @type [String]
  url = ARGV[1] || raise('Please pass task search URL as second argument')
  task_searches = Checkoff::TaskSearches.new
  task_search = task_searches.task_search(workspace_name, url)
  puts "Results: #{task_search}"
end

Instance Method Details

#api_task_search_request(api_params, workspace_gid:, extra_fields:) ⇒ ::Enumerable[Asana::Resources::Task]

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project Perform a search using the Asana Task Search API:

https://developers.asana.com/reference/searchtasksforworkspace

@param api_params

@param workspace_gid

@param extra_fields

Parameters:

  • api_params (::Hash[String, Object])
  • workspace_gid: (String)
  • extra_fields: (::Array[String])

Returns:

  • (::Enumerable[Asana::Resources::Task])


125
126
127
128
129
130
131
132
133
# File 'lib/checkoff/task_searches.rb', line 125

def api_task_search_request(api_params, workspace_gid:, extra_fields:)
  path = "/workspaces/#{workspace_gid}/tasks/search"
  options = calculate_api_options(extra_fields)
  @asana_resources_collection_class.new(parse(client.get(path,
                                                         params: api_params,
                                                         options:)),
                                        type: Asana::Resources::Task,
                                        client:)
end

#as_cache_keyHash

Returns:

  • (Hash)


110
111
112
# File 'lib/checkoff/task_searches.rb', line 110

def as_cache_key
  {}
end

#calculate_api_options(extra_fields) ⇒ ::Hash[Symbol, untyped]

@sg-ignore

@param extra_fields

Parameters:

  • extra_fields (::Array[String])

Returns:

  • (::Hash[Symbol, untyped])


191
192
193
194
195
# File 'lib/checkoff/task_searches.rb', line 191

def calculate_api_options(extra_fields)
  # @type [Hash{Symbol => undefined}]
  all_options = projects.task_options(extra_fields: ['custom_fields'] + extra_fields)
  all_options[:options]
end

#debugvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1920
# File 'sig/checkoff.rbs', line 1920

def debug: (?Object? message) -> void

#errorvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1911
# File 'sig/checkoff.rbs', line 1911

def error: (?Object? message) -> void

#finervoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1923
# File 'sig/checkoff.rbs', line 1923

def finer: (?Object? message) -> void

#infovoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1917
# File 'sig/checkoff.rbs', line 1917

def info: (?Object? message) -> void

#iterated_raw_task_search(api_params, workspace_gid:, extra_fields:) ⇒ ::Enumerable[Asana::Resources::Task]

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project Perform a search using the Asana Task Search API and use manual pagination to ensure all results are returned:

https://developers.asana.com/reference/searchtasksforworkspace

"However, you can paginate manually by sorting the search
results by their creation time and then modifying each
subsequent query to exclude data you have already seen." -
see sort_by field at
https://developers.asana.com/reference/searchtasksforworkspace

@param api_params

@param workspace_gid

@param url

@param extra_fields

Parameters:

  • api_params (::Hash[String, Object])
  • workspace_gid: (String)
  • extra_fields: (::Array[String])

Returns:

  • (::Enumerable[Asana::Resources::Task])


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/checkoff/task_searches.rb', line 152

def iterated_raw_task_search(api_params, workspace_gid:, extra_fields:)
  # https://developers.asana.com/reference/searchtasksforworkspace
  tasks = []
  new_api_params = api_params.dup
  original_sort_by = new_api_params.delete('sort_by')
  # defaults to false
  original_sort_ascending = new_api_params.delete('sort_ascending')
  original_created_at_before = new_api_params.delete('created_at.before')
  raise 'Teach me how to handle original_created_at_before' unless original_created_at_before.nil?

  new_api_params['sort_by'] = 'created_at'

  Kernel.loop do
    # Get the most recently created results, then iterate on until we're out of results

    # @type [Array<Asana::Resources::Task>]
    task_batch = raw_task_search(new_api_params,
                                 workspace_gid:, extra_fields: extra_fields + ['created_at'],
                                 fetch_all: false).to_a
    oldest = task_batch.to_a.last

    break if oldest.nil?

    new_api_params['created_at.before'] = oldest.created_at

    tasks.concat(task_batch.to_a)
  end
  unless original_sort_by.nil? || original_sort_by == 'created_at'
    raise "Teach me how to handle original_sort_by: #{original_sort_by.inspect}"
  end

  raise 'Teach me how to handle original_sort_ascending' unless original_sort_ascending.nil?

  tasks
end

#log_levelSymbol

@sg-ignore

Returns:

  • (Symbol)


1926
# File 'sig/checkoff.rbs', line 1926

def log_level: () -> Symbol

#logger::Logger

Returns:

  • (::Logger)


1908
# File 'sig/checkoff.rbs', line 1908

def logger: () -> ::Logger

#raw_task_search(api_params, workspace_gid:, extra_fields: [], task_selector: [], fetch_all: true) ⇒ Object

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project Perform a search using the Asana Task Search API:

https://developers.asana.com/reference/searchtasksforworkspace

@param api_params

@param workspace_gid

@param extra_fields

@param task_selector

@param fetch_all — Ensure all results are provided by manually paginating



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/checkoff/task_searches.rb', line 93

def raw_task_search(api_params,
                    workspace_gid:, extra_fields: [], task_selector: [],
                    fetch_all: true)
  tasks = api_task_search_request(api_params, workspace_gid:, extra_fields:)

  tasks = iterated_raw_task_search(api_params, workspace_gid:, extra_fields:) if fetch_all && tasks.count == 100

  debug { "#{tasks.count} raw tasks returned" }

  return tasks if task_selector.empty?

  tasks.select do |task|
    task_selectors.filter_via_task_selector(task, task_selector)
  end
end

#task_search(workspace_name, url, extra_fields: []) ⇒ ::Enumerable[Asana::Resources::Task]

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project Perform an equivalent search API to an Asana search URL in the web UI. Not all URL parameters are supported; each one must be added here manually. In addition, not all are supported in the Asana API in a compatible way, so they may result in more tasks being fetched than actually returned as filtering is done manually.

@param workspace_name

@param url

@param extra_fields

Parameters:

  • workspace_name (String)
  • url (String)
  • extra_fields: (::Array[String]) (defaults to: [])

Returns:

  • (::Enumerable[Asana::Resources::Task])


72
73
74
75
76
77
78
79
# File 'lib/checkoff/task_searches.rb', line 72

def task_search(workspace_name, url, extra_fields: [])
  workspace = workspaces.workspace_or_raise(workspace_name)
  api_params, task_selector = @search_url_parser.convert_params(url)
  debug { "Task search params: api_params=#{api_params}, task_selector=#{task_selector}" }
  # @sg-ignore
  raw_task_search(api_params, workspace_gid: workspace.gid, task_selector:,
                              extra_fields:)
end

#warnvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1914
# File 'sig/checkoff.rbs', line 1914

def warn: (?Object? message) -> void