defra_ruby_aws 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/lib/defra_ruby/aws.rb +1 -0
- data/lib/defra_ruby/aws/bucket.rb +4 -0
- data/lib/defra_ruby/aws/response.rb +3 -1
- data/lib/defra_ruby/aws/services/delete_file_from_bucket_service.rb +34 -0
- data/lib/defra_ruby/aws/version.rb +1 -1
- data/spec/lib/defra_ruby/aws/response_spec.rb +26 -2
- data/spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb +56 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9b354b91b6387612b0f6dee19ea11e78773d929
|
4
|
+
data.tar.gz: ce4724a563a416001758583ecc1fe2693122fd71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4e9aefa9d7e36826ab34a3e8814f3974444f896a950ca425e22db97461e45e5f1e7ea4f5d4f3d8725fee7d5872acb9938bc3e08efd9adf99359b92ee98628a2
|
7
|
+
data.tar.gz: '099bf6de690ea832484508c3223b83f7a82d7fd1bc2ded66a040481491a09674a0c373b1f18558a48737536fb38a19642577e63fa35b32851d78c5e642658268'
|
data/README.md
CHANGED
@@ -70,6 +70,19 @@ bucket = DefraRuby::Aws.get_bucket("defra-ruby-aws")
|
|
70
70
|
presigned_url = bucket.presigned_url("test-upload-file.csv")
|
71
71
|
```
|
72
72
|
|
73
|
+
### Delete a file from the bucket
|
74
|
+
```ruby
|
75
|
+
bucket = DefraRuby::Aws.get_bucket("defra-ruby-aws")
|
76
|
+
response = bucket.delete("test-upload-file.csv")
|
77
|
+
|
78
|
+
if response.successful?
|
79
|
+
# Do something
|
80
|
+
else
|
81
|
+
response.error # return the failure error
|
82
|
+
# Do something else
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
73
86
|
## Contributing to this project
|
74
87
|
|
75
88
|
If you have an idea you'd like to contribute please log an issue.
|
data/lib/defra_ruby/aws.rb
CHANGED
@@ -7,6 +7,7 @@ require_relative "aws/configuration"
|
|
7
7
|
require_relative "aws/services/concerns/has_aws_bucket_configuration"
|
8
8
|
require_relative "aws/services/bucket_loader_service"
|
9
9
|
require_relative "aws/services/presigned_url_service"
|
10
|
+
require_relative "aws/services/delete_file_from_bucket_service"
|
10
11
|
|
11
12
|
module DefraRuby
|
12
13
|
module Aws
|
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
module DefraRuby
|
4
4
|
module Aws
|
5
|
+
class UnsuccessfulOperation < StandardError; end
|
6
|
+
|
5
7
|
class Response
|
6
8
|
attr_reader :error
|
7
9
|
|
@@ -21,7 +23,7 @@ module DefraRuby
|
|
21
23
|
attr_reader :success
|
22
24
|
|
23
25
|
def capture_response(response_exe)
|
24
|
-
response_exe.call
|
26
|
+
raise UnsuccessfulOperation unless response_exe.call
|
25
27
|
rescue StandardError => e
|
26
28
|
@error = e
|
27
29
|
@success = false
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DefraRuby
|
4
|
+
module Aws
|
5
|
+
class DeleteFileFromBucketService
|
6
|
+
include HasAwsBucketConfiguration
|
7
|
+
|
8
|
+
def self.run(bucket, file_name)
|
9
|
+
new(bucket, file_name).run
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(bucket, file_name)
|
13
|
+
@bucket = bucket
|
14
|
+
@file_name = file_name
|
15
|
+
end
|
16
|
+
|
17
|
+
def run
|
18
|
+
Response.new(response_exe)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
attr_reader :bucket, :file_name
|
24
|
+
|
25
|
+
def response_exe
|
26
|
+
lambda do
|
27
|
+
delete_object_output = s3_bucket.object(file_name).delete
|
28
|
+
|
29
|
+
delete_object_output.request_charged.length.positive?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -17,7 +17,31 @@ module DefraRuby
|
|
17
17
|
end
|
18
18
|
|
19
19
|
context "when the response don't throw an error" do
|
20
|
-
let(:response_exe) { -> {} }
|
20
|
+
let(:response_exe) { -> { true } }
|
21
|
+
|
22
|
+
it "returns true" do
|
23
|
+
expect(response).to be_successful
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when the response returns a nil object" do
|
28
|
+
let(:response_exe) { -> { nil } }
|
29
|
+
|
30
|
+
it "returns false" do
|
31
|
+
expect(response).to_not be_successful
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when the response returns false" do
|
36
|
+
let(:response_exe) { -> { false } }
|
37
|
+
|
38
|
+
it "returns false" do
|
39
|
+
expect(response).to_not be_successful
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "when the response returns an object" do
|
44
|
+
let(:response_exe) { -> { "I am an object" } }
|
21
45
|
|
22
46
|
it "returns true" do
|
23
47
|
expect(response).to be_successful
|
@@ -35,7 +59,7 @@ module DefraRuby
|
|
35
59
|
end
|
36
60
|
|
37
61
|
context "when the response don't throw an error" do
|
38
|
-
let(:response_exe) { -> {} }
|
62
|
+
let(:response_exe) { -> { true } }
|
39
63
|
|
40
64
|
it "returns a nil object" do
|
41
65
|
expect(response.error).to be_nil
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
module DefraRuby
|
6
|
+
module Aws
|
7
|
+
RSpec.describe DeleteFileFromBucketService do
|
8
|
+
describe "#run" do
|
9
|
+
let(:configs) do
|
10
|
+
{
|
11
|
+
credentials: {
|
12
|
+
access_key_id: "key_id",
|
13
|
+
secret_access_key: "secret"
|
14
|
+
},
|
15
|
+
name: "bulk"
|
16
|
+
}
|
17
|
+
end
|
18
|
+
let(:bucket) { Bucket.new(configs) }
|
19
|
+
|
20
|
+
it "returns a successful response" do
|
21
|
+
aws_resource = double(:aws_resource)
|
22
|
+
s3_bucket = double(:s3_bulk_bucket)
|
23
|
+
file_name = "test.csv"
|
24
|
+
s3_object = double(:s3_object)
|
25
|
+
result = double(:result, request_charged: "present")
|
26
|
+
|
27
|
+
expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
|
28
|
+
expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
|
29
|
+
expect(s3_bucket).to receive(:object).with("test.csv").and_return(s3_object)
|
30
|
+
expect(s3_object).to receive(:delete).and_return(result)
|
31
|
+
|
32
|
+
response = described_class.run(bucket, file_name)
|
33
|
+
expect(response).to be_successful
|
34
|
+
end
|
35
|
+
|
36
|
+
context "when the response body returns an empty charged requests" do
|
37
|
+
it "returns a non successful response" do
|
38
|
+
aws_resource = double(:aws_resource)
|
39
|
+
s3_bucket = double(:s3_bulk_bucket)
|
40
|
+
file_name = "test.csv"
|
41
|
+
s3_object = double(:s3_object)
|
42
|
+
result = double(:result, request_charged: "")
|
43
|
+
|
44
|
+
expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
|
45
|
+
expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
|
46
|
+
expect(s3_bucket).to receive(:object).with("test.csv").and_return(s3_object)
|
47
|
+
expect(s3_object).to receive(:delete).and_return(result)
|
48
|
+
|
49
|
+
response = described_class.run(bucket, file_name)
|
50
|
+
expect(response).to_not be_successful
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: defra_ruby_aws
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Defra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-s3
|
@@ -154,6 +154,7 @@ files:
|
|
154
154
|
- lib/defra_ruby/aws/response.rb
|
155
155
|
- lib/defra_ruby/aws/services/bucket_loader_service.rb
|
156
156
|
- lib/defra_ruby/aws/services/concerns/has_aws_bucket_configuration.rb
|
157
|
+
- lib/defra_ruby/aws/services/delete_file_from_bucket_service.rb
|
157
158
|
- lib/defra_ruby/aws/services/presigned_url_service.rb
|
158
159
|
- lib/defra_ruby/aws/version.rb
|
159
160
|
- spec/defra_ruby_aws_spec.rb
|
@@ -163,6 +164,7 @@ files:
|
|
163
164
|
- spec/lib/defra_ruby/aws/configuration_spec.rb
|
164
165
|
- spec/lib/defra_ruby/aws/response_spec.rb
|
165
166
|
- spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb
|
167
|
+
- spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb
|
166
168
|
- spec/lib/defra_ruby/aws/services/presigned_url_service_spec.rb
|
167
169
|
- spec/spec_helper.rb
|
168
170
|
- spec/support/aws_sdk_s3.rb
|
@@ -209,4 +211,5 @@ test_files:
|
|
209
211
|
- spec/lib/defra_ruby/aws/response_spec.rb
|
210
212
|
- spec/lib/defra_ruby/aws/bucket_spec.rb
|
211
213
|
- spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb
|
214
|
+
- spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb
|
212
215
|
- spec/lib/defra_ruby/aws/services/presigned_url_service_spec.rb
|