Class: Moodle::Client
- Inherits:
-
Object
- Object
- Moodle::Client
- Includes:
- Service::Cohort, Service::Course, Service::User, Service::Webservice
- Defined in:
- lib/moodle/client.rb
Instance Attribute Summary collapse
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
-
#client ⇒ Object
Retuns a Moodle::Protocol client instance.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#obtain_token ⇒ Object
Obtains a token from the username and password.
-
#request(params = {}) ⇒ Object
Make a request using the desired protocol and format.
Methods included from Service::Webservice
#core_webservice_get_site_info
Methods included from Service::Cohort
Methods included from Service::Course
Methods included from Service::User
#core_user_get_users, #core_user_get_users_by_field
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/moodle/client.rb', line 18 def initialize(={}) @username = [:username] || Moodle.config[:username] @password = [:password] || Moodle.config[:password] @domain = [:domain] || Moodle.config[:domain] @protocol = [:protocol] || Moodle.config[:protocol] @service = [:service] || Moodle.config[:service] @format = [:format] || Moodle.config[:format] @token = [:token] || Moodle.config[:token] # If no token is provided generate one if @token.nil? @token = self.obtain_token end end |
Instance Attribute Details
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
16 17 18 |
# File 'lib/moodle/client.rb', line 16 def domain @domain end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
16 17 18 |
# File 'lib/moodle/client.rb', line 16 def format @format end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
16 17 18 |
# File 'lib/moodle/client.rb', line 16 def password @password end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
16 17 18 |
# File 'lib/moodle/client.rb', line 16 def protocol @protocol end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
16 17 18 |
# File 'lib/moodle/client.rb', line 16 def service @service end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
16 17 18 |
# File 'lib/moodle/client.rb', line 16 def token @token end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
16 17 18 |
# File 'lib/moodle/client.rb', line 16 def username @username end |
Instance Method Details
#client ⇒ Object
Retuns a Moodle::Protocol client instance
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/moodle/client.rb', line 34 def client if @client.nil? # Instantiate the client protocol case @protocol when 'rest' @client = Moodle::Protocol::Rest.new else @client = Moodle::Protocol::Rest.new end end @client end |
#obtain_token ⇒ Object
Obtains a token from the username and password
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/moodle/client.rb', line 48 def obtain_token response = client.request(@domain + '/login/token.php', { :username => @username, :password => @password, :service => @service }) parsed = JSON.parse(response) parsed['token'] end |
#request(params = {}) ⇒ Object
Make a request using the desired protocol and format
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/moodle/client.rb', line 60 def request(params={}) params.merge!( :wstoken => @token, :moodlewsrestformat => @format, :wsfunction => caller[0][/`.*'/][1..-2] ) response = client.request(@domain + '/webservice/' + @protocol + '/server.php', params) JSON.parse(response) end |