summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-11-21 16:30:46 -0700
committermo khan <mo.khan@gmail.com>2020-11-21 16:30:46 -0700
commit387e67d8691e69c3830702f851db7a4dc276d620 (patch)
treec5be4ec05c547b4ec0d5ba439b12fed0e8c12bd5
parentb38d7eb4ee2e141e508c99a58cc44947db9a9dc4 (diff)
refactor: exit early when request invalid
-rw-r--r--lib/server.rb29
1 files changed, 11 insertions, 18 deletions
diff --git a/lib/server.rb b/lib/server.rb
index a5c25ab..f465f7b 100644
--- a/lib/server.rb
+++ b/lib/server.rb
@@ -20,11 +20,9 @@ class DataStorageServer
# Objects that are not on the server will return a 404 Not Found.
def get(path)
oid = id_from(path)
- if @storage.key?(oid)
- ['200', {}, [@storage[oid]]]
- else
- not_found
- end
+ return not_found unless @storage.key?(oid)
+
+ ['200', {}, [@storage[oid]]]
end
# Upload an Object
@@ -43,14 +41,11 @@ class DataStorageServer
# and the first 1024 bytes are stored.
def put(io)
data = io.read(MAX_BYTES)
+ return ['400', {}, []] unless io.eof?
- if io.eof?
- oid = Digest::SHA256.hexdigest(data)
- @storage[oid] = data
- ['201', {}, [JSON.generate({ size: data.size, oid: oid })]]
- else
- ['400', {}, []]
- end
+ oid = Digest::SHA256.hexdigest(data)
+ @storage[oid] = data
+ ['201', {}, [JSON.generate({ size: data.size, oid: oid })]]
end
# Delete an Object
@@ -65,12 +60,10 @@ class DataStorageServer
# then a 404 status code is returned.
def destroy(path)
oid = id_from(path)
- if @storage.key?(oid)
- @storage.delete(oid)
- ['200', {}, []]
- else
- not_found
- end
+ return not_found unless @storage.key?(oid)
+
+ @storage.delete(oid)
+ ['200', {}, []]
end
def call(env)