moodle_rb 0.0.5

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.
Files changed (50) hide show
  1. data/.gitignore +35 -0
  2. data/Gemfile +2 -0
  3. data/LICENSE +22 -0
  4. data/README.md +108 -0
  5. data/Rakefile +1 -0
  6. data/lib/moodle_rb/categories.rb +94 -0
  7. data/lib/moodle_rb/client.rb +44 -0
  8. data/lib/moodle_rb/courses.rb +101 -0
  9. data/lib/moodle_rb/enrolments.rb +39 -0
  10. data/lib/moodle_rb/error.rb +18 -0
  11. data/lib/moodle_rb/grades.rb +42 -0
  12. data/lib/moodle_rb/users.rb +83 -0
  13. data/lib/moodle_rb/utility.rb +25 -0
  14. data/lib/moodle_rb/version.rb +7 -0
  15. data/lib/moodle_rb.rb +16 -0
  16. data/moodle_rb.gemspec +27 -0
  17. data/spec/cassettes/MoodleRb_Categories/_create/.yml +39 -0
  18. data/spec/cassettes/MoodleRb_Categories/_create/when_validation_fails/.yml +39 -0
  19. data/spec/cassettes/MoodleRb_Categories/_destroy/.yml +75 -0
  20. data/spec/cassettes/MoodleRb_Categories/_index/.yml +39 -0
  21. data/spec/cassettes/MoodleRb_Categories/_show/.yml +39 -0
  22. data/spec/cassettes/MoodleRb_Client/_site_info/when_invalid_token/.yml +39 -0
  23. data/spec/cassettes/MoodleRb_Client/_site_info/when_valid_token/.yml +39 -0
  24. data/spec/cassettes/MoodleRb_Courses/_create/.yml +39 -0
  25. data/spec/cassettes/MoodleRb_Courses/_create/when_validation_fails/.yml +75 -0
  26. data/spec/cassettes/MoodleRb_Courses/_destroy/.yml +75 -0
  27. data/spec/cassettes/MoodleRb_Courses/_enrolled_users/.yml +75 -0
  28. data/spec/cassettes/MoodleRb_Courses/_index/.yml +39 -0
  29. data/spec/cassettes/MoodleRb_Courses/_show/.yml +39 -0
  30. data/spec/cassettes/MoodleRb_Enrolments/_create/.yml +39 -0
  31. data/spec/cassettes/MoodleRb_Enrolments/_create/when_user_or_course_id_is_invalid/.yml +39 -0
  32. data/spec/cassettes/MoodleRb_Grades/_by_assignment/.yml +39 -0
  33. data/spec/cassettes/MoodleRb_Grades/_by_course/.yml +39 -0
  34. data/spec/cassettes/MoodleRb_Users/_create/.yml +39 -0
  35. data/spec/cassettes/MoodleRb_Users/_create/when_missing_required_parameters/.yml +39 -0
  36. data/spec/cassettes/MoodleRb_Users/_destroy/.yml +39 -0
  37. data/spec/cassettes/MoodleRb_Users/_destroy/when_id_does_not_exist/.yml +39 -0
  38. data/spec/cassettes/MoodleRb_Users/_enrolled_courses/.yml +39 -0
  39. data/spec/cassettes/MoodleRb_Users/_show/.yml +39 -0
  40. data/spec/lib/moodle_rb/categories_spec.rb +74 -0
  41. data/spec/lib/moodle_rb/client_spec.rb +45 -0
  42. data/spec/lib/moodle_rb/courses_spec.rb +92 -0
  43. data/spec/lib/moodle_rb/enrolments_spec.rb +39 -0
  44. data/spec/lib/moodle_rb/error_spec.rb +20 -0
  45. data/spec/lib/moodle_rb/grades_spec.rb +32 -0
  46. data/spec/lib/moodle_rb/users_spec.rb +87 -0
  47. data/spec/lib/moodle_rb/utility_spec.rb +23 -0
  48. data/spec/lib/moodle_rb_spec.rb +11 -0
  49. data/spec/spec_helper.rb +25 -0
  50. metadata +251 -0
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ Gemfile.lock
31
+ .ruby-version
32
+ .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 JobReady Solutions
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # MoodleRb
2
+ A Ruby Client for the Moodle REST API.
3
+
4
+ ## Usage
5
+
6
+ Initialise a MoodleRb client
7
+ ```
8
+ moodle = MoodleRb.new(webservices_token, moodle_url)
9
+ ```
10
+
11
+ Get site information
12
+ ```
13
+ moodle.site_info
14
+ ```
15
+
16
+ ### Courses
17
+
18
+ Create a course
19
+ ```
20
+ moodle.courses.create(
21
+ :full_name => 'My Course',
22
+ :short_name => 'MyC',
23
+ :parent_category => 5,
24
+ :idnumber => 'ExtRef'
25
+ )
26
+ ```
27
+
28
+ List all courses
29
+ ```
30
+ moodle.courses.index
31
+ ```
32
+
33
+ Show a course
34
+ ```
35
+ moodle.courses.show(course_id)
36
+ ```
37
+
38
+ Delete a course
39
+ ```
40
+ moodle.courses.destroy(course_id)
41
+ ```
42
+
43
+ ### Categories
44
+
45
+ Create a category
46
+ ```
47
+ moodle.categories.create(
48
+ :name => 'Test category'
49
+ )
50
+ ```
51
+
52
+ List all categories
53
+ ```
54
+ moodle.categories.index
55
+ ```
56
+
57
+ Show a category
58
+ ```
59
+ moodle.categories.show(category_id)
60
+ ```
61
+
62
+ Delete a category
63
+ ```
64
+ moodle.categories.destroy(category_id)
65
+ ```
66
+
67
+ ### Enrolments
68
+
69
+ Create an enrolment
70
+ ```
71
+ moodle.enrolments.create(
72
+ :user_id => user_id,
73
+ :course_id => course_id
74
+ )
75
+ ```
76
+
77
+ View enrolled users by course
78
+ ```
79
+ moodle.courses.enrolled_users(course_id)
80
+ ```
81
+
82
+ View enrolled courses by user
83
+ ```
84
+ moodle.users.enrolled_courses(user_id)
85
+ ```
86
+
87
+ ### Users
88
+
89
+ Create a user
90
+ ```
91
+ moodle.users.create(
92
+ :username => 'brucew',
93
+ :password => 'password',
94
+ :firstname => 'Bruce',
95
+ :lastname => 'Wayne',
96
+ :email => 'bruce@wayneenterprises.com'
97
+ )
98
+ ```
99
+
100
+ Show a user
101
+ ```
102
+ moodle.users.show(user_id)
103
+ ```
104
+
105
+ Delete a user
106
+ ```
107
+ moodle.users.destroy(user_id)
108
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,94 @@
1
+ module MoodleRb
2
+ class Categories
3
+ include HTTParty
4
+ include Utility
5
+
6
+ attr_reader :token
7
+ ROOT_CATEGORY = 0
8
+
9
+ def initialize(token, url)
10
+ @token = token
11
+ self.class.base_uri url
12
+ end
13
+
14
+ def index
15
+ response = self.class.get(
16
+ '/webservice/rest/server.php',
17
+ {
18
+ :query => query_hash('core_course_get_categories', token)
19
+ }
20
+ )
21
+ response.parsed_response
22
+ end
23
+
24
+ # required params:
25
+ # name
26
+ # optional params:
27
+ # parent_category
28
+ # the parent category id inside which the new category will be created
29
+ # - set to nil for a root category
30
+ # idnumber
31
+ # the new category external reference. must be unique
32
+ # description
33
+ # the new category description
34
+ def create(params)
35
+ response = self.class.post(
36
+ '/webservice/rest/server.php',
37
+ {
38
+ :query => query_hash('core_course_create_categories', token),
39
+ :body => {
40
+ :categories => {
41
+ '0' => {
42
+ :name => params[:name],
43
+ :parent => (params[:parent_category] || ROOT_CATEGORY),
44
+ :idnumber => params[:idnumber],
45
+ :description => params[:description]
46
+ }
47
+ }
48
+ }
49
+ }
50
+ )
51
+ if error_response?(response)
52
+ raise MoodleError.new(response.parsed_response)
53
+ else
54
+ response.parsed_response.first
55
+ end
56
+ end
57
+
58
+ def show(id)
59
+ response = self.class.post(
60
+ '/webservice/rest/server.php',
61
+ {
62
+ :query => query_hash('core_course_get_categories', token),
63
+ :body => {
64
+ :criteria => {
65
+ '0' => {
66
+ :key => 'id',
67
+ :value => id
68
+ }
69
+ }
70
+ }
71
+ }
72
+ )
73
+ response.parsed_response.first
74
+ end
75
+
76
+ def destroy(id)
77
+ response = self.class.post(
78
+ '/webservice/rest/server.php',
79
+ {
80
+ :query => query_hash('core_course_delete_categories', token),
81
+ :body => {
82
+ :categories => {
83
+ '0' => {
84
+ :id => id,
85
+ :recursive => 1
86
+ }
87
+ }
88
+ }
89
+ }
90
+ )
91
+ response.parsed_response.nil?
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,44 @@
1
+ module MoodleRb
2
+ class Client
3
+ include HTTParty
4
+ include Utility
5
+
6
+ attr_reader :token, :url
7
+
8
+ def initialize(token, url)
9
+ @token = token
10
+ @url = url
11
+ self.class.base_uri url
12
+ end
13
+
14
+ def site_info
15
+ response = self.class.get(
16
+ '/webservice/rest/server.php',
17
+ {
18
+ :query => query_hash('core_webservice_get_site_info', token)
19
+ }
20
+ )
21
+ response.parsed_response
22
+ end
23
+
24
+ def courses
25
+ MoodleRb::Courses.new(token, url)
26
+ end
27
+
28
+ def categories
29
+ MoodleRb::Categories.new(token, url)
30
+ end
31
+
32
+ def users
33
+ MoodleRb::Users.new(token, url)
34
+ end
35
+
36
+ def enrolments
37
+ MoodleRb::Enrolments.new(token, url)
38
+ end
39
+
40
+ def grades
41
+ MoodleRb::Grades.new(token, url)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,101 @@
1
+ module MoodleRb
2
+ class Courses
3
+ include HTTParty
4
+ include Utility
5
+
6
+ attr_reader :token
7
+
8
+ def initialize(token, url)
9
+ @token = token
10
+ self.class.base_uri url
11
+ end
12
+
13
+ def index
14
+ response = self.class.get(
15
+ '/webservice/rest/server.php',
16
+ {
17
+ :query => query_hash('core_course_get_courses', token)
18
+ }
19
+ )
20
+ response.parsed_response
21
+ end
22
+
23
+ # required params:
24
+ # full_name
25
+ # short_name
26
+ # must be unique
27
+ # parent_category
28
+ # the parent category id inside which the new category will be created
29
+ # optional params:
30
+ # idnumber
31
+ # the new course external reference. must be unique
32
+ def create(params)
33
+ response = self.class.post(
34
+ '/webservice/rest/server.php',
35
+ {
36
+ :query => query_hash('core_course_create_courses', token),
37
+ :body => {
38
+ :courses => {
39
+ '0' => {
40
+ :fullname => params[:full_name],
41
+ :shortname => params[:short_name],
42
+ :categoryid => params[:parent_category],
43
+ :idnumber => params[:idnumber]
44
+ }
45
+ }
46
+ }
47
+ }
48
+ )
49
+ if error_response?(response)
50
+ raise MoodleError.new(response.parsed_response)
51
+ else
52
+ response.parsed_response.first
53
+ end
54
+ end
55
+
56
+ def show(id)
57
+ response = self.class.post(
58
+ '/webservice/rest/server.php',
59
+ {
60
+ :query => query_hash('core_course_get_courses', token),
61
+ :body => {
62
+ :options => {
63
+ :ids => {
64
+ '0' => id
65
+ }
66
+ }
67
+ }
68
+ }
69
+ )
70
+ response.parsed_response.first
71
+ end
72
+
73
+ def destroy(id)
74
+ response = self.class.post(
75
+ '/webservice/rest/server.php',
76
+ {
77
+ :query => query_hash('core_course_delete_courses', token),
78
+ :body => {
79
+ :courseids => {
80
+ '0' => id
81
+ }
82
+ }
83
+ }
84
+ )
85
+ response.parsed_response.nil?
86
+ end
87
+
88
+ def enrolled_users(course_id)
89
+ response = self.class.post(
90
+ '/webservice/rest/server.php',
91
+ {
92
+ :query => query_hash('core_enrol_get_enrolled_users', token),
93
+ :body => {
94
+ :courseid => course_id
95
+ }
96
+ }
97
+ )
98
+ response.parsed_response
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,39 @@
1
+ module MoodleRb
2
+ class Enrolments
3
+ include HTTParty
4
+ include Utility
5
+
6
+ attr_reader :token
7
+ STUDENT_ROLE_ID = 5
8
+
9
+ def initialize(token, url)
10
+ @token = token
11
+ self.class.base_uri url
12
+ end
13
+
14
+ # required params:
15
+ # user_id course_id
16
+ def create(params)
17
+ response = self.class.post(
18
+ '/webservice/rest/server.php',
19
+ {
20
+ :query => query_hash('enrol_manual_enrol_users', token),
21
+ :body => {
22
+ :enrolments => {
23
+ '0' => {
24
+ :userid => params[:user_id],
25
+ :courseid => params[:course_id],
26
+ :roleid => STUDENT_ROLE_ID
27
+ }
28
+ }
29
+ }
30
+ }
31
+ )
32
+ if error_response?(response)
33
+ raise MoodleError.new(response.parsed_response)
34
+ else
35
+ response.code == 200 && response.parsed_response.nil?
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,18 @@
1
+ module MoodleRb
2
+ class MoodleError < StandardError
3
+ attr_reader :response_body, :code, :message
4
+
5
+ def initialize(response_body)
6
+ @response_body = response_body
7
+
8
+ if response_body.is_a?(Hash)
9
+ @code = response_body["errorcode"]
10
+ @message = response_body["message"]
11
+ else
12
+ @message = response_body.to_s
13
+ end
14
+
15
+ super(response_body.inspect)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ module MoodleRb
2
+ class Grades
3
+ include HTTParty
4
+ include Utility
5
+
6
+ attr_reader :token
7
+
8
+ def initialize(token, url)
9
+ @token = token
10
+ self.class.base_uri url
11
+ end
12
+
13
+ def by_assignment(assignment_id)
14
+ response = self.class.post(
15
+ '/webservice/rest/server.php',
16
+ {
17
+ :query => query_hash('mod_assign_get_grades', token),
18
+ :body => {
19
+ :assignmentids => api_array(assignment_id)
20
+ }
21
+ }
22
+ )
23
+ response.parsed_response.is_a?(Hash) &&
24
+ response.parsed_response['assignments']
25
+ end
26
+
27
+ def by_course(course_id, *user_ids)
28
+ response = self.class.post(
29
+ '/webservice/rest/server.php',
30
+ {
31
+ :query => query_hash('core_grades_get_grades', token),
32
+ :body => {
33
+ :courseid => course_id,
34
+ :userids => api_array(user_ids)
35
+ }
36
+ }
37
+ )
38
+ response.parsed_response.is_a?(Hash) &&
39
+ response.parsed_response['items']
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,83 @@
1
+ module MoodleRb
2
+ class Users
3
+ include HTTParty
4
+ include Utility
5
+
6
+ attr_reader :token
7
+
8
+ def initialize(token, url)
9
+ @token = token
10
+ self.class.base_uri url
11
+ end
12
+
13
+ # required params:
14
+ # username password firstname lastname email
15
+ # optional params:
16
+ # idnumber
17
+ # An arbitrary ID code number perhaps from the institution
18
+ def create(params)
19
+ response = self.class.post(
20
+ '/webservice/rest/server.php',
21
+ {
22
+ :query => query_hash('core_user_create_users', token),
23
+ :body => {
24
+ :users => {
25
+ '0' => params
26
+ }
27
+ }
28
+ }
29
+ )
30
+ if error_response?(response)
31
+ raise MoodleError.new(response.parsed_response)
32
+ else
33
+ response.parsed_response.first
34
+ end
35
+ end
36
+
37
+ def show(id)
38
+ response = self.class.post(
39
+ '/webservice/rest/server.php',
40
+ {
41
+ :query => query_hash('core_user_get_users', token),
42
+ :body => {
43
+ :criteria => {
44
+ '0' => {
45
+ :key => 'id',
46
+ :value => id
47
+ }
48
+ }
49
+ }
50
+ }
51
+ )
52
+ response.parsed_response['users'] && response.parsed_response['users'].first
53
+ end
54
+
55
+ def destroy(id)
56
+ response = self.class.post(
57
+ '/webservice/rest/server.php',
58
+ {
59
+ :query => query_hash('core_user_delete_users', token),
60
+ :body => {
61
+ :userids => {
62
+ '0' => id
63
+ }
64
+ }
65
+ }
66
+ )
67
+ response.parsed_response.nil?
68
+ end
69
+
70
+ def enrolled_courses(user_id)
71
+ response = self.class.post(
72
+ '/webservice/rest/server.php',
73
+ {
74
+ :query => query_hash('core_enrol_get_users_courses', token),
75
+ :body => {
76
+ :userid => user_id
77
+ }
78
+ }
79
+ )
80
+ response.parsed_response
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,25 @@
1
+ module MoodleRb
2
+ module Utility
3
+ def query_hash(function, token)
4
+ {
5
+ :wsfunction => function,
6
+ :moodlewsrestformat => 'json',
7
+ :wstoken => token
8
+ }
9
+ end
10
+
11
+ # The Moodle API requires array arguments to be in this format
12
+ def api_array(*array)
13
+ {}.tap do |h|
14
+ array.flatten.each_with_index do |x, i|
15
+ h[i] = x
16
+ end
17
+ end
18
+ end
19
+
20
+ def error_response?(response)
21
+ response && response.parsed_response.is_a?(Hash) &&
22
+ response.parsed_response.has_key?('exception')
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module MoodleRb
2
+ VERSION = '0.0.5' unless defined?(self::VERSION)
3
+
4
+ def self.version
5
+ VERSION
6
+ end
7
+ end
data/lib/moodle_rb.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'httparty'
2
+ require 'moodle_rb/version'
3
+ require 'moodle_rb/utility'
4
+ require 'moodle_rb/error'
5
+ require 'moodle_rb/client'
6
+ require 'moodle_rb/courses'
7
+ require 'moodle_rb/categories'
8
+ require 'moodle_rb/enrolments'
9
+ require 'moodle_rb/grades'
10
+ require 'moodle_rb/users'
11
+
12
+ module MoodleRb
13
+ def self.new(token, url)
14
+ Client.new(token, url)
15
+ end
16
+ end
data/moodle_rb.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'moodle_rb/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'moodle_rb'
7
+ spec.version = MoodleRb::VERSION
8
+ spec.authors = ['Sam Giffney']
9
+ spec.email = ['samg@jobready.com.au']
10
+ spec.summary = %q{ A Ruby client for the Moodle API }
11
+ spec.homepage = 'https://github.com/jobready/moodle-rb'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_development_dependency 'bundler', '~> 1.3'
20
+ spec.add_development_dependency 'ruby-debug', '0.10.4'
21
+ spec.add_development_dependency 'rake', '~> 0'
22
+ spec.add_development_dependency 'rspec', '~> 3.0'
23
+ spec.add_development_dependency 'webmock', '~> 1.0'
24
+ spec.add_development_dependency 'vcr', '~> 2.9'
25
+
26
+ spec.add_dependency 'httparty', '~> 0.11.0'
27
+ end