Asana / ruby-asana

Official Ruby client library for the Asana API v1
MIT License
77 stars 54 forks source link

create_attachment_for_object method is missing #158

Closed zeeshangulzar closed 1 year ago

zeeshangulzar commented 1 year ago

As per api documentation, i can attached a external url using following code, but create_attachment_for_object is not defined in this repo's code so getting undefined method error while calling this method result = client.attachments.create_attachment_for_object(field: "value", field: "value", options: {pretty: true}) https://developers.asana.com/reference/createattachmentforobject

Here is my code

@user = User.last
@asana_client = Asana::Client.new do |c| c.authentication :access_token, @user.asana_info['access_token'] end
result = @asana_client.attachments.create_attachment_for_object(parent: new_asana_card.gid, resource_subtype: 'gdrive', url: "https://docs.google.com/document/d/1BkCilscSoVgq8Bmhqu_Olh1zmummPkbGznrFHnbkzVs")

and here is the error

*** NoMethodError Exception: undefined method `create_attachment_for_object' for Asana::Resources::Attachment:Class

        @resource.public_send(m, *([@client] + args), **kwargs, &block)
                 ^^^^^^^^^^^^

nil
zeeshangulzar commented 1 year ago

Used following monkey patch. Add a file config/initializers/asana.rb

`module Asana module Resources class AttachmentsBase < Resource

  def self.inherited(base)
    Registry.register(base)
  end

  class << self
    # Upload an attachment
    #
    # task_gid - [str]  (required) The task to operate on.
    # options - [Hash] the request I/O options
    # > offset - [str]  Offset token. An offset to the next page returned by the API. A pagination request will return an offset token, which can be used as an input parameter to the next request. If an offset is not passed in, the API will return the first page of results. 'Note: You can only pass in an offset that was returned to you via a previously paginated request.'
    # > limit - [int]  Results per page. The number of objects to return per page. The value must be between 1 and 100.
    # > opt_fields - [list[str]]  Defines fields to return. Some requests return *compact* representations of objects in order to conserve resources and complete the request more efficiently. Other times requests return more information than you may need. This option allows you to list the exact set of fields that the API should be sure to return for the objects. The field names should be provided as paths, described below. The id of included objects will always be returned, regardless of the field options.
    # > opt_pretty - [bool]  Provides “pretty” output. Provides the response in a “pretty” format. In the case of JSON this means doing proper line breaking and indentation to make it readable. This will take extra time and increase the response size so it is advisable only to use this during debugging.
    # data - [Hash] the attributes to POST
    def create_attachment_for_task(client, task_gid: required("task_gid"), options: {}, **data)
      path = "/tasks/{task_gid}/attachments"
      path["{task_gid}"] = task_gid
      Attachment.new(parse(client.post(path, body: data, options: options)).first, client: client)
    end

  end
end

end end `