Class: Checkoff::TaskSearches

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

Overview

Run task searches against the Asana API

Constant Summary collapse

MINUTE =
60
HOUR =
MINUTE * 60
DAY =
24 * HOUR
REALLY_LONG_CACHE_TIME =
HOUR * 1
LONG_CACHE_TIME =
MINUTE * 15
SHORT_CACHE_TIME =
MINUTE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #error, #finer, #info, #logger, #warn

Constructor Details

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

Returns a new instance of TaskSearches.

Parameters:

  • config (Hash<Symbol, Object>) (defaults to: Checkoff::Internal::ConfigLoader.load(:asana))
  • workspaces (Checkoff::Workspaces) (defaults to: Checkoff::Workspaces.new(config: config))
  • task_selectors (Checkoff::TaskSelectors) (defaults to: Checkoff::TaskSelectors.new(config: config))
  • projects (Checkoff::Projects) (defaults to: Checkoff::Projects.new(config: config))
  • clients (Checkoff::Clients) (defaults to: Checkoff::Clients.new(config: config))
  • client (Asana::Client) (defaults to: clients.client)
  • search_url_parser (Checkoff::Internal::SearchUrl::Parser) (defaults to: Checkoff::Internal::SearchUrl::Parser.new)
  • asana_resources_collection_class (Class<Asana::Resources::Collection>) (defaults to: Asana::Resources::Collection)


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

def initialize(config: Checkoff::Internal::ConfigLoader.load(:asana),
               workspaces: Checkoff::Workspaces.new(config: config),
               task_selectors: Checkoff::TaskSelectors.new(config: config),
               projects: Checkoff::Projects.new(config: config),
               clients: Checkoff::Clients.new(config: 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

Class Method Details

.runvoid

This method returns an undefined value.



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/checkoff/task_searches.rb', line 206

def run
  # @sg-ignore
  # @type [String]
  workspace_name = ARGV[0] || raise('Please pass workspace name as first argument')
  # @sg-ignore
  # @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

#as_cache_keyHash

Returns:

  • (Hash)


113
114
115
# File 'lib/checkoff/task_searches.rb', line 113

def as_cache_key
  {}
end

#raw_task_search(api_params, workspace_gid:, extra_fields: [], task_selector: [], fetch_all: true) ⇒ Enumerable<Asana::Resources::Task>

Perform a search using the Asana Task Search API:

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

Parameters:

  • api_params (Hash<Symbol, Object>)
  • workspace_gid (String)
  • extra_fields (Array<String>) (defaults to: [])
  • task_selector (Array) (defaults to: [])
  • fetch_all (Boolean) (defaults to: true)

    Ensure all results are provided by manually paginating

Returns:

  • (Enumerable<Asana::Resources::Task>)


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

def raw_task_search(api_params,
                    workspace_gid:, extra_fields: [], task_selector: [],
                    fetch_all: true)
  # @sg-ignore
  tasks = api_task_search_request(api_params, workspace_gid: workspace_gid, extra_fields: extra_fields)

  if fetch_all && tasks.count == 100
    # @sg-ignore
    tasks = iterated_raw_task_search(api_params, workspace_gid: workspace_gid, extra_fields: extra_fields)
  end

  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>

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.

Parameters:

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

Returns:

  • (Enumerable<Asana::Resources::Task>)


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

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