Class: CanvasObject

Inherits:
Forgery
  • Object
show all
Defined in:
lib/models/canvas_object.rb

Class Method Summary collapse

Class Method Details

.push_csv_to_canvas(opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/models/canvas_object.rb', line 3

def self.push_csv_to_canvas(opts={})
  if(opts.nil? || opts[:host].nil? || opts[:token].nil?)
    raise 'Please provide options hash with both :host and :token'
  end
  if(!File.file?(opts[:file]))
    raise 'Please gen_file before trying to push to canvas'
  end
  uri = URI.parse("https://#{opts[:host]}/api/v1/accounts/self/sis_imports.json?import_type=instructure_csv")
  request = Net::HTTP::Post.new(uri)
  request.content_type = 'text/csv'
  request['Authorization'] = "Bearer #{opts[:token]}"
  request.body = ''
  request.body << File.read(opts[:file])

  req_options = {
    use_ssl: uri.scheme == 'https',
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
end