Class: Checkoff::Portfolios

Inherits:
Object
  • Object
show all
Extended by:
CacheMethod::ClassMethods
Defined in:
lib/checkoff/portfolios.rb

Overview

Pull portfolios from Asana

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

Constructor Details

#initialize(config: Checkoff::Internal::ConfigLoader.load(:asana), clients: Checkoff::Clients.new(config: config), client: clients.client, workspaces: Checkoff::Workspaces.new(config: config, client: client)) ⇒ Portfolios

Returns a new instance of Portfolios.

Parameters:

  • config (Hash) (defaults to: Checkoff::Internal::ConfigLoader.load(:asana))
  • workspaces (Checkoff::Workspaces) (defaults to: Checkoff::Workspaces.new(config: config, client: client))
  • clients (Checkoff::Clients) (defaults to: Checkoff::Clients.new(config: config))
  • client (Asana::Client) (defaults to: clients.client)


30
31
32
33
34
35
36
# File 'lib/checkoff/portfolios.rb', line 30

def initialize(config: Checkoff::Internal::ConfigLoader.load(:asana),
               clients: Checkoff::Clients.new(config: config),
               client: clients.client,
               workspaces: Checkoff::Workspaces.new(config: config, client: client))
  @workspaces = workspaces
  @client = client
end

Class Method Details

.runvoid

This method returns an undefined value.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/checkoff/portfolios.rb', line 117

def run
  # @sg-ignore
  # @type [String]
  workspace_name = ARGV[0] || raise('Please pass workspace name as first argument')
  # @sg-ignore
  # @type [String]
  portfolio_name = ARGV[1] || raise('Please pass portfolio name as second argument')
  portfolios = Checkoff::Portfolios.new
  portfolio = portfolios.portfolio_or_raise(workspace_name, portfolio_name)
  puts "Results: #{portfolio}"
end

Instance Method Details

#portfolio(workspace_name, portfolio_name) ⇒ Asana::Resources::Portfolio?

@sg-ignore

Parameters:

  • workspace_name (String)
  • portfolio_name (String)

Returns:

  • (Asana::Resources::Portfolio, nil)


55
56
57
58
59
60
61
62
# File 'lib/checkoff/portfolios.rb', line 55

def portfolio(workspace_name, portfolio_name)
  workspace = workspaces.workspace_or_raise(workspace_name)
  me = client.users.me
  portfolio_objs = client.portfolios.find_all(workspace: workspace.gid,
                                              owner: me.gid)
  # @type [Asana::Resources::Portfolio, nil]
  portfolio_objs.find { |portfolio_obj| portfolio_obj.name == portfolio_name }
end

#portfolio_by_gid(portfolio_gid, extra_fields: []) ⇒ Asana::Resources::Portfolio?

Pull a specific portfolio by gid

Parameters:

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

Returns:

  • (Asana::Resources::Portfolio, nil)


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

def portfolio_by_gid(portfolio_gid,
                     extra_fields: [])
  options = {
    fields: ['name'],
  }
  options[:fields] += extra_fields
  client.portfolios.find_by_id(portfolio_gid, options: options)
end

#portfolio_or_raise(workspace_name, portfolio_name) ⇒ Asana::Resources::Portfolio

Parameters:

  • workspace_name (String)
  • portfolio_name (String)

Returns:

  • (Asana::Resources::Portfolio)


42
43
44
45
46
47
# File 'lib/checkoff/portfolios.rb', line 42

def portfolio_or_raise(workspace_name, portfolio_name)
  portfolio_obj = portfolio(workspace_name, portfolio_name)
  raise "Could not find portfolio #{portfolio_name} under workspace #{workspace_name}." if portfolio_obj.nil?

  portfolio_obj
end

#projects_in_portfolio(workspace_name, portfolio_name, extra_project_fields: []) ⇒ Enumerable<Asana::Resources::Project>

Parameters:

  • workspace_name (String)
  • portfolio_name (String)
  • extra_project_fields (Array<String>) (defaults to: [])

Returns:

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


86
87
88
89
90
# File 'lib/checkoff/portfolios.rb', line 86

def projects_in_portfolio(workspace_name, portfolio_name,
                          extra_project_fields: [])
  portfolio = portfolio_or_raise(workspace_name, portfolio_name)
  projects_in_portfolio_obj(portfolio)
end

#projects_in_portfolio_obj(portfolio, extra_project_fields: []) ⇒ Enumerable<Asana::Resources::Project>

Parameters:

  • portfolio (Asana::Resources::Portfolio)
  • extra_project_fields (Array<String>) (defaults to: [])

Returns:

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


97
98
99
100
101
102
103
# File 'lib/checkoff/portfolios.rb', line 97

def projects_in_portfolio_obj(portfolio, extra_project_fields: [])
  options = {
    fields: ['name'],
  }
  options[:fields] += extra_project_fields
  client.portfolios.get_items_for_portfolio(portfolio_gid: portfolio.gid, options: options)
end