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.



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

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



242
243
244
245
# File 'lib/shipeasy/sdk/see.rb', line 242

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.



254
255
256
257
258
259
260
261
262
263
264
# File 'lib/shipeasy/sdk/see.rb', line 254

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.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/shipeasy/sdk/see.rb', line 270

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