Skip to main content

Ruby SDK Samples - Files Operations

Upload Files
Get File
              
              
require 'ZOHOCRMSDK2_0'
class File
    def self.get_file(id, destination_folder)
    # """
    # This method is used to download the file with ID and write in the destinationFolder
    # :param id: The ID of the uploaded File.
    # :param destination_folder: The absolute path of the destination folder to store the File
    # """

    # """
    # example
    # id = "ae9c7cefa418aec1d6a5cc2d9ab35c3231aae3bfeef7d5e00a54b7563c0dd42b";
    # destination_folder = "/Users/user_name/Desktop"
    # """
    # Get instance of FileOperations Class
    fo = Files::FileOperations.new
    # Get instance of ParameterMap Class
    pm = ParameterMap.new
    # Add the id to ParameterMap instance
    pm.add(Files::FileOperations::GetFileParam.id, 'f46166fa14ce16c6e2622b3ce8283075a4aeb9c355b109cd8f502c78ffdb9324')
    # Call get_file method that takes ParameterMap instance as parameter
    response = fo.get_file(pm)
    unless response.nil?
      status_code = response.status_code
      # Get the status code from response
      print "\n Status Code :" + status_code.to_s
      if [204, 304].include? status_code
        print(status_code == 204 ? 'No Content' : 'Not Modified')
        return
      end
      # Check if expected instance is received.
      if response.is_expected
        # Get object from response
        response_handler = response.data_object
        # Check if expected FileBodyWrapper instance is received.
        if response_handler.is_a? Files::FileBodyWrapper
          # Get StreamWrapper instance from the returned FileBodyWrapper instance
          file_body_wrapper = response_handler
          stream_wrapper = file_body_wrapper.file
          # Construct the file name by joining the destinationFolder and the name from StreamWrapper instance
          # Open the destination file where the file needs to be written in 'w' mode
          File.open(destination_folder + '/' + stream_wrapper.name, 'w') do |f|
            f.write(stream_wrapper.stream)
          end
        # Check if the request returned an exception
        elsif response_handler.is_a? Files::APIException
          exception = response_handler
          # Get the Code
          print 'code:'
          print exception.code.value
          # Get the Status
          print "\n status:"
          print exception.status.value
          # Get the Message
          print "\n message:"
          print exception.message.value
          # Get the details map
          exception.details.each do |k, v|
            print "\n"
            print k
            print v
            print "\n"
          end
          print "\n"
        end
      elsif status_code != 204
        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