m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/engine.rb
blob: b04e2f2392558e9bf02ccca6fd04e3e3c9263b49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Engine
  def initialize hash
    validate_checksum hash
    hash_to_state hash
  end

  def validate_checksum hash
    if !checksum_valid? hash
      throw 'Invalid state, checksum does not match!'
    end
  end

  def checksum_valid? hash
    if hash.empty?
      return true
    end

    expected = checksum hash
    expected == hash['checksum']
  end

  def checksum hash
    string = hash
      .keys
      .filter { |k| k != 'checksum' }
      .sort
      .map { |k| "#{k}:#{hash[k]}" }
      .append("secret:#{secret}")
      .join '|'
    Digest::SHA2.hexdigest string
  end

  def hash_to_state hash
  end

  def step command
  end

  def state_h
    hash = state_to_hash
    hash['checksum'] = checksum hash
    hash
  end

  def state_to_hash
    {}
  end

  def message
  end
end