Module: SpreeCmCommissioner::Webhooks::SubscriberRulable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/spree_cm_commissioner/webhooks/subscriber_rulable.rb

Constant Summary collapse

MATCH_POLICIES =
%i[all any].freeze
SUPPORTED_RULE_TYPES =
[
  SpreeCmCommissioner::Webhooks::Rules::OrderStates,
  SpreeCmCommissioner::Webhooks::Rules::OrderVendors
].map(&:to_s)
AUTHORIZER_RULE_TYPES =
[
  SpreeCmCommissioner::Webhooks::Rules::OrderVendors
].map(&:to_s)

Instance Method Summary collapse

Instance Method Details

#authorized_to?(resource) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/concerns/spree_cm_commissioner/webhooks/subscriber_rulable.rb', line 57

def authorized_to?(resource)
  # subscriber does not have access to resource if it has no rules
  return false if authorizer_rules.none?

  if match_all?
    authorizer_rules.all? do |rule|
      rule.matches?(resource.send(:webhook_payload_body), **resource.send(:webhooks_request_options))
    end
  elsif match_any?
    authorizer_rules.any? do |rule|
      rule.matches?(resource.send(:webhook_payload_body), **resource.send(:webhooks_request_options))
    end
  end
end

#available_rule_typesObject



24
25
26
27
28
# File 'app/models/concerns/spree_cm_commissioner/webhooks/subscriber_rulable.rb', line 24

def available_rule_types
  existing = rules.pluck(:type)

  SUPPORTED_RULE_TYPES.reject { |r| existing.include? r }
end

#match_all?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/models/concerns/spree_cm_commissioner/webhooks/subscriber_rulable.rb', line 30

def match_all?
  match_policy == 'all'
end

#match_any?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/models/concerns/spree_cm_commissioner/webhooks/subscriber_rulable.rb', line 34

def match_any?
  match_policy == 'any'
end

#matches?(event, webhook_payload_body, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/concerns/spree_cm_commissioner/webhooks/subscriber_rulable.rb', line 38

def matches?(event, webhook_payload_body, options = {})
  # Subscriber without rules are always match by default.
  return true if rules.none?

  # Reject if event is not supported by rules
  supported_rules = rules.select { |rule| rule.supported?(event) }
  return false if supported_rules.none?

  if match_all?
    supported_rules.all? do |rule|
      rule.matches?(webhook_payload_body, options)
    end
  elsif match_any?
    supported_rules.any? do |rule|
      rule.matches?(webhook_payload_body, options)
    end
  end
end