def download_file(response)
      tempfile = nil
      encoding = nil
      content_disposition = response.headers['Content-Disposition']
      if content_disposition and content_disposition =~ /filename=/i
        filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
        prefix = sanitize_filename(filename)
      else
        prefix = 'download-'
      end
      prefix = prefix + '-' unless prefix.end_with?('-')
      encoding = response.body.encoding
      tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
      @tempfile = tempfile
      tempfile.write(response.body)
      response.on_complete do |resp|
        path = tempfile.path
        tempfile.close!
        File.open(path, 'wb') { |file| file.write(response.body) }
        @config.logger.info "File written to #{path}. Please move the file to a proper "\
                                "folder for further processing and delete the temp afterwards"
        File.new(path)
      end
    end
def download_file(response)
      tempfile = nil
      encoding = nil
      content_disposition = response.headers['Content-Disposition']
      if content_disposition and content_disposition =~ /filename=/i
        filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
        prefix = sanitize_filename(filename)
      else
        prefix = 'download-'
      end
      prefix = prefix + '-' unless prefix.end_with?('-')
      encoding = response.body.encoding
      tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
      @tempfile = tempfile
      tempfile.write(response.body)
      response.on_complete do |resp|
        path = tempfile.path
        tempfile.close!
        File.open(path, 'wb') { |file| file.write(response.body) }
        @config.logger.info "File written to #{path}. Please move the file to a proper "\
                                "folder for further processing and delete the temp afterwards"
        File.new(path)
      end
    end
Save response body into a file in (the defined) temporary folder, using the filename
 from the "Content-Disposition" header if provided, otherwise a random filename.
 The response body is written to the file in chunks in order to handle files which
 size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
 process can use.

 @see Configuration#temp_folder_path