Class: Checkoff::Internal::AsanaEventFilter

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/checkoff/internal/asana_event_filter.rb,
sig/checkoff.rbs

Overview

Uses an enhanced version of Asana event filter configuration

See https://developers.asana.com/reference/createwebhook | body params | data | filters | add object for a general description of the scheme.

Additional supported filter keys:

  • 'checkoff:parent.gid' - requires that the 'gid' key in the 'parent' object match the given value

Instance Method Summary collapse

Constructor Details

#initialize(filters:, clients: Checkoff::Clients.new, tasks: Checkoff::Tasks.new, client: clients.client) ⇒ Object

sord warn - Asana::Client wasn't able to be resolved to a constant in this project @param filters — The filters to match against

@param clients

@param tasks

@param client



24
25
26
27
28
29
30
31
# File 'lib/checkoff/internal/asana_event_filter.rb', line 24

def initialize(filters:,
               clients: Checkoff::Clients.new,
               tasks: Checkoff::Tasks.new,
               client: clients.client)
  @filters = filters
  @client = client
  @tasks = tasks
end

Instance Method Details

#asana_event_matches_filter_item?(key, value, asana_event) ⇒ Boolean

@param key

@param value

@param asana_event

Parameters:

  • key (String)
  • value (String, ::Array[String])
  • asana_event (::Hash[untyped, untyped])

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/checkoff/internal/asana_event_filter.rb', line 76

def asana_event_matches_filter_item?(key, value, asana_event)
  case key
  when 'resource_type'
    asana_event.fetch('resource', {})['resource_type'] == value
  when 'resource_subtype'
    asana_event.fetch('resource', {})['resource_subtype'] == value
  when 'action'
    asana_event['action'] == value
  when 'fields'
    value.include? asana_event.fetch('change', {})['field']
  when 'checkoff:parent.gid'
    asana_event.fetch('parent', {})['gid'] == value
  when 'checkoff:resource.gid'
    asana_event.fetch('resource', {})['gid'] == value
  when 'checkoff:fetched.section.gid'
    fields = ['memberships.project.gid', 'memberships.project.name',
              'memberships.section.name', 'assignee', 'assignee_section']
    task = uncached_fetch_task(key, asana_event, fields)
    return false if task.nil?

    task_data = @tasks.task_to_h(task)
    task_data.fetch('unwrapped').fetch('membership_by_section_gid').keys.include?(value)
  when 'checkoff:fetched.parent_task.gid'
    fields = ['parent']
    task = uncached_fetch_task(key, asana_event, fields)
    return false if task.nil?

    task.parent&.fetch('gid', nil) == value
  when 'checkoff:fetched.completed'
    fields = ['completed_at']
    task = uncached_fetch_task(key, asana_event, fields)
    return false if task.nil?

    task_completed = !task.completed_at.nil?
    task_completed == value
  else
    raise "Unknown filter key #{key}"
  end
end

#debugvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


2661
# File 'sig/checkoff.rbs', line 2661

def debug: (?Object? message) -> void

#errorvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


2652
# File 'sig/checkoff.rbs', line 2652

def error: (?Object? message) -> void

#filter_matches_asana_event?(filter, asana_event, failures) ⇒ Boolean

@param filter

@param asana_event

@param failures

Parameters:

  • filter (::Hash[untyped, untyped])
  • asana_event (::Hash[untyped, untyped])
  • failures (::Array[String])

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
# File 'lib/checkoff/internal/asana_event_filter.rb', line 60

def filter_matches_asana_event?(filter, asana_event, failures)
  # @param key [String]
  # @param value [String, Array<String>]
  filter.all? do |key, value|
    matches = asana_event_matches_filter_item?(key, value, asana_event)
    failures << "#{key.inspect} = #{value.inspect}" unless matches

    matches
  end
end

#finervoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


2664
# File 'sig/checkoff.rbs', line 2664

def finer: (?Object? message) -> void

#infovoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


2658
# File 'sig/checkoff.rbs', line 2658

def info: (?Object? message) -> void

#log_levelSymbol

@sg-ignore

Returns:

  • (Symbol)


2667
# File 'sig/checkoff.rbs', line 2667

def log_level: () -> Symbol

#logger::Logger

Returns:

  • (::Logger)


2649
# File 'sig/checkoff.rbs', line 2649

def logger: () -> ::Logger

#matches?(asana_event) ⇒ Boolean

@param asana_event — The event that Asana sent

Parameters:

  • asana_event (::Hash[untyped, untyped])

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/checkoff/internal/asana_event_filter.rb', line 34

def matches?(asana_event)
  logger.debug { "Filtering using #{@filters.inspect}" }
  return true if @filters.nil?

  failures = []

  @filters.any? do |filter|
    filter_matches = filter_matches_asana_event?(filter, asana_event, failures)
    logger.debug { "Filter #{filter.inspect} matched? #{filter_matches} against event #{asana_event.inspect}" }
    unless filter_matches
      logger.debug do
        "Filter #{filter.inspect} failed to match event #{asana_event.inspect} because of #{failures.inspect}"
      end
      failures << filter
    end
    filter_matches
  end
end

#uncached_fetch_task(key, asana_event, fields) ⇒ Asana::Resources::Task?

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project @param key

@param asana_event

@param fields

Parameters:

  • key (String)
  • asana_event (::Hash[untyped, untyped])
  • fields (::Array[String])

Returns:

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


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/checkoff/internal/asana_event_filter.rb', line 121

def uncached_fetch_task(key, asana_event, fields)
  # @type [Hash{String => String}]
  resource = asana_event.fetch('resource')
  # @type [String]
  resource_type = resource.fetch('resource_type')
  unless resource_type == 'task'
    raise "Teach me how to check #{key.inspect} on resource type #{resource_type.inspect}"
  end

  task_gid = resource.fetch('gid')
  options = {
    fields:,
  }
  @client.tasks.find_by_id(task_gid, options:)
rescue Asana::Errors::NotFound
  nil
end

#warnvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


2655
# File 'sig/checkoff.rbs', line 2655

def warn: (?Object? message) -> void