canvas-tools 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +2 -0
- data/canvas-tools.gemspec +22 -0
- data/exe/canvas-addusers.rb +180 -0
- data/lib/canvas-tools.rb +0 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3941f3a71b4ae2ecd1d8803d6f08269f7da80c75
|
4
|
+
data.tar.gz: c57360b4557adca9bb2fe05417cc592ea9dd5c0e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0c0ab6b9df77b1608dec5878790b8a1a3bc227cf3bea9cfcd40f5befc22d401c09d515f877d86e641e985e0db5b9ce7546e059f2b0760bfeac8dd7abad21d2c9
|
7
|
+
data.tar.gz: 187f5d479ce1b2967af5a757000c6f2bb1bd202cb1b08fa6bbdcf446dac5893e0d8a1dbcc2045492b80bb0506d2d3014a6594690fc6307f1395fe7d6d4cc18a8
|
data/Gemfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'canvas-tools'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2016-09-26'
|
5
|
+
s.summary = "Basic commmand line tools for canvas"
|
6
|
+
s.description = ""
|
7
|
+
s.authors = ["Dr. Ogg"]
|
8
|
+
s.email = ["ogg@sr375.com"]
|
9
|
+
# s.files = ["lib/canvas-tools.rb"]
|
10
|
+
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
11
|
+
s.bindir = "exe"
|
12
|
+
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
13
|
+
s.require_paths = ["lib"]
|
14
|
+
s.homepage = ''
|
15
|
+
s.license = 'MIT'
|
16
|
+
|
17
|
+
s.add_dependency "bundler", "~> 1.12"
|
18
|
+
s.add_dependency "rake", "~> 10.0"
|
19
|
+
s.add_dependency 'console_table', '~> 0.2.4'
|
20
|
+
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'getoptlong'
|
4
|
+
require 'colorize'
|
5
|
+
require 'json'
|
6
|
+
require 'fileutils'
|
7
|
+
require 'rest-client'
|
8
|
+
require 'csv'
|
9
|
+
require 'console_table'
|
10
|
+
|
11
|
+
|
12
|
+
@config_file = "#{ENV["HOME"]}/.canvas-tools.json"
|
13
|
+
@active_config = ""
|
14
|
+
@file = ""
|
15
|
+
|
16
|
+
def generate_example_config
|
17
|
+
example_config = {
|
18
|
+
:learnistute => {
|
19
|
+
:url => "https://lms.someschool.org",
|
20
|
+
:token => "PLACE YOUR ADMIN TOKEN HERE.",
|
21
|
+
:account_id => 1
|
22
|
+
}
|
23
|
+
}
|
24
|
+
if !File.exists? @config_file
|
25
|
+
puts "Config file not found".colorize(:red)
|
26
|
+
puts "Generating example config: #{@config_file}".colorize(:yellow)
|
27
|
+
File.open(@config_file,"w") do |f|
|
28
|
+
f.write(JSON.pretty_generate(example_config))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def load_config
|
34
|
+
if File.exists? @config_file
|
35
|
+
puts "Found config file: #{@config_file}"
|
36
|
+
begin
|
37
|
+
@config = JSON.parse(File.read @config_file)
|
38
|
+
rescue
|
39
|
+
puts "I'm So Sorry, I can't open or read or find your config file (#{@config_file})".colorize(:red)
|
40
|
+
exit 1
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def generate_example_csv
|
46
|
+
filename = "canvas-addusers-example.csv"
|
47
|
+
example_data = [
|
48
|
+
["Name","e-mail","SIS_ID"],
|
49
|
+
["Fake3 middle Student","Fake3@example.com","4043.6"],
|
50
|
+
["Fake2 Student-dash","Fake2@example.com","4044.6"],
|
51
|
+
["Fake1 Student","Fake1@example.com","4042.6"]
|
52
|
+
]
|
53
|
+
if File.exists? filename
|
54
|
+
puts "I have already created an example, if you want to repeat this, try renameing the file".colorize(:red)
|
55
|
+
else
|
56
|
+
puts "Generating example csv: #{filename}".colorize(:yellow)
|
57
|
+
s = File.open(filename,"w")
|
58
|
+
csv = CSV.new(s, :force_quotes => true)
|
59
|
+
example_data.each do |row|
|
60
|
+
csv << row
|
61
|
+
end
|
62
|
+
s.close
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
def display_help
|
68
|
+
puts
|
69
|
+
puts "This tool is used to add users to your canvas install using an CSV file"
|
70
|
+
puts "Config File: #{@config_file.colorize(:blue)}"
|
71
|
+
puts
|
72
|
+
puts "Options:"
|
73
|
+
puts "-h or --help ".ljust(30) +"-> Display this help message"
|
74
|
+
puts "-c or --config".ljust(30) +"-> Specifiy Config to use"
|
75
|
+
puts "-f or --file".ljust(30) +"-> CSV File"
|
76
|
+
puts "-l or --list".ljust(30) + "-> List Valid Configs"
|
77
|
+
puts "-g or --generate".ljust(30) + "-> Generate an example csv file"
|
78
|
+
puts
|
79
|
+
exit 1
|
80
|
+
end
|
81
|
+
|
82
|
+
def parse_cli
|
83
|
+
if ARGV[0] == nil
|
84
|
+
display_help
|
85
|
+
end
|
86
|
+
|
87
|
+
parser = GetoptLong.new
|
88
|
+
parser.set_options(["-h", "--help", GetoptLong::NO_ARGUMENT],
|
89
|
+
["-c", "--config", GetoptLong::NO_ARGUMENT],
|
90
|
+
["-f", "--fqdn", GetoptLong::NO_ARGUMENT],
|
91
|
+
["-l", "--list", GetoptLong::NO_ARGUMENT],
|
92
|
+
["-g", "--generate", GetoptLong::NO_ARGUMENT]
|
93
|
+
|
94
|
+
)
|
95
|
+
|
96
|
+
begin
|
97
|
+
begin
|
98
|
+
opt,arg = parser.get_option
|
99
|
+
break if not opt
|
100
|
+
case opt
|
101
|
+
when "-h" || "--help"
|
102
|
+
display_help
|
103
|
+
exit
|
104
|
+
when "-c" || "--config"
|
105
|
+
@active_config = ARGV[0].strip().downcase()
|
106
|
+
when "-f" || "--file"
|
107
|
+
@file = ARGV[0].strip()
|
108
|
+
when "-l" || "--list"
|
109
|
+
list
|
110
|
+
when "-g" || "--generate"
|
111
|
+
generate_example_csv
|
112
|
+
end
|
113
|
+
rescue => err
|
114
|
+
puts "#{err.class()}: #{err.message}"
|
115
|
+
puts "this should never happen, Did'nt i warn you?".red.blink
|
116
|
+
display_help
|
117
|
+
exit
|
118
|
+
end
|
119
|
+
end while 1
|
120
|
+
end
|
121
|
+
|
122
|
+
def list
|
123
|
+
table_config = [
|
124
|
+
{:key=>:name, :size=>19, :title=>"Name"},
|
125
|
+
{:key=>:url, :size=>35, :title=>"URL"},
|
126
|
+
{:key=>:account_id, :size=>15, :title=>"Account ID"}
|
127
|
+
]
|
128
|
+
ConsoleTable.define(table_config) do |table|
|
129
|
+
@config.each do |r|
|
130
|
+
table << [
|
131
|
+
r[0].dup.colorize(:yellow),
|
132
|
+
r[1]["url"],
|
133
|
+
r[1]["account_id"]
|
134
|
+
]
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def create_user(sis_id,full_name,email)
|
140
|
+
# application/x-www-form-urlencoded
|
141
|
+
# POST /api/v1/accounts/:account_id/users
|
142
|
+
sel_config = @config[@active_config]
|
143
|
+
parms = {
|
144
|
+
"pseudonym[unique_id]" => email,
|
145
|
+
"pseudonym[force_self_registration]" => true,
|
146
|
+
"user[name]" => full_name,
|
147
|
+
"pseudonym[sis_user_id]" => sis_id,
|
148
|
+
"communication_channel[type]" => "email",
|
149
|
+
"communication_channel[address]" => email,
|
150
|
+
"pseudonym[send_confirmation]" => true,
|
151
|
+
}
|
152
|
+
begin
|
153
|
+
RestClient.post "#{sel_config["canvas"]["url"]}/api/v1/accounts/#{sel_config["canvas"]["account_id"]}/users?access_token=#{sel_config["canvas"]["token"]}", parms, :accept => :json
|
154
|
+
rescue RestClient::RequestFailed => e
|
155
|
+
puts "The request failed with HTTP status code #{e.response.code}"
|
156
|
+
puts "The body was:"
|
157
|
+
puts e.response.body
|
158
|
+
'{"id":0}'
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
def run_import
|
164
|
+
if @file.nil? or !File.file? @file
|
165
|
+
puts "Import file not found, please check your path".red
|
166
|
+
end
|
167
|
+
new_user_list = CSV.read(@file, headers:true)
|
168
|
+
new_user_list.each do |u|
|
169
|
+
puts "Adding #{u["Name"]}, #{u["e-mail"]}".blue
|
170
|
+
result = JSON.parse(create_user(u["SIS_ID"],u["Name"],u["e-mail"]))
|
171
|
+
puts "Canvas ID: #{result["id"]}".yellow
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
generate_example_config
|
176
|
+
load_config
|
177
|
+
parse_cli
|
178
|
+
if @file != "" && @active_config != ""
|
179
|
+
run_import
|
180
|
+
end
|
data/lib/canvas-tools.rb
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: canvas-tools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dr. Ogg
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: console_table
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.2.4
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.2.4
|
55
|
+
description: ''
|
56
|
+
email:
|
57
|
+
- ogg@sr375.com
|
58
|
+
executables:
|
59
|
+
- canvas-addusers.rb
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- Gemfile
|
64
|
+
- canvas-tools.gemspec
|
65
|
+
- exe/canvas-addusers.rb
|
66
|
+
- lib/canvas-tools.rb
|
67
|
+
homepage: ''
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.5.1
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Basic commmand line tools for canvas
|
91
|
+
test_files: []
|