Class: CommandTower::Messaging::RecipientReadiness::Evaluate

Inherits:
Object
  • Object
show all
Defined in:
app/services/command_tower/messaging/recipient_readiness/evaluate.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipient_id:, platform_enabled_channels: []) ⇒ Evaluate

Returns a new instance of Evaluate.



21
22
23
24
25
# File 'app/services/command_tower/messaging/recipient_readiness/evaluate.rb', line 21

def initialize(recipient_id:, platform_enabled_channels: [])
  @recipient_id = recipient_id
  @platform_enabled_channels = Array(platform_enabled_channels).map(&:to_s).freeze
  @evaluated_at = Time.current
end

Class Method Details

.for_channel(recipient_id:, channel_key:, platform_enabled_channels: []) ⇒ Object



7
8
9
10
11
12
# File 'app/services/command_tower/messaging/recipient_readiness/evaluate.rb', line 7

def self.for_channel(recipient_id:, channel_key:, platform_enabled_channels: [])
  new(
    recipient_id:,
    platform_enabled_channels:,
  ).for_channel(channel_key)
end

.for_recipient(recipient_id:, platform_enabled_channels: []) ⇒ Object



14
15
16
17
18
19
# File 'app/services/command_tower/messaging/recipient_readiness/evaluate.rb', line 14

def self.for_recipient(recipient_id:, platform_enabled_channels: [])
  new(
    recipient_id:,
    platform_enabled_channels:,
  ).for_recipient
end

Instance Method Details

#for_channel(channel_key) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'app/services/command_tower/messaging/recipient_readiness/evaluate.rb', line 27

def for_channel(channel_key)
  key = channel_key.to_s
  definition = Channels.fetch(key)
  raise UnknownChannelError, "unknown channel: #{key.inspect}" if definition.nil?

  user = load_user!
  result = evaluate_definition(definition, user)
  log_evaluation(result)
  result
end

#for_recipientObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/command_tower/messaging/recipient_readiness/evaluate.rb', line 38

def for_recipient
  user = load_user!
  channels = {}

  Channels.definitions.each do |definition|
    channels[definition.key] = evaluate_definition(definition, user)
  end

  result = RecipientResult.build(
    recipient_id: @recipient_id,
    channels:,
    evaluated_at: @evaluated_at,
  )
  channels.each_value { |channel_result| log_evaluation(channel_result) }
  result
end