Class: ReqresRspec::Uploaders::AmazonS3

Inherits:
Object
  • Object
show all
Defined in:
lib/reqres_rspec/uploaders/amazon_s3.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAmazonS3

Returns a new instance of AmazonS3.



7
8
9
10
11
12
13
14
15
# File 'lib/reqres_rspec/uploaders/amazon_s3.rb', line 7

def initialize
  @path     = ReqresRspec.configuration.output_path
  @logger   = ReqresRspec.logger
  @enabled  = ReqresRspec.configuration.amazon_s3[:enabled] || false
  @bucket   = ReqresRspec.configuration.amazon_s3[:bucket]

  ::Aws.config = ReqresRspec.configuration.amazon_s3[:credentials]
  @s3 = ::Aws::S3::Client.new
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



16
17
18
# File 'lib/reqres_rspec/uploaders/amazon_s3.rb', line 16

def logger
  @logger
end

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/reqres_rspec/uploaders/amazon_s3.rb', line 16

def path
  @path
end

Class Method Details

.uploadObject



18
19
20
21
# File 'lib/reqres_rspec/uploaders/amazon_s3.rb', line 18

def self.upload
  uploader = self.new
  uploader.process if uploader.enabled?
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/reqres_rspec/uploaders/amazon_s3.rb', line 23

def enabled?
  !!@enabled
end

#processObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/reqres_rspec/uploaders/amazon_s3.rb', line 27

def process
  prepare_bucket

  Dir["#{path}/**/*"].each { |file|
    next if File.directory?(file)
    local_path = file.gsub("#{@path}/", '')
    content_type = MIME::Types.type_for(local_path).first.content_type

    start = Time.now
    opts = {
      bucket: @bucket,
      key: local_path,
      body: File.open(file, 'rb'),
      acl: 'public-read',
      content_type: content_type
    }
    @s3.put_object opts
    done = Time.now

    puts "\n[#{local_path}] Uploaded in #{done.to_i - start.to_i}s"
  }
end