Class: Moodle::Client

Inherits:
Object
  • Object
show all
Includes:
Service::Cohort, Service::Course, Service::User, Service::Webservice
Defined in:
lib/moodle/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Service::Webservice

#core_webservice_get_site_info

Methods included from Service::Cohort

#core_cohort_get_cohorts

Methods included from Service::Course

#core_course_get_courses

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(options={})
  @username = options[:username] || Moodle.config[:username]
  @password = options[:password] || Moodle.config[:password]
  @domain   = options[:domain]   || Moodle.config[:domain]
  @protocol = options[:protocol] || Moodle.config[:protocol]
  @service  = options[:service]  || Moodle.config[:service]
  @format   = options[:format]   || Moodle.config[:format]
  @token    = options[:token]    || Moodle.config[:token]

  # If no token is provided generate one
  if @token.nil?
    @token = self.obtain_token
  end
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



16
17
18
# File 'lib/moodle/client.rb', line 16

def domain
  @domain
end

#formatObject (readonly)

Returns the value of attribute format.



16
17
18
# File 'lib/moodle/client.rb', line 16

def format
  @format
end

#passwordObject (readonly)

Returns the value of attribute password.



16
17
18
# File 'lib/moodle/client.rb', line 16

def password
  @password
end

#protocolObject (readonly)

Returns the value of attribute protocol.



16
17
18
# File 'lib/moodle/client.rb', line 16

def protocol
  @protocol
end

#serviceObject (readonly)

Returns the value of attribute service.



16
17
18
# File 'lib/moodle/client.rb', line 16

def service
  @service
end

#tokenObject (readonly)

Returns the value of attribute token.



16
17
18
# File 'lib/moodle/client.rb', line 16

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



16
17
18
# File 'lib/moodle/client.rb', line 16

def username
  @username
end

Instance Method Details

#clientObject

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_tokenObject

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