Class: Ecoportal::API::V2::S3::Upload

Inherits:
Object
  • Object
show all
Defined in:
lib/ecoportal/api/v2/s3/upload.rb

Defined Under Namespace

Classes: MissingFile

Constant Summary collapse

DEFAULT_MIME_TYPE =
'application/octet-stream'.freeze
EXPECTED_CODE =

No Content

"204".freeze
MAX_RETRIES =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials, file:) ⇒ Upload

Returns a new instance of Upload.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
# File 'lib/ecoportal/api/v2/s3/upload.rb', line 14

def initialize(credentials, file:)
  msg = "Expecting #{credentials_class}. Given: #{credentials.class}"
  raise ArgumentError, msg unless credentials.is_a?(credentials_class)

  @credentials = credentials
  @filename    = file
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



12
13
14
# File 'lib/ecoportal/api/v2/s3/upload.rb', line 12

def filename
  @filename
end

Instance Method Details

#failed?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/ecoportal/api/v2/s3/upload.rb', line 40

def failed?
  return false unless response

  response.code != EXPECTED_CODE
end

#s3_file_referenceObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/ecoportal/api/v2/s3/upload.rb', line 52

def s3_file_reference
  return unless success?

  @s3_file_reference ||= {
    'filename' => file_basename,
    'filetype' => mime_type,
    'filesize' => file_size,
    'path'     => s3_file_path
  }
end

#success?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/ecoportal/api/v2/s3/upload.rb', line 46

def success?
  return false unless response

  response.code == EXPECTED_CODE
end

#upload! {|s3_file_reference| ... } ⇒ Object

Yields:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ecoportal/api/v2/s3/upload.rb', line 22

def upload!
  require_deps!

  @response = Net::HTTP.start(
    uri.hostname,
    uri.port,
    use_ssl: true
  ) do |https|
    https.max_retries = self.class::MAX_RETRIES
    https.request(request)
  end

  return unless success?

  yield(s3_file_reference) if block_given?
  s3_file_reference
end