Class: DIDWW::Resource::EncryptedFile

Inherits:
Base
  • Object
show all
Defined in:
lib/didww/resource/encrypted_file.rb

Defined Under Namespace

Classes: UploadError

Class Method Summary collapse

Methods inherited from Base

#as_json_api

Class Method Details

.upload(payload) ⇒ Array<String>

Parameters:

  • payload (Hash)

    encryption_fingerprint [String] DIDWW::Encrypt#encryption_fingerprint items [Array]

    file [Faraday::UploadIO] upload io
    description [String,nil] optional description
    

Returns:

  • (Array<String>)

Raises:



47
48
49
50
51
52
53
54
55
# File 'lib/didww/resource/encrypted_file.rb', line 47

def self.upload(payload)
  connection = upload_connection
  response = connection.post('/v3/encrypted_files', encrypted_files: payload)
  if response.status == 201
    JSON.parse(response.body, symbolize_names: true)[:ids]
  else
    raise UploadError, "Code: #{response.status} #{response.body}"
  end
end

.upload_connectionFaraday::Connection

Returns:

  • (Faraday::Connection)


17
18
19
20
21
22
23
24
# File 'lib/didww/resource/encrypted_file.rb', line 17

def self.upload_connection
  Faraday.new(url: site) do |connection|
    connection.request :multipart
    connection.request :url_encoded
    connection.adapter Faraday.default_adapter
    connection.use DIDWW::BaseMiddleware
  end
end

.upload_files(files, fingerprint) ⇒ Array<String>

Parameters:

  • files (Rack::Multipart::UploadedFile, (#tempfile,#content_type,#original_filename))

Returns:

  • (Array<String>)

Raises:



29
30
31
32
33
34
35
36
37
38
# File 'lib/didww/resource/encrypted_file.rb', line 29

def self.upload_files(files, fingerprint)
  items = files.map do |file|
    {
      file: Faraday::UploadIO.new(file.tempfile, file.content_type),
      description: file.original_filename
    }
  end
  payload = { encryption_fingerprint: fingerprint, items: items }
  upload(payload)
end