Class: Textus::Application::Read::GetOrRefresh::Impl

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/application/read/get_or_refresh.rb

Instance Method Summary collapse

Constructor Details

#initialize(caps:, get:, orchestrator:) ⇒ Impl

Returns a new instance of Impl.



22
23
24
25
26
# File 'lib/textus/application/read/get_or_refresh.rb', line 22

def initialize(caps:, get:, orchestrator:)
  @manifest     = caps.manifest
  @get          = get
  @orchestrator = orchestrator
end

Instance Method Details

#call(key) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/textus/application/read/get_or_refresh.rb', line 28

def call(key)
  envelope = @get.call(key)
  return nil if envelope.nil?
  return envelope unless envelope.freshness&.stale

  policy_set = @manifest.rules.for(key)
  refresh_policy = policy_set.refresh
  return envelope if refresh_policy.nil?

  policy = refresh_policy.to_freshness_policy
  verdict = Textus::Domain::Freshness::Verdict.stale(envelope.freshness.reason)
  action = policy.decide(verdict)
  outcome = @orchestrator.execute(action, key: key)

  case outcome
  when Textus::Domain::Outcome::Skipped
    envelope
  when Textus::Domain::Outcome::Refreshed
    outcome.envelope.with(
      freshness: Textus::Domain::Freshness.build(stale: false, reason: nil, refreshing: false),
    )
  when Textus::Domain::Outcome::Detached
    envelope.with(freshness: envelope.freshness.with(refreshing: true))
  when Textus::Domain::Outcome::Failed
    envelope.with(
      freshness: envelope.freshness.with(refresh_error: outcome.error.message),
    )
  end
end