Class: Moodle::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Service::User

#core_user_get_users_by_field

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/moodle/client.rb', line 12

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.



10
11
12
# File 'lib/moodle/client.rb', line 10

def domain
  @domain
end

#formatObject (readonly)

Returns the value of attribute format.



10
11
12
# File 'lib/moodle/client.rb', line 10

def format
  @format
end

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/moodle/client.rb', line 10

def password
  @password
end

#protocolObject (readonly)

Returns the value of attribute protocol.



10
11
12
# File 'lib/moodle/client.rb', line 10

def protocol
  @protocol
end

#serviceObject (readonly)

Returns the value of attribute service.



10
11
12
# File 'lib/moodle/client.rb', line 10

def service
  @service
end

#tokenObject (readonly)

Returns the value of attribute token.



10
11
12
# File 'lib/moodle/client.rb', line 10

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



10
11
12
# File 'lib/moodle/client.rb', line 10

def username
  @username
end

Instance Method Details

#clientObject

Retuns a Moodle::Protocol client instance



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/moodle/client.rb', line 28

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



42
43
44
45
46
47
48
49
50
# File 'lib/moodle/client.rb', line 42

def obtain_token
  response = client.request(@domain + '/login/token.php', {
    :username => @username, 
    :password => @password, 
    :service  => @service
  })
  # @TODO: deal with error response
  response.token
end

#request(params = {}) ⇒ Object

Make a request using the desired protocol and format



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/moodle/client.rb', line 53

def request(params={})
  params.merge!(
    :wstoken => @token,
    :moodlewsrestformat => @format,
    :wsfunction => caller[0][/`.*'/][1..-2]
  )
  response = client.request(@domain + '/webservice/' + @protocol + '/server.php', params)
  array_response = JSON.parse response
  hash_response = Hash[*array_response]
  Hashie::Mash.new hash_response
end