instructure / pandarus

A Ruby library for the Canvas API (and code generator for other languages, eventually)
34 stars 25 forks source link

sample code for creating zip file for sis upload #11

Closed eriko closed 9 years ago

eriko commented 9 years ago

Is there any chance you have any sample code to bundling up the individual sis csv files into a zip. I have been trying to do it in memory using ruby zip with no success. The example only has one file as it is from the the tasks that updates faculty members. The same code using stringIO.new(data) and not the zip outputstream works. I can build the zip on the file system but I would like to avoid that.

        ar.put_next_entry('users.csv')
        ar.write( @users_csv)
        ar.close_buffer
      end
import = client.import_sis_data(1, {attachment: UploadIO.new(sis_data, "application/zip", "#{filename}.zip"),
                                    import_type: 'instructure_csv',
                                    extension: 'zip'
                                 })
puts import
canadaduane commented 9 years ago

Here's a cobbled together bit of code. Untested, but at least you can compare :)

zipout = "myfile.zip"
files = ["users.csv"]
zip_file = Zip::File.open(zipout, Zip::File::CREATE) do |zip|
  files.each do |path|
    filename = File.basename(path)
    zip.add(filename, path)
  end
  zip
end

payload = Faraday::UploadIO.new(zip_file, 'zip')
@pandarus.import_sis_data(@canvas_account_id, attachment: payload)
eriko commented 9 years ago

It maybe that switching over to the faraday uploadIO will do the trick. Thanks.