Class: Clickwrap::Verification::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/clickwrap/verification.rb

Overview

The structured answer. details carries stable machine-readable facts — never a surprise field of personal data, because a verification result routinely ends up in a log line or an API response.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success:, error: nil, policy_key: nil, statement_key: nil, event_id: nil, details: {}) ⇒ Result

Returns a new instance of Result.



22
23
24
25
26
27
28
29
30
31
# File 'lib/clickwrap/verification.rb', line 22

def initialize(success:, error: nil, policy_key: nil, statement_key: nil,
               event_id: nil, details: {})
  @success = success
  @error = error
  @policy_key = policy_key
  @statement_key = statement_key
  @event_id = event_id
  @details = details.freeze
  freeze
end

Instance Attribute Details

#detailsObject (readonly)

Returns the value of attribute details.



20
21
22
# File 'lib/clickwrap/verification.rb', line 20

def details
  @details
end

#errorObject (readonly)

Returns the value of attribute error.



20
21
22
# File 'lib/clickwrap/verification.rb', line 20

def error
  @error
end

#event_idObject (readonly)

Returns the value of attribute event_id.



20
21
22
# File 'lib/clickwrap/verification.rb', line 20

def event_id
  @event_id
end

#policy_keyObject (readonly)

Returns the value of attribute policy_key.



20
21
22
# File 'lib/clickwrap/verification.rb', line 20

def policy_key
  @policy_key
end

#statement_keyObject (readonly)

Returns the value of attribute statement_key.



20
21
22
# File 'lib/clickwrap/verification.rb', line 20

def statement_key
  @statement_key
end

Class Method Details

.failure(error, policy_key: nil, statement_key: nil, event_id: nil, details: {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/clickwrap/verification.rb', line 71

def self.failure(error, policy_key: nil, statement_key: nil, event_id: nil, details: {})
  unless Vocabulary::VERIFICATION_ERRORS.include?(error)
    raise ArgumentError,
          "#{error.inspect} is not a stable verification error. Add it to " \
          "Clickwrap::Vocabulary::VERIFICATION_ERRORS so applications can branch on it."
  end

  new(success: false, error: error, policy_key: policy_key, statement_key: statement_key,
      event_id: event_id, details: details)
end

.success(policy_key:, event_id: nil, details: {}) ⇒ Object



33
34
35
# File 'lib/clickwrap/verification.rb', line 33

def self.success(policy_key:, event_id: nil, details: {})
  new(success: true, policy_key: policy_key, event_id: event_id, details: details)
end

Instance Method Details

#as_jsonObject



107
# File 'lib/clickwrap/verification.rb', line 107

def as_json(*) = to_h

#failure?Boolean

Returns:

  • (Boolean)


83
# File 'lib/clickwrap/verification.rb', line 83

def failure? = !@success

#inspectObject



109
110
111
# File 'lib/clickwrap/verification.rb', line 109

def inspect
  success? ? "#<Clickwrap::Verification::Result success>" : "#<Clickwrap::Verification::Result #{error}>"
end

#messageObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/clickwrap/verification.rb', line 85

def message
  return I18n.t("clickwrap.verification.success", default: "Verified.") if success?

  I18n.t(
    "clickwrap.verification.errors.#{error}",
    policy: policy_key,
    statement: statement_key,
    default: default_message
  )
end

#recorded_after?(other) ⇒ Boolean

Whether this result's evidence was durably sequenced after another's. Events written after the recording-order migration reserve a database-assigned sequence, so this remains true across actors, app processes, and same-microsecond writes; ULID lexical order is deliberately not used as chronology. It returns false for either event predating that migration because an upgrade cannot invent honest order.

declaration.recorded_after?(acknowledgments)  # => true or false

Accepts another verification result or a bare event id. False whenever either side has no event, which composes with success? the way a guard should: nothing about a missing act is "after" anything.

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/clickwrap/verification.rb', line 49

def recorded_after?(other)
  other_event_id = other.respond_to?(:event_id) ? other.event_id : other
  return false unless event_id.present? && other_event_id.present?

  current, previous = Event.where(id: [event_id, other_event_id]).index_by(&:id).values_at(
    event_id.to_s, other_event_id.to_s
  )
  return false unless current && previous
  return false if current.recording_sequence.blank? || previous.recording_sequence.blank?

  current.recording_sequence > previous.recording_sequence
end

#success?Boolean

Returns:

  • (Boolean)


82
# File 'lib/clickwrap/verification.rb', line 82

def success? = @success

#to_hObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/clickwrap/verification.rb', line 96

def to_h
  {
    "success" => success?,
    "policy" => policy_key,
    "statement" => statement_key,
    "error" => error&.to_s,
    "event_id" => event_id,
    "details" => details
  }.compact
end