canvas_faker 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at justinball@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in canvas_faker.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016-2017 Atomic Jolt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # CanvasFaker
2
+
3
+ This gem generates courses, students and other data for an Instructure Canvas instance which
4
+ will help save time if you need to test
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'canvas_faker'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install canvas_faker
21
+
22
+ Rename .env.example to .env and provide a Canvas URL and an API Token
23
+
24
+
25
+ ## Usage
26
+
27
+
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
32
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version,
35
+ update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git
36
+ tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/atomicjolt/canvas_faker.
41
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to
42
+ adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
43
+
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
48
+
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ load "./lib/tasks/canvas_faker.rake"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "canvas_faker"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,45 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require "canvas_faker/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "canvas_faker"
9
+ spec.version = CanvasFaker::VERSION
10
+ spec.authors = ["Justin Ball", "Benjamin Call"]
11
+ spec.email = ["justin.ball@atomicjolt.com"]
12
+
13
+ spec.summary = "Utility for setting up courses and users in Instructure Canvas."
14
+ spec.description = "Atomic Jolt found that we frequently needed to setup courses with " \
15
+ "users to test. This gem handles that requirement."
16
+ spec.homepage = "https://github.com/atomicjolt/canvas_faker"
17
+ spec.license = "MIT"
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
29
+ f.match(%r{^(test|spec|features)/})
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_dependency "lms-api"
36
+ spec.add_dependency "highline"
37
+ spec.add_dependency "faker"
38
+ spec.add_dependency "dotenv"
39
+
40
+ spec.add_development_dependency "bundler", "~> 1.13"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "rspec", "~> 3.0"
43
+ spec.add_development_dependency "byebug", "~> 8.2", ">= 8.2.4"
44
+
45
+ end
@@ -0,0 +1,6 @@
1
+ require "canvas_faker/version"
2
+
3
+ module CanvasFaker
4
+ end
5
+
6
+ require "canvas_faker/functionality"
@@ -0,0 +1,220 @@
1
+ require "highline"
2
+ require "lms_api"
3
+ require "faker"
4
+ require "byebug"
5
+
6
+ module CanvasFaker
7
+
8
+ class Functionality
9
+
10
+ def initialize(canvas_url, token, tools = [])
11
+ @api = LMS::Canvas.new(canvas_url, token)
12
+ @canvas_url = canvas_url
13
+ @tools = tools
14
+ @cli ||= HighLine.new
15
+ end
16
+
17
+ def setup_course
18
+ account_id = get_account_id
19
+ courses = course_list(account_id)
20
+ course_id = create_course(account_id, courses)["id"]
21
+ students = create_users(account_id)
22
+ enroll_user_in_course(students, course_id)
23
+ make_assignments_in_course(course_id)
24
+ install_lti_tool_to_course(course_id)
25
+ end
26
+
27
+ def add_students_to_course(account_id, course_id)
28
+ students = create_users(account_id)
29
+ enroll_user_in_course(students, course_id)
30
+ end
31
+
32
+ def delete_course
33
+ account_id = get_account_id
34
+ courses = course_list(account_id)
35
+ course = @cli.ask("Delete which course? ex.. 2", Integer)
36
+ @api.proxy(
37
+ "CONCLUDE_COURSE",
38
+ { id: courses[course]["id"],
39
+ event: "delete"
40
+ }
41
+ )
42
+ puts "Deleted #{courses[course]['name']}"
43
+ end
44
+
45
+ def get_quizzes(course_id)
46
+ a = @api.proxy(
47
+ "LIST_QUIZZES_IN_COURSE",
48
+ { course_id: course_id }
49
+ )
50
+ puts "QUIZ::: #{a}"
51
+ end
52
+
53
+ def delete_course_by_id(course_id)
54
+ @api.proxy(
55
+ "CONCLUDE_COURSE",
56
+ { id: course_id,
57
+ event: "delete"
58
+ }
59
+ )
60
+ puts "Deleted course with id: #{course_id}"
61
+ end
62
+
63
+ def make_assignments_in_course(course_id)
64
+ num_of_assignments = @cli.ask("How many assignments?", Integer)
65
+ create_assignments_in_course(course_id, num_of_assignments)
66
+ end
67
+
68
+ private
69
+
70
+ def create_assignments_in_course(course_id, num_of_assignments)
71
+ (1..num_of_assignments).map do
72
+ food = Faker::Pokemon.name
73
+ payload = {
74
+ assignment: {
75
+ name: "All about #{food}",
76
+ published: true
77
+ }
78
+ }
79
+ @api.proxy(
80
+ "CREATE_ASSIGNMENT",
81
+ { course_id: course_id },
82
+ payload
83
+ ).tap { |assignment| puts "Creating #{assignment['name']} in your course." }
84
+ end
85
+ puts "Added #{num_of_assignments} assignments to your course"
86
+ end
87
+
88
+ def course_list(account_id)
89
+ courses = @api.proxy(
90
+ "LIST_ACTIVE_COURSES_IN_ACCOUNT",
91
+ { account_id: account_id }
92
+ )
93
+ courses.each_with_index do |course, index|
94
+ puts "#{index}. #{course['name']}, id: (#{course['id']})"
95
+ end
96
+ courses
97
+ end
98
+
99
+ def get_account_id
100
+ accounts = @api.all_accounts # gets the accounts
101
+ accounts.each_with_index do |account, index|
102
+ puts "#{index}. #{account['name']}"
103
+ end
104
+ answer = @cli.ask("Which account? ex.. 2", Integer)
105
+ accounts[answer]["id"]
106
+ end
107
+
108
+ # Returns true or false
109
+ def should_create_course?(existing_course_names, course_name)
110
+ if existing_course_names.include?(course_name)
111
+ use_old_course = @cli.ask "That course already exists, want to use it? [y/n]"
112
+ if use_old_course == "y" || use_old_course == "yes"
113
+ return false
114
+ end
115
+ return true
116
+ end
117
+ return true
118
+ end
119
+
120
+ def create_course(account_id, courses)
121
+ existing_course_names = courses.map { |course| course["name"] }
122
+ course_name = @cli.ask "Name your new course."
123
+ if should_create_course?(existing_course_names, course_name)
124
+ payload = {
125
+ course: {
126
+ name: course_name,
127
+ # sis_course_id: course_id,
128
+ }
129
+ }
130
+ course = @api.proxy(
131
+ "CREATE_NEW_COURSE",
132
+ { account_id: account_id },
133
+ payload
134
+ )
135
+ puts "#{@canvas_url}/courses/#{course['id']}"
136
+ course
137
+ else
138
+ index = existing_course_names.find_index("#{course_name}")
139
+ courses[index]
140
+ end
141
+ end
142
+
143
+ def create_users(account_id)
144
+ num_students = @cli.ask(
145
+ "How many students do you want in your course?",
146
+ Integer
147
+ )
148
+ (1..num_students).map do
149
+ user_first_name = Faker::Name.first_name
150
+ user_last_name = Faker::Name.last_name
151
+ full_name = "#{user_first_name}#{user_last_name}"
152
+ email = Faker::Internet.safe_email(full_name)
153
+ payload = {
154
+ user: {
155
+ name: "#{user_first_name} #{user_last_name}",
156
+ short_name: user_first_name,
157
+ sortable_name: "#{user_last_name}, #{user_first_name}",
158
+ terms_of_use: true,
159
+ skip_registration: true,
160
+ avatar: {
161
+ url: Faker::Avatar.image
162
+ }
163
+ },
164
+ pseudonym: {
165
+ unique_id: "#{email}",
166
+ password: "asdfasdf"
167
+ }
168
+ }
169
+ @api.proxy(
170
+ "CREATE_USER",
171
+ { account_id: account_id },
172
+ payload
173
+ ).tap { |stud| puts "#{stud['name']} creating." }
174
+ end
175
+ end
176
+
177
+ def enroll_user_in_course(students, course_id)
178
+ students.each do |student|
179
+ payload = {
180
+ enrollment: {
181
+ user_id: student["id"],
182
+ type: "StudentEnrollment",
183
+ enrollment_state: "active"
184
+ }
185
+ }
186
+ @api.proxy(
187
+ "ENROLL_USER_COURSES",
188
+ { course_id: course_id },
189
+ payload
190
+ )
191
+ puts "Enrolled #{student['name']} into your course_id #{course_id}"
192
+ end
193
+ end
194
+
195
+ def install_lti_tool_to_course(course_id)
196
+ return if @tools.empty?
197
+ # Taken from canvas documentation, below.
198
+ # https://canvas.instructure.com/doc/api/external_tools.html
199
+ @tools.each_with_index do |tool, index|
200
+ puts "#{index}. #{tool[:app][:lti_key]}"
201
+ end
202
+ tool_index =
203
+ @cli.ask("Which tool do you want to add to your course?", Integer)
204
+ tool = tools[tool_index]
205
+ payload = {
206
+ name: "#{tool[:app][:lti_key]}",
207
+ privacy_level: "public",
208
+ consumer_key: "#{tool[:app][:lti_key]}",
209
+ shared_secret: "#{tool[:app][:lti_secret]}",
210
+ config_type: "by_xml",
211
+ config_xml: "#{tool[:config]}"
212
+ }
213
+ @api.proxy(
214
+ "CREATE_EXTERNAL_TOOL_COURSES",
215
+ { course_id: course_id },
216
+ payload
217
+ )
218
+ end
219
+ end
220
+ end