Sha256: dc0eba4c606ed11690c9da20b45b585335292ddb16b68061ad51f16b21bb9006

Contents?: true

Size: 1.84 KB

Versions: 29

Compression:

Stored size: 1.84 KB

Contents

#!/usr/bin/env ruby

# frozen_string_literal: true

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

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

module Checkoff
  # Work with tags in Asana
  class Tags
    MINUTE = 60
    HOUR = MINUTE * 60
    DAY = 24 * HOUR
    REALLY_LONG_CACHE_TIME = HOUR * 1
    LONG_CACHE_TIME = MINUTE * 15
    SHORT_CACHE_TIME = MINUTE

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

    def tag_or_raise(workspace_name, tag_name)
      tag = tag(workspace_name, tag_name)
      raise "Could not find tag #{tag_name} under workspace #{workspace_name}." if tag.nil?

      tag
    end
    cache_method :tag_or_raise, LONG_CACHE_TIME

    def tag(workspace_name, tag_name)
      workspace = workspaces.workspace_or_raise(workspace_name)
      tags = client.tags.get_tags_for_workspace(workspace_gid: workspace.gid)
      tags.find { |tag| tag.name == tag_name }
    end
    cache_method :tag, LONG_CACHE_TIME

    private

    attr_reader :workspaces, :client

    # bundle exec ./tags.rb
    # :nocov:
    class << self
      def run
        workspace_name = ARGV[0] || raise('Please pass workspace name as first argument')
        tag_name = ARGV[1] || raise('Please pass tag name as second argument')
        tags = Checkoff::Tags.new
        tag = tags.tag_or_raise(workspace_name, tag_name)
        puts "Results: #{tag}"
      end
    end
    # :nocov:
  end
end

# :nocov:
abs_program_name = File.expand_path($PROGRAM_NAME)
Checkoff::Tags.run if abs_program_name == __FILE__
# :nocov:

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
checkoff-0.34.1 lib/checkoff/tags.rb
checkoff-0.34.0 lib/checkoff/tags.rb
checkoff-0.33.2 lib/checkoff/tags.rb
checkoff-0.33.1 lib/checkoff/tags.rb
checkoff-0.33.0 lib/checkoff/tags.rb
checkoff-0.32.0 lib/checkoff/tags.rb
checkoff-0.31.0 lib/checkoff/tags.rb
checkoff-0.30.0 lib/checkoff/tags.rb
checkoff-0.29.4 lib/checkoff/tags.rb
checkoff-0.29.3 lib/checkoff/tags.rb
checkoff-0.29.2 lib/checkoff/tags.rb
checkoff-0.29.1 lib/checkoff/tags.rb
checkoff-0.29.0 lib/checkoff/tags.rb
checkoff-0.28.0 lib/checkoff/tags.rb
checkoff-0.27.0 lib/checkoff/tags.rb
checkoff-0.26.1 lib/checkoff/tags.rb
checkoff-0.26.0 lib/checkoff/tags.rb
checkoff-0.25.0 lib/checkoff/tags.rb
checkoff-0.24.1 lib/checkoff/tags.rb
checkoff-0.24.0 lib/checkoff/tags.rb