Class: RailVerdict::Waiver

Inherits:
Object
  • Object
show all
Defined in:
lib/rail_verdict/waiver.rb

Defined Under Namespace

Classes: IncompatibleError

Constant Summary collapse

SCHEMA_VERSION =
"1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Waiver

Returns a new instance of Waiver.



14
15
16
17
# File 'lib/rail_verdict/waiver.rb', line 14

def initialize(hash)
  @hash = hash.freeze
  freeze
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



12
13
14
# File 'lib/rail_verdict/waiver.rb', line 12

def hash
  @hash
end

Class Method Details

.active?(waiver, clock: Time.now.utc) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'lib/rail_verdict/waiver.rb', line 31

def self.active?(waiver, clock: Time.now.utc)
  hash = waiver.is_a?(Hash) ? waiver : waiver.to_h
  created_at = parse_time(hash["created_at"] || hash[:created_at])
  expires_at = parse_time(hash["expires_at"] || hash[:expires_at])
  return false unless created_at && expires_at

  clock >= created_at && clock < expires_at
end

.valid_fingerprint?(value) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rail_verdict/waiver.rb', line 27

def self.valid_fingerprint?(value)
  value.is_a?(String) && value.match?(Fingerprint::FINGERPRINT_PATTERN)
end

.validate_hash(hash) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rail_verdict/waiver.rb', line 40

def self.validate_hash(hash)
  errors = SchemaValidator.validate_waiver(hash)
  unless errors.empty?
    raise IncompatibleError, "waiver invalid: #{errors.join('; ')}"
  end
  created_at = parse_time(hash["created_at"])
  expires_at = parse_time(hash["expires_at"])
  raise IncompatibleError, "waiver expires_at must be after created_at" unless created_at && expires_at && expires_at > created_at
  raise IncompatibleError, "waiver timestamps must be UTC (Z)" unless utc_string?(hash["created_at"]) && utc_string?(hash["expires_at"])

  hash
end

Instance Method Details

#fingerprintObject



19
20
21
# File 'lib/rail_verdict/waiver.rb', line 19

def fingerprint
  @hash.fetch("fingerprint")
end

#to_hObject



23
24
25
# File 'lib/rail_verdict/waiver.rb', line 23

def to_h
  @hash
end