Class: Shipeasy::SDK::See::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/shipeasy/sdk/see.rb

Overview

Accumulates consequence + extras; .to(outcome) dispatches once.

Instance Method Summary collapse

Constructor Details

#initialize(problem, dispatch) ⇒ Chain

Returns a new instance of Chain.



210
211
212
213
214
215
216
217
# File 'lib/shipeasy/sdk/see.rb', line 210

def initialize(problem, dispatch)
  @problem  = problem
  @dispatch = dispatch
  @subject  = nil
  @outcome  = nil
  @extras   = nil
  @done     = false
end

Instance Method Details

#causes_the(subject) ⇒ Object Also known as: causesThe



219
220
221
222
# File 'lib/shipeasy/sdk/see.rb', line 219

def causes_the(subject)
  @subject = subject.to_s
  self
end

#extras(extras) ⇒ Object

Attach debugging metadata. Chainable — call repeatedly (keys merge, later wins) before .to. Called after .to it is a no-op with a warning: the report already shipped, so there is nothing to amend and, crucially, it must not raise into the caller's rescue block. Use .to(outcome, extras) or Shipeasy.add_extras for late/scattered context instead.



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/shipeasy/sdk/see.rb', line 231

def extras(extras)
  if @done
    Shipeasy::Logging.warn(
      "[shipeasy] see() .extras(...) called after .to(...) is ignored — " \
      "pass extras to .to(outcome, extras) or call .extras before .to"
    )
    return self
  end
  merge_extras(extras)
  self
end

#to(outcome, extras = nil) ⇒ Object

Terminal: build the event and fire-and-forget the report. Idempotent. extras may be passed inline here as the trailing form .to(outcome, { order_id: oid }) — merged like a final .extras call. Returns self so a stray trailing .extras chains harmlessly.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/shipeasy/sdk/see.rb', line 247

def to(outcome, extras = nil)
  return self if @done

  merge_extras(extras) unless extras.nil?
  @done = true
  @outcome = outcome.to_s
  begin
    @dispatch.call(
      Built.new(@problem, @subject || DEFAULT_SUBJECT, @outcome.empty? ? DEFAULT_OUTCOME : @outcome, resolved_extras)
    )
  rescue StandardError
    # Reporting must never raise into caller code.
    nil
  end
  self
end