Sha256: 42caddddedd4314dae05d98388da7505ceb083829d3a302c9fa4c21dfad33ec5

Contents?: true

Size: 1.9 KB

Versions: 169

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

require 'asana'
require 'forwardable'
require 'cache_method'
require_relative 'internal/config_loader'
require_relative 'clients'

# https://developers.asana.com/docs/workspaces

module Checkoff
  # Query different workspaces of Asana projects
  class Workspaces
    MINUTE = 60
    HOUR = MINUTE * 60
    DAY = 24 * HOUR
    REALLY_LONG_CACHE_TIME = HOUR * 1
    LONG_CACHE_TIME = MINUTE * 15
    SHORT_CACHE_TIME = MINUTE

    # @!parse
    #   extend CacheMethod::ClassMethods

    # @param config [Hash<Symbol, Object>]
    # @param client [Asana::Client]
    # @param asana_workspace [Class<Asana::Resources::Workspace>]
    def initialize(config: Checkoff::Internal::ConfigLoader.load(:asana),
                   client: Checkoff::Clients.new(config: config).client,
                   asana_workspace: Asana::Resources::Workspace)
      @config = config
      @client = client
      @asana_workspace = asana_workspace
    end

    # Pulls an Asana workspace object
    # @param [String] workspace_name
    # @sg-ignore
    # @return [Asana::Resources::Workspace, nil]
    def workspace(workspace_name)
      client.workspaces.find_all.find do |workspace|
        workspace.name == workspace_name
      end
    end
    cache_method :workspace, LONG_CACHE_TIME

    # @return [Asana::Resources::Workspace]
    def default_workspace
      @asana_workspace.find_by_id(client, default_workspace_gid)
    end
    cache_method :default_workspace, REALLY_LONG_CACHE_TIME

    # @param [String] workspace_name
    # @return [Asana::Resources::Workspace]
    def workspace_or_raise(workspace_name)
      w = workspace(workspace_name)
      raise "Could not find workspace #{workspace_name}" if w.nil?

      w
    end

    # @sg-ignore
    # @return [String]
    def default_workspace_gid
      @config.fetch(:default_workspace_gid)
    end

    private

    # @return [Asana::Client]
    attr_reader :client
  end
end

Version data entries

169 entries across 169 versions & 1 rubygems

Version Path
checkoff-0.222.0 lib/checkoff/workspaces.rb
checkoff-0.221.0 lib/checkoff/workspaces.rb
checkoff-0.220.0 lib/checkoff/workspaces.rb
checkoff-0.219.0 lib/checkoff/workspaces.rb
checkoff-0.218.0 lib/checkoff/workspaces.rb
checkoff-0.217.0 lib/checkoff/workspaces.rb
checkoff-0.216.0 lib/checkoff/workspaces.rb
checkoff-0.215.0 lib/checkoff/workspaces.rb
checkoff-0.214.0 lib/checkoff/workspaces.rb
checkoff-0.213.0 lib/checkoff/workspaces.rb
checkoff-0.212.0 lib/checkoff/workspaces.rb
checkoff-0.211.0 lib/checkoff/workspaces.rb
checkoff-0.210.0 lib/checkoff/workspaces.rb
checkoff-0.209.0 lib/checkoff/workspaces.rb
checkoff-0.208.0 lib/checkoff/workspaces.rb
checkoff-0.207.0 lib/checkoff/workspaces.rb
checkoff-0.206.0 lib/checkoff/workspaces.rb
checkoff-0.205.0 lib/checkoff/workspaces.rb
checkoff-0.204.0 lib/checkoff/workspaces.rb
checkoff-0.203.0 lib/checkoff/workspaces.rb