Skip to product menu
Skip to main content

Ruby SDK Samples - Files Operations

Upload Files
              
              
require 'ZOHOCRMSDK2_0'
class File
    def self.upload_file
    # """
    # This method is used to upload a file and print the response.
    # :param absolute_file_path: The absolute file path of the file to be attached
    # """

    # """
    # example
    # absolute_file_path = "/Users/user_name/Desktop/download.png";
    # """

    # Get instance of FileOperations Class
    fo = Files::FileOperations.new
    # Get instance of FileBodyWrapper Class that will contain the request body
    bw = Files::BodyWrapper.new
    # """
    # StreamWrapper can be initialized in any of the following ways

    # * param 1 -> fileName
    # * param 2 -> Read Stream.
    # * param 3 -> Absolute File Path of the file to be attached
    # """
    # stream_wrapper = StreamWrapper.new(name,stream,absolutefilepath)
    # Get instance of ParameterMap Class
    pm = ParameterMap.new
    pm.add(Files::FileOperations::UploadFilesParam.type, 'inline')
    sw1 = Util::StreamWrapper.new(nil, nil, '/Users/user_name/RUBYWS/zohocrm-rubysdk-sample-application/3524033000005520002.zip')
    sw2 = Util::StreamWrapper.new(nil, nil, '/Users/user_name/RUBYWS/zohocrm-rubysdk-sample-application/3524033000005902005.zip')
    # Set list of files to the BodyWrapper instance
    files = [sw1, sw2]
    bw.file = files
    # Call upload_file method that takes BodyWrapper instance as parameter.
    response = fo.upload_file(bw, pm)
    unless response.nil?
      status_code = response.status_code
      # Get the status code from response
      print "\n Status Code :" + status_code.to_s
      # Check if expected instance is received.
      if response.is_expected
        # Get object from response
        action_handler = response.data_object
        # Check if expected ActionWrapper instance is received.
        if action_handler.is_a? Files::ActionWrapper
          action_responses = action_handler.data
          action_responses.each do |action_response|
            # Check if expected SuccessResponse instance is received.
            if action_response.is_a? Files::SuccessResponse
              success_response = action_response
              # Get the Code
              print 'code:'
              print success_response.code.value
              # Get the Status
              print "\n status:"
              print success_response.status.value
              # Get the Message
              print "\n message:"
              print success_response.message.value
              # Get the details map
              success_response.details.each do |k, v|
                print "\n"
                print k
                print v
                print "\n"
              end
            # Check if the request returned an exception
            elsif action_response.is_a? Files::APIException
              api_exception = action_response
              # Get the Code
              print 'code:'
              print api_exception.code.value
              # Get the Status
              print "\n status:"
              print api_exception.status.value
              # Get the Message
              print "\n message:"
              print api_exception.message.value
              # Get the details map
              api_exception.details.each do |k, v|
                print "\n"
                print k
                print v
                print "\n"
              end
              print "\n"
            end
          end
        # Check if the request returned an exception
        elsif action_handler.is_a? Files::APIException
          api_exception = action_handler
          # Get the Code
          print 'code:'
          print api_exception.code.value
          # Get the Status
          print "\n status:"
          print api_exception.status.value
          # Get the Message
          print "\n message:"
          print api_exception.message.value
          # Get the details map
          api_exception.details.each do |k, v|
            print "\n"
            print k
            print v
            print "\n"
          end
          print "\n"
        end
      else
        response_object = response.data_object
        response_object.instance_variables.each do |field|
          print field
          print "\n"
          print response_object.instance_variable_get(field)
        end
      end
    end
  end
end
 
Get File