Module: Asana::Resources::AttachmentUploading Private

Included in:
Task
Defined in:
lib/asana/resource_includes/attachment_uploading.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Mixin to add the ability to upload an attachment to a specific Asana resource (a Task, really).

Instance Method Summary collapse

Instance Method Details

#attach(filename: required('filename'), mime: required('mime'), io: nil, options: {}, **data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Uploads a new attachment to the resource.

Parameters:

  • filename (String) (defaults to: required('filename'))

    the absolute path of the file to upload OR the desired filename when using io

  • mime (String) (defaults to: required('mime'))

    the MIME type of the file

  • io (IO) (defaults to: nil)

    an object which returns the file's content on #read, e.g. a ::StringIO

  • options (Hash) (defaults to: {})

    the request I/O options

  • data (Hash)

    extra attributes to post

  • rubocop:disable

    Metrics/AbcSize

  • rubocop:disable

    Metrics/MethodLength


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/asana/resource_includes/attachment_uploading.rb', line 16

def attach(filename: required('filename'),
           mime: required('mime'),
           io: nil, options: {}, **data)

  upload = if io.nil?
             path = File.expand_path(filename)
             raise ArgumentError, "file #{filename} doesn't exist" unless File.exist?(path)

             Faraday::FilePart.new(path, mime)
           else
             Faraday::FilePart.new(io, mime, filename)
           end

  response = client.post("/#{self.class.plural_name}/#{gid}/attachments",
                         body: data,
                         upload: upload,
                         options: options)

  Attachment.new(parse(response).first, client: client)
end