m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/engine.rb
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-08-17 23:31:13 -0700
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-08-17 23:31:13 -0700
commite3afffc91d678301fcf7660db541cacc8b7a4e8d (patch)
treefd391ede8eb099d5b66672751ee01ae07f6deb98 /engine.rb
Initial commit
Diffstat (limited to 'engine.rb')
-rw-r--r--engine.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/engine.rb b/engine.rb
new file mode 100644
index 0000000..638a80e
--- /dev/null
+++ b/engine.rb
@@ -0,0 +1,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