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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f98773dfc3468b835ccb7120609f527544bb794
4
- data.tar.gz: 450ba740fa675f36bcd7b1c072524e06306e0f0f
3
+ metadata.gz: f9b354b91b6387612b0f6dee19ea11e78773d929
4
+ data.tar.gz: ce4724a563a416001758583ecc1fe2693122fd71
5
5
  SHA512:
6
- metadata.gz: bce99a5fbe92846cdf796771c153b3a90d2fef9459458f87f89e6626b24011c3bff84f5a42ba8ef4e457fa9bc297e4c59e8774fffdb958d0573fc6a46f524fd3
7
- data.tar.gz: 27d93f4cc90f7d8eb5445017a990215afec9e345d9121dd04243fd734854cfd8362c7c1403b7c8891199b26a236b0871c526b345075cd12c730957b28eba9f86
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.
@@ -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
@@ -29,6 +29,10 @@ module DefraRuby
29
29
  PresignedUrlService.run(self, file_name)
30
30
  end
31
31
 
32
+ def delete(file_name)
33
+ DeleteFileFromBucketService.run(self, file_name)
34
+ end
35
+
32
36
  private
33
37
 
34
38
  attr_writer :region
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module DefraRuby
4
4
  module Aws
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  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.1.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-06-14 00:00:00.000000000 Z
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