Ruby Sample Code Courses

This demonstrates how to create, read, update and delete a course. It does not demonstrate how to add slides to a course nor alter what the course actually shows.

You will need an authorization token. To get a token, follow the instructions in the API overview.

 # =====================================================================
 #
 # Sample code for Bridge Course API's
 # Documents how to Create, Read, Update and Delete Bridge Courses.
 # This does not demonstrate how to upload SCORM courses.
 # 
 # =====================================================================

 require 'rest_client'
 require 'json'

 # Global path to Bridge URL. Your URL will be different.
 $bridge_url = "http://bridge.local.bridgeops.sh:3000"
 $auth_token = "your authorization token"


 # As an author, create some courses
 # Note that this alone will not 'Publish a course'
 def create_courses(token)
   url = "#{$bridge_url}/api/author/course_templates"
   one = {
     "title" => "Star Wars",
     "is_published" => true,
     "has_unpublished_changes" => false,
     "default_days_until_due" => 9,
     "course_type" => "bridge",
     "has_certificate" => true,
     "passing_threshold" => 100
   }
   two = {
     "title" => "Empire Strikes Back",
     "is_published" => true,
     "has_unpublished_changes" => false,
     "default_days_until_due" => 11,
     "course_type" => "bridge",
     "has_certificate" => false,
     "passing_threshold" => 90
   }
   three = {
     "title" => "Return of the Jedi",
     "is_published" => true,
     "has_unpublished_changes" => false,
     "default_days_until_due" => 13,
     "course_type" => "bridge",
     "has_certificate" => true,
     "passing_threshold" => 80
   }
   payload = { "course_templates" => ["one" => one, "two" => two, "three" => three] }
   headers = {AUTHORIZATION: "Basic #{token}"}
   response = RestClient.post(url, payload, headers)
   puts response.description
 end

 def list_courses(token, print = true)
   url = "#{$bridge_url}/api/author/course_templates"
   headers = {AUTHORIZATION: "Basic #{token}"}
   response = RestClient.get(url, headers)
   json_response = JSON.parse(response)
   puts "Found #{json_response["course_templates"].count} courses" if print
   course_ids = []
   json_response["course_templates"].each do |course|
     course_ids << course["id"]
     inspect_course(token, course["id"]) if print
   end
   course_ids
 end

 # List details about a course
 def inspect_course(token, id)
   url = "#{$bridge_url}/api/author/course_templates/#{id}"
   headers = {AUTHORIZATION: "Basic #{token}"}
   response = RestClient.get(url, headers)
   json_response = JSON.parse(response)
   json_response["course_templates"].each do |course|
     puts "#{course["id"]}: #{course["title"]} "
     puts "  estimated time: #{course["estimated_time"]}"
     puts "  required score: #{course["passing_threshold"]}"
     puts "  is published:   #{course["is_published"]}"
     puts "  has changes:    #{course["has_unpublished_changes"]}"
     puts "  due date:       #{course["default_days_until_due"]}"
     puts "  certificate:    #{course["has_certificate"]}"
   end
 end

 # gives the course a new name
 def rename_course(token, id, new_title)
   url = "#{$bridge_url}/api/author/course_templates/#{id}"
   payload = { "course_template" => {
       "title" => new_title,
       # add other entries here to update other properties of the course
   }}
   headers = {AUTHORIZATION: "Basic #{token}"}
   response = RestClient.put(url, payload, headers)
   puts response.description
 end

 # marks a course published
 def publish_courses(token, ids)
   ids.each do |id|
     url = "#{$bridge_url}/api/author/course_templates/#{id}/publish"
     payload = { }
     headers = {AUTHORIZATION: "Basic #{token}"}
     response = RestClient.post(url, payload, headers)
     puts response.description
   end
 end

 # Discards changes made to the content of the course.
 # The content is slides, text, images etc... that make up the body of the course.
 # Changes to the course title, default due date, certificate etc... do not mark the
 # course as changed.
 def discard_changes(token, id)
   url = "#{$bridge_url}/api/author/course_templates/#{id}/discard_changes"
   payload = { }
   headers = {AUTHORIZATION: "Basic #{token}"}
   response = RestClient.post(url, payload, headers)
   puts response.description
 end

 # copies a course.
 def clone_course(token, id)
   url = "#{$bridge_url}/api/author/course_templates/#{id}/clone"
   payload = { }
   headers = {AUTHORIZATION: "Basic #{token}"}
   response = RestClient.post(url, payload, headers)
   puts response.description
 end

 # removes and deletes a course
 def delete_course(token, id)
   url = "#{$bridge_url}/api/author/course_templates/#{id}"
   headers = {AUTHORIZATION: "Basic #{token}"}
   response = RestClient.delete(url, headers)
   puts response.description
 end

 # Exporting a course is not supported through the API. It returns a 403 forbidden if attempted.
 def export_course(token, id)
   url = "#{$bridge_url}/api/author/course_templates/#{id}/export"
   headers = {AUTHORIZATION: "Basic #{token}"}
   response = RestClient.get(url, headers)
   puts response.description
   json_response = JSON.parse(response)
   json_response.each do |course|
     puts "#{course["id"]}: #{course["title"]} "
     puts "  estimated time: #{course["estimated_time"]}"
     puts "  required score: #{course["passing_threshold"]}"
     puts "  published at:   #{course["published_at"]}"
     puts "  has changes:    #{course["has_unpublished_changes"]}"
     puts "  due date:       #{course["default_days_until_due"]}"
     puts "  certificate:    #{course["has_certificate"]}"
   end
 end

 # Exporting a course is not supported through the API. It returns a 403 forbidden if attempted.
 def export_courses(token)
   ids = list_courses(token, false) # need to call this again to get the id of the cloned course
   ids.each do |id|
     export_course(token, id)
   end
 end

 # Start execution
 create_courses($auth_token)
 ids = list_courses($auth_token, false)
 rename_course($auth_token, ids.first, "The Phantom Menace")
 publish_courses($auth_token, ids)
 rename_course($auth_token, ids.last, "Revenge of the Sith")
 discard_changes($auth_token, ids.last)
 clone_course($auth_token, ids[1])
 ids = list_courses($auth_token, false) # update the ids list, and don't print anything
 delete_course($auth_token, ids.first) # delete the copied course
 ids = list_courses($auth_token) # update the ids list