moodle 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
- require 'rake/testtask'
2
-
3
- Rake::TestTask.new do |t|
4
- t.libs << 'test'
5
- end
6
-
7
- desc "Running tests"
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Running tests"
8
8
  task :default => :test
@@ -1,39 +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
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
@@ -1,69 +1,71 @@
1
- require 'moodle/protocols/rest'
2
- require 'moodle/services/user'
3
- require 'moodle/services/course'
4
- require 'moodle/services/webservice'
5
- require 'hashie'
6
- require 'json'
7
-
8
- module Moodle
9
- class Client
10
- include Moodle::Service::User
11
- include Moodle::Service::Course
12
- include Moodle::Service::Webservice
13
-
14
- attr_reader :username, :password, :domain, :protocol, :service, :format, :token
15
-
16
- def initialize(options={})
17
- @username = options[:username] || Moodle.config[:username]
18
- @password = options[:password] || Moodle.config[:password]
19
- @domain = options[:domain] || Moodle.config[:domain]
20
- @protocol = options[:protocol] || Moodle.config[:protocol]
21
- @service = options[:service] || Moodle.config[:service]
22
- @format = options[:format] || Moodle.config[:format]
23
- @token = options[:token] || Moodle.config[:token]
24
-
25
- # If no token is provided generate one
26
- if @token.nil?
27
- @token = self.obtain_token
28
- end
29
- end
30
-
31
- # Retuns a Moodle::Protocol client instance
32
- def client
33
- if @client.nil?
34
- # Instantiate the client protocol
35
- case @protocol
36
- when 'rest'
37
- @client = Moodle::Protocol::Rest.new
38
- else
39
- @client = Moodle::Protocol::Rest.new
40
- end
41
- end
42
- @client
43
- end
44
-
45
- # Obtains a token from the username and password
46
- def obtain_token
47
- response = client.request(@domain + '/login/token.php', {
48
- :username => @username,
49
- :password => @password,
50
- :service => @service
51
- })
52
-
53
- parsed = JSON.parse(response)
54
- parsed['token']
55
- end
56
-
57
- # Make a request using the desired protocol and format
58
- def request(params={})
59
- params.merge!(
60
- :wstoken => @token,
61
- :moodlewsrestformat => @format,
62
- :wsfunction => caller[0][/`.*'/][1..-2]
63
- )
64
- response = client.request(@domain + '/webservice/' + @protocol + '/server.php', params)
65
-
66
- JSON.parse(response)
67
- end
68
- end
1
+ require 'moodle/protocols/rest'
2
+ require 'moodle/services/user'
3
+ require 'moodle/services/course'
4
+ require 'moodle/services/cohort'
5
+ require 'moodle/services/webservice'
6
+ require 'hashie'
7
+ require 'json'
8
+
9
+ module Moodle
10
+ class Client
11
+ include Moodle::Service::User
12
+ include Moodle::Service::Course
13
+ include Moodle::Service::Cohort
14
+ include Moodle::Service::Webservice
15
+
16
+ attr_reader :username, :password, :domain, :protocol, :service, :format, :token
17
+
18
+ def initialize(options={})
19
+ @username = options[:username] || Moodle.config[:username]
20
+ @password = options[:password] || Moodle.config[:password]
21
+ @domain = options[:domain] || Moodle.config[:domain]
22
+ @protocol = options[:protocol] || Moodle.config[:protocol]
23
+ @service = options[:service] || Moodle.config[:service]
24
+ @format = options[:format] || Moodle.config[:format]
25
+ @token = options[:token] || Moodle.config[:token]
26
+
27
+ # If no token is provided generate one
28
+ if @token.nil?
29
+ @token = self.obtain_token
30
+ end
31
+ end
32
+
33
+ # Retuns a Moodle::Protocol client instance
34
+ def client
35
+ if @client.nil?
36
+ # Instantiate the client protocol
37
+ case @protocol
38
+ when 'rest'
39
+ @client = Moodle::Protocol::Rest.new
40
+ else
41
+ @client = Moodle::Protocol::Rest.new
42
+ end
43
+ end
44
+ @client
45
+ end
46
+
47
+ # Obtains a token from the username and password
48
+ def obtain_token
49
+ response = client.request(@domain + '/login/token.php', {
50
+ :username => @username,
51
+ :password => @password,
52
+ :service => @service
53
+ })
54
+
55
+ parsed = JSON.parse(response)
56
+ parsed['token']
57
+ end
58
+
59
+ # Make a request using the desired protocol and format
60
+ def request(params={})
61
+ params.merge!(
62
+ :wstoken => @token,
63
+ :moodlewsrestformat => @format,
64
+ :wsfunction => caller[0][/`.*'/][1..-2]
65
+ )
66
+ response = client.request(@domain + '/webservice/' + @protocol + '/server.php', params)
67
+
68
+ JSON.parse(response)
69
+ end
70
+ end
69
71
  end
@@ -1,11 +1,11 @@
1
- require 'hashie'
2
- require 'json'
3
-
4
- module Moodle
5
- class Helper
6
-
7
- def to_object(json)
8
-
9
- end
10
- end
11
- end
1
+ require 'hashie'
2
+ require 'json'
3
+
4
+ module Moodle
5
+ class Helper
6
+
7
+ def to_object(json)
8
+
9
+ end
10
+ end
11
+ end
@@ -1,13 +1,12 @@
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
1
+ require 'rest-client'
2
+
3
+ module Moodle
4
+ module Protocol
5
+ class Rest
6
+
7
+ def request(url, params={})
8
+ RestClient.get(url, :params => params)
9
+ end
10
+ end
11
+ end
13
12
  end
@@ -0,0 +1,22 @@
1
+ module Moodle
2
+ module Service
3
+ module Cohort
4
+ # Returns cohort details
5
+ def core_cohort_get_cohorts(ids)
6
+ params = {}
7
+
8
+ counter = 0
9
+ ids.each do |id|
10
+ params['cohortids[' + counter.to_s + ']'] = id
11
+ counter = counter + 1
12
+ end
13
+
14
+ response = request(params)
15
+
16
+ if response.any?
17
+ cohorts = response.map { |cohort| Hashie::Mash.new(cohort) }
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,22 +1,22 @@
1
- module Moodle
2
- module Service
3
- module Course
4
- # Return course details
5
- def core_course_get_courses(options)
6
- params = {}
7
-
8
- counter = 0
9
- options.each do |id|
10
- params['options[ids][' + counter.to_s + ']'] = id
11
- counter = counter + 1
12
- end
13
-
14
- response = request(params)
15
-
16
- if response.any?
17
- courses = response.map { |course| Hashie::Mash.new(course) }
18
- end
19
- end
20
- end
21
- end
1
+ module Moodle
2
+ module Service
3
+ module Course
4
+ # Return course details
5
+ def core_course_get_courses(options)
6
+ params = {}
7
+
8
+ counter = 0
9
+ options.each do |id|
10
+ params['options[ids][' + counter.to_s + ']'] = id
11
+ counter = counter + 1
12
+ end
13
+
14
+ response = request(params)
15
+
16
+ if response.any?
17
+ courses = response.map { |course| Hashie::Mash.new(course) }
18
+ end
19
+ end
20
+ end
21
+ end
22
22
  end
@@ -1,40 +1,40 @@
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
- response = request(params)
18
- Hashie::Mash.new *response
19
- end
20
-
21
- # Search for users matching the criteria
22
- def core_user_get_users(criteria)
23
- params = {}
24
-
25
- counter = 0
26
- criteria.each do |key,value|
27
- params['criteria[' + counter.to_s + '][key]'] = key.to_s
28
- params['criteria[' + counter.to_s + '][value]'] = value
29
- counter = counter + 1
30
- end
31
-
32
- response = request(params)
33
-
34
- if response['users']
35
- users = response['users'].map { |user| Hashie::Mash.new(user) }
36
- end
37
- end
38
- end
39
- end
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
+ response = request(params)
18
+ Hashie::Mash.new *response
19
+ end
20
+
21
+ # Search for users matching the criteria
22
+ def core_user_get_users(criteria)
23
+ params = {}
24
+
25
+ counter = 0
26
+ criteria.each do |key,value|
27
+ params['criteria[' + counter.to_s + '][key]'] = key.to_s
28
+ params['criteria[' + counter.to_s + '][value]'] = value
29
+ counter = counter + 1
30
+ end
31
+
32
+ response = request(params)
33
+
34
+ if response['users']
35
+ users = response['users'].map { |user| Hashie::Mash.new(user) }
36
+ end
37
+ end
38
+ end
39
+ end
40
40
  end
@@ -1,10 +1,10 @@
1
- module Moodle
2
- module Service
3
- module Webservice
4
- # Return some site info / user info / list web service functions
5
- def core_webservice_get_site_info
6
- Hashie::Mash.new request
7
- end
8
- end
9
- end
1
+ module Moodle
2
+ module Service
3
+ module Webservice
4
+ # Return some site info / user info / list web service functions
5
+ def core_webservice_get_site_info
6
+ Hashie::Mash.new request
7
+ end
8
+ end
9
+ end
10
10
  end