moodle_rb 1.0.1 → 1.0.2
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.
- data/README.md +19 -0
- data/lib/moodle_rb/users.rb +38 -1
- data/lib/moodle_rb/utility.rb +10 -0
- data/lib/moodle_rb/version.rb +1 -1
- data/spec/cassettes/MoodleRb_Users/_search/.yml +39 -0
- data/spec/cassettes/MoodleRb_Users/_update/.yml +39 -0
- data/spec/lib/moodle_rb/users_spec.rb +25 -0
- metadata +8 -4
data/README.md
CHANGED
@@ -108,3 +108,22 @@ Delete a user
|
|
108
108
|
```
|
109
109
|
moodle.users.destroy(user_id)
|
110
110
|
```
|
111
|
+
|
112
|
+
Search for a user
|
113
|
+
```
|
114
|
+
moodle.users.search(:email => 'admin@localhost')
|
115
|
+
```
|
116
|
+
|
117
|
+
Update a user
|
118
|
+
```
|
119
|
+
moodle.users.update(:id => 4, :email => 'bwayne@wayneenterprises.com')
|
120
|
+
```
|
121
|
+
|
122
|
+
## Tests
|
123
|
+
|
124
|
+
You will need to set some environment variables to create new cassettes. We build using MAMP https://www.mamp.info/en/ so it might be
|
125
|
+
|
126
|
+
```
|
127
|
+
export MOODLE_URL=http://localhost:8888/moodle29/
|
128
|
+
export MOODLE_TOKEN=87b95af2df709fa60b395b5c59a3fc2e
|
129
|
+
```
|
data/lib/moodle_rb/users.rb
CHANGED
@@ -79,5 +79,42 @@ module MoodleRb
|
|
79
79
|
)
|
80
80
|
response.parsed_response
|
81
81
|
end
|
82
|
+
|
83
|
+
# input keys must be in the list of supported user columns to search
|
84
|
+
# id, lastname, firstname, idnumber, username, email
|
85
|
+
def search(params = {})
|
86
|
+
response = self.class.post(
|
87
|
+
'/webservice/rest/server.php',
|
88
|
+
{
|
89
|
+
:query => query_hash('core_user_get_users', token),
|
90
|
+
:body => {
|
91
|
+
:criteria => key_value_query_format(params)
|
92
|
+
}
|
93
|
+
}
|
94
|
+
)
|
95
|
+
response.parsed_response['users']
|
96
|
+
end
|
97
|
+
|
98
|
+
# params must include the id of the user
|
99
|
+
# it may include any other standard user attributes:
|
100
|
+
# username, password, firstname, lastname, email ...
|
101
|
+
def update(params)
|
102
|
+
response = self.class.post(
|
103
|
+
'/webservice/rest/server.php',
|
104
|
+
{
|
105
|
+
:query => query_hash('core_user_update_users', token),
|
106
|
+
:body => {
|
107
|
+
:users => {
|
108
|
+
'0' => params
|
109
|
+
}
|
110
|
+
}
|
111
|
+
}
|
112
|
+
)
|
113
|
+
if error_response?(response)
|
114
|
+
raise MoodleError.new(response.parsed_response)
|
115
|
+
else
|
116
|
+
response.response.code == '200'
|
117
|
+
end
|
118
|
+
end
|
82
119
|
end
|
83
|
-
end
|
120
|
+
end
|
data/lib/moodle_rb/utility.rb
CHANGED
@@ -17,6 +17,16 @@ module MoodleRb
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
+
def key_value_query_format(hash)
|
21
|
+
{}.tap do |h|
|
22
|
+
hash.each_with_index do |key_value_array, i|
|
23
|
+
h[i] = {}
|
24
|
+
h[i][:key] = key_value_array[0]
|
25
|
+
h[i][:value] = key_value_array[1]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
20
30
|
def error_response?(response)
|
21
31
|
response && response.parsed_response.is_a?(Hash) &&
|
22
32
|
response.parsed_response.has_key?('exception')
|
data/lib/moodle_rb/version.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: post
|
5
|
+
uri: http://localhost:8888/moodle29/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=core_user_get_users&wstoken=87b95af2df709fa60b395b5c59a3fc2e
|
6
|
+
body:
|
7
|
+
string: criteria[0][value]=Guest%25&criteria[0][key]=firstname
|
8
|
+
headers: {}
|
9
|
+
|
10
|
+
response:
|
11
|
+
status:
|
12
|
+
code: 200
|
13
|
+
message: OK
|
14
|
+
headers:
|
15
|
+
Date:
|
16
|
+
- Wed, 08 Jun 2016 06:25:46 GMT
|
17
|
+
Expires:
|
18
|
+
- Thu, 01 Jan 1970 00:00:00 GMT
|
19
|
+
Pragma:
|
20
|
+
- no-cache
|
21
|
+
X-Powered-By:
|
22
|
+
- PHP/5.6.2
|
23
|
+
Server:
|
24
|
+
- Apache/2.2.29 (Unix) mod_fastcgi/2.4.6 mod_wsgi/3.4 Python/2.7.8 PHP/5.6.2 mod_ssl/2.2.29 OpenSSL/0.9.8zg DAV/2 mod_perl/2.0.8 Perl/v5.20.0
|
25
|
+
Content-Type:
|
26
|
+
- application/json
|
27
|
+
Access-Control-Allow-Origin:
|
28
|
+
- "*"
|
29
|
+
Content-Length:
|
30
|
+
- "339"
|
31
|
+
Accept-Ranges:
|
32
|
+
- none
|
33
|
+
Cache-Control:
|
34
|
+
- private, must-revalidate, pre-check=0, post-check=0, max-age=0
|
35
|
+
body:
|
36
|
+
string: "{\"users\":[{\"id\":1,\"firstname\":\"Guest user\",\"lastname\":\" \",\"fullname\":\"Guest user \",\"email\":\"root@localhost\",\"firstaccess\":0,\"lastaccess\":0,\"profileimageurlsmall\":\"http:\\/\\/localhost:8888\\/moodle29\\/pluginfile.php\\/4\\/user\\/icon\\/f2\",\"profileimageurl\":\"http:\\/\\/localhost:8888\\/moodle29\\/pluginfile.php\\/4\\/user\\/icon\\/f1\"}],\"warnings\":[]}"
|
37
|
+
http_version:
|
38
|
+
recorded_at: Wed, 08 Jun 2016 06:25:46 GMT
|
39
|
+
recorded_with: VCR 2.9.3
|
@@ -0,0 +1,39 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: post
|
5
|
+
uri: http://localhost:8888/moodle29/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=core_user_update_users&wstoken=87b95af2df709fa60b395b5c59a3fc2e
|
6
|
+
body:
|
7
|
+
string: users[0][email]=samg%40jobready.com.au&users[0][id]=4
|
8
|
+
headers: {}
|
9
|
+
|
10
|
+
response:
|
11
|
+
status:
|
12
|
+
code: 200
|
13
|
+
message: OK
|
14
|
+
headers:
|
15
|
+
Expires:
|
16
|
+
- Thu, 01 Jan 1970 00:00:00 GMT
|
17
|
+
X-Powered-By:
|
18
|
+
- PHP/5.6.2
|
19
|
+
Content-Type:
|
20
|
+
- application/json
|
21
|
+
Content-Length:
|
22
|
+
- "4"
|
23
|
+
Date:
|
24
|
+
- Wed, 08 Jun 2016 06:40:27 GMT
|
25
|
+
Access-Control-Allow-Origin:
|
26
|
+
- "*"
|
27
|
+
Accept-Ranges:
|
28
|
+
- none
|
29
|
+
Pragma:
|
30
|
+
- no-cache
|
31
|
+
Server:
|
32
|
+
- Apache/2.2.29 (Unix) mod_fastcgi/2.4.6 mod_wsgi/3.4 Python/2.7.8 PHP/5.6.2 mod_ssl/2.2.29 OpenSSL/0.9.8zg DAV/2 mod_perl/2.0.8 Perl/v5.20.0
|
33
|
+
Cache-Control:
|
34
|
+
- private, must-revalidate, pre-check=0, post-check=0, max-age=0
|
35
|
+
body:
|
36
|
+
string: "null"
|
37
|
+
http_version:
|
38
|
+
recorded_at: Wed, 08 Jun 2016 06:40:27 GMT
|
39
|
+
recorded_with: VCR 2.9.3
|
@@ -84,4 +84,29 @@ describe MoodleRb::Users do
|
|
84
84
|
expect(enrolment).to have_key 'shortname'
|
85
85
|
end
|
86
86
|
end
|
87
|
+
|
88
|
+
describe '#search', :vcr => {
|
89
|
+
:match_requests_on => [:headers], :record => :once
|
90
|
+
} do
|
91
|
+
let(:results) { user_moodle_rb.search({ :firstname => 'Guest%' }) }
|
92
|
+
|
93
|
+
specify do
|
94
|
+
expect(results).to be_a Array
|
95
|
+
expect(results.first).to be_a Hash
|
96
|
+
expect(results.first['firstname']).to eq 'Guest user'
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '#update', :vcr => {
|
101
|
+
:match_requests_on => [:headers], :record => :once
|
102
|
+
} do
|
103
|
+
let(:user_id) { 4 }
|
104
|
+
let(:result) do
|
105
|
+
user_moodle_rb.update(:id => user_id, :email => 'samg@jobready.com.au')
|
106
|
+
end
|
107
|
+
|
108
|
+
specify do
|
109
|
+
expect(result).to eq true
|
110
|
+
end
|
111
|
+
end
|
87
112
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moodle_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sam Giffney
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2016-06-08 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: bundler
|
@@ -173,7 +173,9 @@ files:
|
|
173
173
|
- spec/cassettes/MoodleRb_Users/_destroy/.yml
|
174
174
|
- spec/cassettes/MoodleRb_Users/_destroy/when_id_does_not_exist/.yml
|
175
175
|
- spec/cassettes/MoodleRb_Users/_enrolled_courses/.yml
|
176
|
+
- spec/cassettes/MoodleRb_Users/_search/.yml
|
176
177
|
- spec/cassettes/MoodleRb_Users/_show/.yml
|
178
|
+
- spec/cassettes/MoodleRb_Users/_update/.yml
|
177
179
|
- spec/lib/moodle_rb/categories_spec.rb
|
178
180
|
- spec/lib/moodle_rb/client_spec.rb
|
179
181
|
- spec/lib/moodle_rb/courses_spec.rb
|
@@ -241,7 +243,9 @@ test_files:
|
|
241
243
|
- spec/cassettes/MoodleRb_Users/_destroy/.yml
|
242
244
|
- spec/cassettes/MoodleRb_Users/_destroy/when_id_does_not_exist/.yml
|
243
245
|
- spec/cassettes/MoodleRb_Users/_enrolled_courses/.yml
|
246
|
+
- spec/cassettes/MoodleRb_Users/_search/.yml
|
244
247
|
- spec/cassettes/MoodleRb_Users/_show/.yml
|
248
|
+
- spec/cassettes/MoodleRb_Users/_update/.yml
|
245
249
|
- spec/lib/moodle_rb/categories_spec.rb
|
246
250
|
- spec/lib/moodle_rb/client_spec.rb
|
247
251
|
- spec/lib/moodle_rb/courses_spec.rb
|