moodle 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/.gitignore +2 -0
- data/Gemfile +8 -0
- data/LICENSE.md +20 -0
- data/README.md +96 -0
- data/Rakefile +8 -0
- data/lib/moodle.rb +39 -0
- data/lib/moodle/client.rb +65 -0
- data/lib/moodle/protocols/rest.rb +13 -0
- data/lib/moodle/services/user.rb +21 -0
- data/moodle.gemspec +13 -0
- data/test/fixtures/config.yml +2 -0
- data/test/test_client.rb +34 -0
- data/test/test_helper.rb +9 -0
- data/test/test_moodle.rb +29 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3ac635580be17fb36b6b0b1f12bffbf9592ff773
|
4
|
+
data.tar.gz: ab7c1465b102719613c1499ad3a84341fea0b703
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 159208b1c4ca10250fd7265ff86bf43e46d177a9dd30941425aa71d99d39af3198e24f2edc474b2bbd17fb48a3869188487dce5bb2330ace753236c24c525717
|
7
|
+
data.tar.gz: 812371e44861ed813083ee50062d85fc52708567a0e6afecf8c69dcb7aa6dc8a58bb05467bbe1865364b02d23b1100aa6ce5e4ce7c4bb83e2cb613a5af9311f2
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Robert Boloc
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
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, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# Moodle
|
2
|
+
Ruby gem to interact with the Moodle via web services.
|
3
|
+
|
4
|
+
## Table of contents
|
5
|
+
- [Installation](#installation)
|
6
|
+
- [Usage](#usage)
|
7
|
+
- [Protocols](#protocols)
|
8
|
+
- [Functions](#functions)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
```shell
|
12
|
+
gem install moodle
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
To use this gem you must first have configured the moodle web services. To do that use the official documentation.
|
17
|
+
|
18
|
+
### Without a token
|
19
|
+
If you don't have a token, you can create an instance of the client using your username and password.
|
20
|
+
The client will then obtain a token for you.
|
21
|
+
```ruby
|
22
|
+
client = Moodle::Client.new(
|
23
|
+
:username => 'myusername',
|
24
|
+
:password => 'secret',
|
25
|
+
:protocol => 'rest',
|
26
|
+
:domain => 'http://mydomain/moodle',
|
27
|
+
:service => 'myservice',
|
28
|
+
:format => 'json'
|
29
|
+
)
|
30
|
+
```
|
31
|
+
|
32
|
+
### Using a token
|
33
|
+
If you already have a token the client can be created without a username and password:
|
34
|
+
```ruby
|
35
|
+
client = Moodle::Client.new(
|
36
|
+
:token => 'b31dde13bade28f25d548a31fa994816',
|
37
|
+
:protocol => 'rest',
|
38
|
+
:domain => 'http://mydomain/moodle',
|
39
|
+
:service => 'myservice',
|
40
|
+
:format => 'json'
|
41
|
+
)
|
42
|
+
```
|
43
|
+
|
44
|
+
### Short syntax
|
45
|
+
When creating the client you can use a shorter and simpler syntax:
|
46
|
+
```ruby
|
47
|
+
client = Moodle.new(
|
48
|
+
:token => 'b31dde13bade28f25d548a31fa994816',
|
49
|
+
:protocol => 'rest',
|
50
|
+
:domain => 'http://mydomain/moodle',
|
51
|
+
:service => 'myservice',
|
52
|
+
:format => 'json'
|
53
|
+
)
|
54
|
+
```
|
55
|
+
|
56
|
+
### Global configuration
|
57
|
+
Configuration can be set globally.
|
58
|
+
```ruby
|
59
|
+
# Using a hash
|
60
|
+
Moodle.configure(
|
61
|
+
:token => 'b31dde13bade28f25d548a31fa994816',
|
62
|
+
:protocol => 'rest',
|
63
|
+
:domain => 'http://mydomain/moodle',
|
64
|
+
:service => 'myservice',
|
65
|
+
:format => 'json'
|
66
|
+
)
|
67
|
+
|
68
|
+
# Using a file
|
69
|
+
Moodle.configure_with(/path/to/yaml/file)
|
70
|
+
|
71
|
+
client = Moodle.new
|
72
|
+
```
|
73
|
+
|
74
|
+
Once you have an instance of the client it's only a matter of calling the moodle web services functions.
|
75
|
+
|
76
|
+
## Protocols
|
77
|
+
Moodle implements 4 protocols: AMF, REST, SOAP, XML-RPC. Currently this gem only supports REST.
|
78
|
+
|
79
|
+
## Functions
|
80
|
+
These are the currently implemented web services functions:
|
81
|
+
|
82
|
+
### core_user_get_users_by_field
|
83
|
+
Retrieve users information for a specified unique field
|
84
|
+
```ruby
|
85
|
+
user = client.core_user_get_users_by_field('id', [2])
|
86
|
+
|
87
|
+
user.id # => 2,
|
88
|
+
user.firstname # => Test
|
89
|
+
user.lastname # => User
|
90
|
+
user.fullname # => Test User
|
91
|
+
user.email # => webservicetester@gmail.com
|
92
|
+
user.firstaccess # => 139240932,
|
93
|
+
user.lastaccess # => 1392471263
|
94
|
+
user.profileimageurlsmall # => http://mydomain/moodle/pluginfile.php/5/user/icon/f2
|
95
|
+
user.profileimageurl # => http://mydomain/moodle/pluginfile.php/5/user/icon/f1
|
96
|
+
```
|
data/Rakefile
ADDED
data/lib/moodle.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
|
3
|
+
require 'moodle/client'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
module Moodle
|
7
|
+
@@config = {
|
8
|
+
:username => nil,
|
9
|
+
:password => nil,
|
10
|
+
:token => nil,
|
11
|
+
:protocol => nil,
|
12
|
+
:domain => nil,
|
13
|
+
:service => nil,
|
14
|
+
:format => 'json'
|
15
|
+
}
|
16
|
+
|
17
|
+
@valid_config_keys = @@config.keys
|
18
|
+
|
19
|
+
# Configuration is for the instance only
|
20
|
+
def self.new(options={})
|
21
|
+
Moodle::Client.new(options)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Configure at global level trough hash
|
25
|
+
def self.configure(options={})
|
26
|
+
options.each {|k,v| @@config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
|
27
|
+
end
|
28
|
+
|
29
|
+
# Configure at global level through yaml file
|
30
|
+
def self.configure_with(path_to_yaml_file)
|
31
|
+
config = YAML::load(IO.read(path_to_yaml_file))
|
32
|
+
configure(config)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Obtain the global configuration
|
36
|
+
def self.config
|
37
|
+
@@config
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'moodle/protocols/rest'
|
2
|
+
require 'moodle/services/user'
|
3
|
+
require 'hashie'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Moodle
|
7
|
+
class Client
|
8
|
+
include Moodle::Service::User
|
9
|
+
|
10
|
+
attr_reader :username, :password, :domain, :protocol, :service, :format, :token
|
11
|
+
|
12
|
+
def initialize(options={})
|
13
|
+
@username = options[:username] || Moodle.config[:username]
|
14
|
+
@password = options[:password] || Moodle.config[:password]
|
15
|
+
@domain = options[:domain] || Moodle.config[:domain]
|
16
|
+
@protocol = options[:protocol] || Moodle.config[:protocol]
|
17
|
+
@service = options[:service] || Moodle.config[:service]
|
18
|
+
@format = options[:format] || Moodle.config[:format]
|
19
|
+
@token = options[:token] || Moodle.config[:token]
|
20
|
+
|
21
|
+
# If no token is provided generate one
|
22
|
+
if @token.nil?
|
23
|
+
@token = self.obtain_token
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Retuns a Moodle::Protocol client instance
|
28
|
+
def client
|
29
|
+
if @client.nil?
|
30
|
+
# Instantiate the client protocol
|
31
|
+
case @protocol
|
32
|
+
when 'rest'
|
33
|
+
@client = Moodle::Protocol::Rest.new
|
34
|
+
else
|
35
|
+
@client = Moodle::Protocol::Rest.new
|
36
|
+
end
|
37
|
+
end
|
38
|
+
@client
|
39
|
+
end
|
40
|
+
|
41
|
+
# Obtains a token from the username and password
|
42
|
+
def obtain_token
|
43
|
+
response = client.request(@domain + '/login/token.php', {
|
44
|
+
:username => @username,
|
45
|
+
:password => @password,
|
46
|
+
:service => @service
|
47
|
+
})
|
48
|
+
# @TODO: deal with error response
|
49
|
+
response.token
|
50
|
+
end
|
51
|
+
|
52
|
+
# Make a request using the desired protocol and format
|
53
|
+
def request(params={})
|
54
|
+
params.merge!(
|
55
|
+
:wstoken => @token,
|
56
|
+
:moodlewsrestformat => @format,
|
57
|
+
:wsfunction => caller[0][/`.*'/][1..-2]
|
58
|
+
)
|
59
|
+
response = client.request(@domain + '/webservice/' + @protocol + '/server.php', params)
|
60
|
+
array_response = JSON.parse response
|
61
|
+
hash_response = Hash[*array_response]
|
62
|
+
Hashie::Mash.new hash_response
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'sanitize'
|
3
|
+
|
4
|
+
module Moodle
|
5
|
+
module Protocol
|
6
|
+
class Rest
|
7
|
+
# Return must be sanitized oh html as debug on moodle site can be active
|
8
|
+
def request(url, params={})
|
9
|
+
Sanitize.clean(RestClient.get(url, :params => params), :remove_contents => true)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Moodle
|
2
|
+
module Service
|
3
|
+
module User
|
4
|
+
# Get users by field
|
5
|
+
def core_user_get_users_by_field(field, values)
|
6
|
+
params = {
|
7
|
+
:field => field
|
8
|
+
}
|
9
|
+
|
10
|
+
# Add all the userids as array params
|
11
|
+
counter = 0
|
12
|
+
values.each do |id|
|
13
|
+
params['values[' + counter.to_s + ']'] = id
|
14
|
+
counter = counter + 1
|
15
|
+
end
|
16
|
+
|
17
|
+
request(params)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/moodle.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = 'moodle'
|
3
|
+
spec.version = '0.0.1'
|
4
|
+
spec.date = '2014-02-14'
|
5
|
+
spec.summary = "Moodle web services from ruby"
|
6
|
+
spec.description = "Interact with Moodle from ruby"
|
7
|
+
spec.authors = ["Robert Boloc"]
|
8
|
+
spec.email = 'robertboloc@gmail.com'
|
9
|
+
spec.files = `git ls-files`.split("\n")
|
10
|
+
spec.homepage =
|
11
|
+
'http://robertboloc.github.com/moodle'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
end
|
data/test/test_client.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'moodle/client'
|
3
|
+
|
4
|
+
class ClientTest < Test::Unit::TestCase
|
5
|
+
# Test initialization with hash
|
6
|
+
def test_initialize_with_hash
|
7
|
+
client = Moodle::Client.new({
|
8
|
+
:username => 'test_username',
|
9
|
+
:password => 'test_password',
|
10
|
+
:domain => 'test_domain',
|
11
|
+
:protocol => 'test_protocol',
|
12
|
+
:service => 'test_service',
|
13
|
+
:format => 'test_format',
|
14
|
+
:token => 'test_token'
|
15
|
+
})
|
16
|
+
|
17
|
+
assert_equal 'test_username', client.username
|
18
|
+
assert_equal 'test_password', client.password
|
19
|
+
assert_equal 'test_domain', client.domain
|
20
|
+
assert_equal 'test_protocol', client.protocol
|
21
|
+
assert_equal 'test_service', client.service
|
22
|
+
assert_equal 'test_format', client.format
|
23
|
+
assert_equal 'test_token', client.token
|
24
|
+
end
|
25
|
+
|
26
|
+
# Test obtaining a token
|
27
|
+
def test_obtain_token
|
28
|
+
client = Moodle::Protocol::Rest.new
|
29
|
+
client.stubs(:request).returns('{"token" : "12345"}')
|
30
|
+
moodle_client = Moodle::Client.new({:token => 'dummy', :domain => 'test'})
|
31
|
+
moodle_client.stubs(:client).returns(client)
|
32
|
+
assert_equal '12345', moodle_client.obtain_token()
|
33
|
+
end
|
34
|
+
end
|
data/test/test_helper.rb
ADDED
data/test/test_moodle.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'moodle'
|
3
|
+
|
4
|
+
class MoodleTest < Test::Unit::TestCase
|
5
|
+
# Test creating new instance
|
6
|
+
def test_new_with_token
|
7
|
+
client = Moodle.new(:token => 'test')
|
8
|
+
assert client.instance_of?(Moodle::Client)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Test that global configuration can be set with hash
|
12
|
+
def test_configure
|
13
|
+
Moodle.configure({
|
14
|
+
:username => 'test_username',
|
15
|
+
:password => 'test_password'
|
16
|
+
})
|
17
|
+
|
18
|
+
assert_equal 'test_username', Moodle.config[:username]
|
19
|
+
assert_equal 'test_password', Moodle.config[:password]
|
20
|
+
end
|
21
|
+
|
22
|
+
# Test that global configuration can be set with yaml
|
23
|
+
def test_configure_with
|
24
|
+
Moodle.configure_with('test/fixtures/config.yml')
|
25
|
+
|
26
|
+
assert_equal 'test_username', Moodle.config[:username]
|
27
|
+
assert_equal 'test_password', Moodle.config[:password]
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moodle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Boloc
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Interact with Moodle from ruby
|
14
|
+
email: robertboloc@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- .gitignore
|
20
|
+
- Gemfile
|
21
|
+
- LICENSE.md
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/moodle.rb
|
25
|
+
- lib/moodle/client.rb
|
26
|
+
- lib/moodle/protocols/rest.rb
|
27
|
+
- lib/moodle/services/user.rb
|
28
|
+
- moodle.gemspec
|
29
|
+
- test/fixtures/config.yml
|
30
|
+
- test/test_client.rb
|
31
|
+
- test/test_helper.rb
|
32
|
+
- test/test_moodle.rb
|
33
|
+
homepage: http://robertboloc.github.com/moodle
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata: {}
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 2.0.3
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: Moodle web services from ruby
|
57
|
+
test_files: []
|