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) ⇒ String

Returns new resource id.

Parameters:

  • payload (Hash)

    encryption_fingerprint [String] DIDWW::Encrypt#encryption_fingerprint file [Faraday::UploadIO] upload io description [String,nil] optional description

Returns:

  • (String)

    new resource id

Raises:



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

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).dig(:data, :id)
  else
    raise UploadError, "Code: #{response.status} #{response.body}"
  end
end

.upload_connectionFaraday::Connection

Returns:

  • (Faraday::Connection)


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

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_file(file, fingerprint, description: nil) ⇒ String

Returns new resource id.

Parameters:

  • file (Rack::Multipart::UploadedFile, (#tempfile,#content_type,#original_filename))
  • fingerprint (String)

    DIDWW::Encrypt#encryption_fingerprint

  • description (String, nil) (defaults to: nil)

    optional description (defaults to file.original_filename)

Returns:

  • (String)

    new resource id

Raises:



33
34
35
36
37
38
39
40
# File 'lib/didww/resource/encrypted_file.rb', line 33

def self.upload_file(file, fingerprint, description: nil)
  payload = {
    encryption_fingerprint: fingerprint,
    file: Faraday::Multipart::FilePart.new(file.tempfile, file.content_type),
    description: description || file.original_filename
  }
  upload(payload)
end