Skip to content

Commit

Permalink
Merge pull request #25 from gocardless/joesouthan/delete-this
Browse files Browse the repository at this point in the history
Introduce #delete
  • Loading branch information
JoeSouthan authored Feb 9, 2021
2 parents 90969ef + 8c96423 commit e9ccc10
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ between different storage options. Callers don't need to worry about the specifi
of where and how a file is stored and retrieved as long as the given key is valid.

Keys within the `FileStorage` are URI strings that can universally locate an object
in the given provider. A valid key example would be
in the given provider. A valid key example would be
`gs://gc-prd-nx-incoming/file/path.json`.

## Usage
Expand Down Expand Up @@ -132,3 +132,9 @@ FileStorage.for("inmemory://bucket/path/file.xml").download
FileStorage.for("inmemory://bucket/path/").list
=> ["inmemory://bucket/path/file.xml"]
```

### Delete a file
```ruby
FileStorage.for("inmemory://bucket/path/").delete!
=> true
```
16 changes: 16 additions & 0 deletions lib/file_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@ def list
result[:keys].map { |key| "#{adapter_type}://#{result[:bucket]}/#{key}" }
end

# Deletes a given key
# @return [true]
def delete!
info("Deleting file",
event: "delete_started")

start = FileStorage::Timing.monotonic_now
adapter.delete!(bucket: bucket, key: key)

info("File deleted",
event: "delete_finished",
duration: FileStorage::Timing.monotonic_now - start)

true
end

private

attr_reader :adapter
Expand Down
6 changes: 6 additions & 0 deletions lib/file_storage/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def list(bucket:, key:)
}
end

def delete!(bucket:, key:)
File.unlink(key_path(bucket, key))

true
end

private

attr_reader :base_dir
Expand Down
6 changes: 6 additions & 0 deletions lib/file_storage/gcs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def list(bucket:, key:)
}
end

def delete!(bucket:, key:)
get_bucket(bucket).file(key).delete

true
end

private

attr_reader :storage
Expand Down
6 changes: 6 additions & 0 deletions lib/file_storage/in_memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,11 @@ def list(bucket:, key:)
keys: matching_keys,
}
end

def delete!(bucket:, key:)
@buckets[bucket].delete(key)

true
end
end
end
11 changes: 11 additions & 0 deletions spec/file_storage/disk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,15 @@
end
end
end

describe "#delete!" do
before { instance.upload!(bucket: bucket, key: "hello", content: "world") }

it "deletes the given content" do
expect(instance.delete!(bucket: bucket, key: "hello")).to eq(true)

expect { instance.download(bucket: bucket, key: "hello").download }.
to raise_error(Errno::ENOENT, /No such file or directory/)
end
end
end
10 changes: 10 additions & 0 deletions spec/file_storage/in_memory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,14 @@
end
end
end

describe "#delete!" do
before { instance.upload!(bucket: bucket, key: "hello", content: "world") }

it "deletes the given content" do
expect(instance.delete!(bucket: bucket, key: "hello")).to eq(true)

expect { instance.download(bucket: bucket, key: "hello").download }.to raise_error(KeyError)
end
end
end
10 changes: 10 additions & 0 deletions spec/file_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,15 @@
end
end
end

describe "delete!" do
before do
described_class.for("inmemory://bucket/file1").upload!("content1")
end

it "downloads the given file" do
expect(described_class.for("inmemory://bucket/file1").delete!).to eq(true)
end
end
end
end

0 comments on commit e9ccc10

Please sign in to comment.