Skip to content

Instantly share code, notes, and snippets.

@rivernate
Created February 26, 2016 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rivernate/2832ea3051f946473c33 to your computer and use it in GitHub Desktop.
Save rivernate/2832ea3051f946473c33 to your computer and use it in GitHub Desktop.
require 'httparty'
require 'httmultiparty'
access_token = ""
domain = ""
course_id = ""
assignment_id = ""
file_path = ''
api_endpoint = "#{domain}/api/v1/courses/#{course_id}/assignments/#{assignment_id}/submissions/self/files"
file = File.new(file_path)
filename = File.basename(file_path)
filesize = file.size
headers = {"Authorization" => "Bearer #{access_token}"}
# Provision upload
options = {
headers: headers,
query: {name: filename, size: filesize}
}
response = HTTParty.post(api_endpoint, options)
if response.code != 200
puts 'Provision Failed with #{response.code}'
response
else
response_parsed = JSON.parse(response.body)
upload_params = response_parsed['upload_params'].merge('file' => file, 'Filename' => filename)
upload_url = response_parsed['upload_url']
# Perform upload
options = {query: upload_params, :detect_mime_type => true, no_follow: true}
begin
HTTMultiParty.post(upload_url, options)
rescue HTTParty::RedirectionTooDeep #HTTPParty Doesn't handle redirects on file uploads, so just catch the exception
response = HTTParty.post(upload_params['success_action_redirect'], {headers: headers})
puts response.code
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment