diff options
| author | mo khan <mo.khan@gmail.com> | 2020-11-21 15:04:49 -0700 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-11-21 15:04:50 -0700 |
| commit | 25c304c14a6ec9841ce03b0482e6a507ef73d811 (patch) | |
| tree | bd9d11a65caaf1491d32d7ff95799e9861ad056b | |
| parent | 8d5d0504d4c89d0c2883f16340ddd9d76a0c71f7 (diff) | |
feat: generate SHA1 for the object id.
SHA1 is known to have collisions so we might want
to consider using SHA256 but I think this exercise
is meant to see if I recognize the git dumb/smart
REST API.
Also, this is fun to do in pieces. So yeah. Let's
see if we can add a test to try to create a SHA1
collision for bonus points. Right?!
😀
| -rw-r--r-- | server-rack.rb | 16 | ||||
| -rw-r--r-- | test.rb | 2 |
2 files changed, 14 insertions, 4 deletions
diff --git a/server-rack.rb b/server-rack.rb index 676956e..e16d383 100644 --- a/server-rack.rb +++ b/server-rack.rb @@ -2,10 +2,12 @@ # Usage: # # $ ruby rack-server.rb +require 'digest/sha1' require 'rack' require 'json' class DataStorageServer + MAX_BYTES=1024 # You may initialize any variables you want to use across requests here def initialize @storage = {} @@ -20,11 +22,19 @@ class DataStorageServer # {object data} # Objects that are not on the server will return a 404 Not Found. def get(path) - ['200', {}, ["hello from get #{path}"]] + oid = path.split('/')[-1] + if @storage.key?(oid) + ['200', {}, [@storage[oid]]] + else + ['200', {}, ["hello from get #{path}"]] + end end - def put(_env) - ['201', {}, [JSON.generate({ size: 11, oid: "x" })]] + def put(env) + data = env['rack.input'].read(MAX_BYTES) + oid = Digest::SHA1.hexdigest(data) + @storage[oid] = data + ['201', {}, [JSON.generate({ size: data.size, oid: oid })]] end def call(env) @@ -19,7 +19,7 @@ class DataStorageServerTest < Minitest::Test assert result["oid"].length > 0 end - def xtest_get + def test_get put '/data/foo', 'some object' res1 = JSON.parse(last_response.body) |
