Class: Asana::Workspace

Inherits:
Resource
  • Object
show all
Defined in:
lib/asana/resources/workspace.rb

Instance Method Summary collapse

Methods inherited from Resource

collection_path, custom_method_collection_url, element_path, #method_not_allowed, new_element_path, #reload, remove_json_extension, singleton_path, #to_json

Instance Method Details

#completed_tasksObject



32
33
34
# File 'lib/asana/resources/workspace.rb', line 32

def completed_tasks
  tasks.select(&:completed?)
end

#create_tag(options = {}) ⇒ Object



44
45
46
47
# File 'lib/asana/resources/workspace.rb', line 44

def create_tag(options = {})
  response = post("tags", nil, {data: options}.to_json)
  Asana::Tag.new(connection.format.decode(response.body), true)
end

#create_task(options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/asana/resources/workspace.rb', line 3

def create_task(options = {})
  # options.merge!(assignee: 'me') unless options[:assignee] or options['assignee']
  options.merge!(workspace: self.id)

  post_body = { data: options }
  response = Task.post(nil, nil, post_body.to_json)
  hash = connection.format.decode(response.body)

  # want to return a persisted record
  Task.find hash["id"]
rescue Exception => e
  $stderr.puts "ERROR: #{e.message}"
  $stderr.puts "RESPONSE: #{e.response.body}" if e.respond_to?(:response)
end

#find_or_create_tag(options = {}) ⇒ Object

TODO imporve this when Asana API is more advanced



37
38
39
40
41
42
# File 'lib/asana/resources/workspace.rb', line 37

def find_or_create_tag(options = {})
  options[:name].strip! if options[:name]

  # try to load from cache first, if not found, force reload, if still not found, create one
  find_tag_from_list(tags, options) || find_tag_from_list(tags(true), options) || create_tag(options)
end

#find_tag_from_list(tag_list, options = {}) ⇒ Object



61
62
63
# File 'lib/asana/resources/workspace.rb', line 61

def find_tag_from_list(tag_list, options = {})
  tag_list.find {|t| t.name && options[:name] && t.name.strip == options[:name] }
end

#load_tags_by_idObject

expensive query: reload each task by id only use this if Asana::Tag.new(tag, true) doesn’t work as expected



57
58
59
# File 'lib/asana/resources/workspace.rb', line 57

def load_tags_by_id
  get(:tags).map{|h| Asana::Tag.find h["id"]}
end

#tags(force_reload = false) ⇒ Object

Resource.new(record, true) is the trick to initialize a persisted record



50
51
52
53
# File 'lib/asana/resources/workspace.rb', line 50

def tags(force_reload = false)
  return @tags if defined?(@tags) and !force_reload
  @tags ||= get(:tags).map{|h| Asana::Tag.new(h, true)}
end

#tasks(options = {}) ⇒ Object

From API docs: If you specify a workspace you must also specify an assignee to filter on.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/asana/resources/workspace.rb', line 19

def tasks(options = {})
  options.merge!(assignee: 'me') unless options[:assignee] or options['assignee']

  url = "tasks?assignee=#{options[:assignee]}"

  url += "&include_archived=true" if options[:include_archived]

  get(url).map { |h| Asana::Task.find h["id"] }
rescue Exception => e
  $stderr.puts "ERROR: #{e.message}"
  $stderr.puts "RESPONSE: #{e.response.body}" if e.respond_to?(:response)
end