Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to upload imscc files #9

Merged
merged 6 commits into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ require "blacklight/tasks"
Blacklight::Tasks.install_tasks
```

Create a `blacklight.yml` and add credentials
```yaml
:canvas_url: <url>
:canvas_token: <token>
```

## Usage

Run the rake task to convert from .zip to .imscc
Expand Down
5 changes: 3 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require "rake/testtask"
require "bundler/setup"
require "bundler/gem_tasks"
require "blacklight/tasks"

Rake.application.options.trace_rules = true

Rake::TestTask.new do |t|
t.name = :spec
t.pattern = "spec/**/*_spec.rb"
Expand All @@ -11,5 +13,4 @@ end

task default: :spec

Bundler::GemHelper.install_tasks
Blacklight::Tasks.install_tasks
Empty file modified bin/console
100644 → 100755
Empty file.
Empty file modified bin/import_blackboard
100644 → 100755
Empty file.
15 changes: 8 additions & 7 deletions blacklight.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ Gem::Specification.new do |spec|
spec.license = "MIT"
spec.extra_rdoc_files = ["README.md"]

spec.files = `git ls-files -z`.split("\x0").
reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.required_ruby_version = ">= 2.0"

spec.files = Dir["LICENSE.txt", "README.md", "lib/**/*", "bin/*"]
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }

spec.add_development_dependency "pry-byebug", "~> 3.4"
spec.add_development_dependency "minitest", "~> 5.9"

spec.metadata["allowed_push_host"] = "https://github.com/atomicjolt/"
spec.add_development_dependency "webmock", "~> 2.1"

[
["rake", "~> 11.3"],
["rubyzip", "~> 1.1"],
["nokogiri", "~> 1.6.6"],
["fileutils", "~> 0.7"],
["require_all", "~> 1.3.3"],
["pandarus", "~> 0.6"],
["activesupport", "~> 4.2"],
["rest-client", "~> 2.0"],
].each { |d| spec.add_runtime_dependency(*d) }
end
8 changes: 8 additions & 0 deletions lib/blacklight.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "blacklight/version"
require "blacklight/xml_parser"
require "blacklight/canvas_course"

require "canvas_cc"
require "optparse"
require "ostruct"
Expand Down Expand Up @@ -88,4 +90,10 @@ def self.create_canvas_course(resources, zip_name)
end
course
end

def self.initialize_course(filename)
metadata = Blacklight::CanvasCourse.metadata_from_file(filename)
course = Blacklight::CanvasCourse.from_metadata(metadata)
course.upload_content(filename)
end
end
101 changes: 101 additions & 0 deletions lib/blacklight/canvas_course.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require "pandarus"
require "blacklight/config"
require "rest-client"

module Blacklight
##
# This class represents a canvas course for which we are uploading data to
##
class CanvasCourse
##
# A new canvas course accepts the metadata for a course
# and the pandarus course resourse
##
def initialize(metadata, course_resource)
@metadata = metadata
@course_resource = course_resource
end

##
# Given a filename to a zip file, extract the necessary metadata
# for the course
##
def self.metadata_from_file(filename)
Zip::File.open(filename) do |file|
settings = "course_settings/course_settings.xml"
config = file.find_entry(settings).get_input_stream.read
doc = Nokogiri::XML(config)
{
name: doc.at("title").text,
}
end
end

##
# Create a new pandarus instance to communicate with the canvas server
##
def self.client
@client ||= Pandarus::Client.new(
prefix: Blacklight.canvas_url,
token: Blacklight.canvas_token,
)
end

##
# Find or Create a new CanvasCourse instance from the given metadata
##
def self.from_metadata(metadata)
course_name = metadata[:name] || metadata[:title]
courses = client.list_active_courses_in_account(:self)
canvas_course = courses.detect { |course| course.name == course_name } ||
client.create_new_course(
:self,
course: {
name: course_name,
},
)
CanvasCourse.new(metadata, canvas_course)
end

##
# Create a migration for the course
# and upload the imscc file to be imported into the course
##
def upload_content(filename)
client = CanvasCourse.client
name = File.basename(filename)
# Create a migration for the course and get S3 upload authorization
migration = client.
create_content_migration_courses(
@course_resource.id,
:canvas_cartridge_importer,
pre_attachment: { name: name },
)

puts "Uploading: #{name}"
upload_to_s3(migration, filename)
puts "Done uploading: #{name}"
end

def upload_to_s3(migration, filename)
# Attach the file to the S3 auth
pre_attachment = migration.pre_attachment
upload_url = pre_attachment["upload_url"]
upload_params = pre_attachment["upload_params"]
upload_params[:file] = File.new(filename, "rb")

# Post to S3
RestClient.post(
upload_url,
upload_params,
) do |response|
# Post to Canvas
RestClient.post(
response.headers[:location],
nil,
Authorization: "Bearer #{Blacklight.canvas_token}",
)
end
end
end
end
19 changes: 19 additions & 0 deletions lib/blacklight/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "yaml"

module Blacklight
def self.canvas_url
Blacklight._config[:canvas_url]
end

def self.canvas_token
Blacklight._config[:canvas_token]
end

def self._config
@config ||= if File.exists? "blacklight.yml"
YAML::load(File.read("blacklight.yml"))
else
{}
end
end
end
41 changes: 37 additions & 4 deletions lib/blacklight/tasks.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
require "rake"
require "rake/clean"
require "blacklight"
require "byebug"

## CHANGED THESE TO CHANGE THE FOLDER LOCATIONS
SOURCE_DIR = "sources".freeze
OUTPUT_DIR = "canvas".freeze
UPLOAD_DIR = "uploaded".freeze

## Don"t change these, these are just getting the last
## of the folder name for the script below"s use
## Don't change these, these are just getting the last
## of the folder name for the script below to use
SOURCE_NAME = SOURCE_DIR.split("/").last
OUTPUT_NAME = OUTPUT_DIR.split("/").last
UPLOAD_NAME = UPLOAD_DIR.split("/").last
SOURCE_FILES = Rake::FileList.new("#{SOURCE_DIR}/*.zip")
CONVERTED_FILES = Rake::FileList.new("#{OUTPUT_DIR}/*.imscc")

def source_for_imscc(imscc_file)
SOURCE_FILES.detect do |f|
Expand All @@ -19,6 +23,23 @@ def source_for_imscc(imscc_file)
end
end

def source_for_upload_log(upload_log)
CONVERTED_FILES.detect do |f|
path = upload_log.pathmap("%{^#{UPLOAD_DIR}/,#{OUTPUT_DIR}/}X")
f.ext("") == path
end
end

def make_directories(name, upload_dir)
mkdir_p name.pathmap("%d")
mkdir_p upload_dir
end

def log_file(name)
sh "touch #{name}"
sh "date >> #{name}"
end

module Blacklight
class Tasks
extend Rake::DSL if defined? Rake::DSL
Expand All @@ -42,11 +63,23 @@ def self.install_tasks
directory OUTPUT_NAME

rule ".imscc" => [->(f) { source_for_imscc(f) }, OUTPUT_NAME] do |t|
mkdir_p t.name.pathmap("%d")
mkdir_p OUTPUT_DIR
make_directories(t.name, OUTPUT_DIR)
Blacklight.parse(SOURCE_DIR, OUTPUT_DIR)
end

desc "Upload converted files to canvas"
task upload: CONVERTED_FILES.pathmap(
"%{^#{OUTPUT_NAME}/,#{UPLOAD_DIR}/}X.txt",
)

directory UPLOAD_NAME

rule ".txt" => [->(f) { source_for_upload_log(f) }, UPLOAD_NAME] do |t|
make_directories(t.name, UPLOAD_DIR)
Blacklight.initialize_course(t.source)
log_file(t.name)
end

desc "Completely delete all converted files"
task :clean do
rm_rf OUTPUT_DIR
Expand Down
26 changes: 26 additions & 0 deletions spec/canvas_course_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "minitest/autorun"
require "blacklight/canvas_course"
require_relative "helpers/spec_helper"
require "byebug"

include Blacklight

describe Blacklight::CanvasCourse do
describe "metadata_from_file" do
it "should return metadata" do
file = fixture_finder("bfcoding-101-export.imscc")
metadata = Blacklight::CanvasCourse.metadata_from_file(file)
assert_equal metadata, name: "bfcoding 101"
end
end

describe "from_metadata" do
it "should return a canvas course" do
name = "bfcoding 101"
stub_active_courses_in_account([{ name: name }])
metadata = { name: name }
course = Blacklight::CanvasCourse.from_metadata(metadata)
assert_kind_of Blacklight::CanvasCourse, course
end
end
end
Binary file added spec/fixtures/bfcoding-101-export.imscc
Binary file not shown.
22 changes: 22 additions & 0 deletions spec/helpers/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "webmock/minitest"
WebMock.disable_net_connect!(allow_localhost: true)

def fixture_finder(name)
File.join(File.dirname(__FILE__), "..", "fixtures", name)
end

def stub_toe(type, pattern, json, with_params = nil)
request = stub_request(type, pattern).
to_return(
status: 200,
body: json,
headers: { content_type: "json" },
)
if with_params
request.with(with_params)
end
end

def stub_active_courses_in_account(json)
stub_toe(:get, /v1\/accounts\/self\/courses/, json)
end