riboseinc / ribose-ruby

Ribose API in Ruby
MIT License
3 stars 0 forks source link

Download a File or a File Version? #129

Closed ronaldtse closed 5 years ago

ronaldtse commented 6 years ago

I can "fetch" the details using existing syntax, but I needed to download the file... So here's the workaround.

@abunashir could you please help implement this? e.g., Ribose::SpaceFile.all.first.download.

Thanks.

file_id = remote_file[:id]
version_id = remote_file[:versions].first[:current_version_id]

file_location = remote_file._agent.call(:get, "https://www.ribose.com/spaces/#{COLLAB_SPACE_ID}/file/files/#{file_id}/versions/#{version_id}", {headers: {accept: "text/html"}})

unless file_location.headers['status'] =~ /^30[12]/
  puts "Download failed!"
  exit 1
end

File.open(options[:output], "w") do |f|
  x = remote_file._agent.call(:get, file_location.headers['location'], {headers: {accept: "application/octet-stream"}}).data
  f << x
end
abunashir commented 5 years ago

@ronaldtse I've added this feature in the attached PR, in the future we can probably do some imporvements like featching the name and file type dynamically, but since those are addiing additional ruequests so I'm leaving those for now and trusting the user to provide a filename. Please have a look and let me know if there is anything else on this one, thanks!