moodle-api 1.3 → 1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +22 -0
- data/.rubocop.yml +9 -0
- data/.rubocop_todo.yml +16 -0
- data/Guardfile +3 -3
- data/README.md +10 -14
- data/Rakefile +3 -3
- data/bin/console +4 -4
- data/fixtures/vcr_cassettes/external_service/invalid_service.yml +2 -2
- data/fixtures/vcr_cassettes/external_service/valid_service.yml +2 -2
- data/fixtures/vcr_cassettes/external_service/valid_service_empty_array_response.yml +2 -2
- data/fixtures/vcr_cassettes/external_service/valid_service_not_external.yml +2 -2
- data/fixtures/vcr_cassettes/token_service/token_service.yml +1 -1
- data/lib/moodle/api.rb +7 -4
- data/lib/moodle/api/client.rb +9 -7
- data/lib/moodle/api/configuration.rb +9 -7
- data/lib/moodle/api/request.rb +10 -8
- data/lib/moodle/api/token_generator.rb +9 -3
- data/lib/moodle/api/version.rb +4 -1
- data/moodle-api.gemspec +15 -13
- metadata +34 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a71818e7b85063b4316df6fa56edb7d7ef693b71
|
4
|
+
data.tar.gz: cbca9d7f5335f6a9c767cec223ebd79b3366ab7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1109adec53160b999567f6184e9f2a6c8c896922e5aa90bf299585aa4cb349cd1027d805199e145c721031a0faa5296780c91d3e5ca87776e97bb78c9dc0ad78
|
7
|
+
data.tar.gz: 9d9663bb2e17dfcc5ea94373dee096f794ff2c8914d09e7ad30630ad876c144ebe6b27a4945359bbda8831679925e49792d52b0db3db1d67e209dd2f05fadadb
|
data/.codeclimate.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Under the engines key, you can configure which engines will analyze your repo.
|
2
|
+
# Each key is an engine name. For each value, you need to specify enabled: true
|
3
|
+
# to enable the engine as well as any other engines-specific configuration.
|
4
|
+
engines:
|
5
|
+
# to turn on an engine, add it here and set enabled to `true`
|
6
|
+
# to turn off an engine, set enabled to `false` or remove it
|
7
|
+
rubocop:
|
8
|
+
enabled: true
|
9
|
+
# Engines can analyze files and report issues on them, but you can separately
|
10
|
+
# decide which files will receive ratings based on those issues. This is
|
11
|
+
# specified by path patterns under the ratings key.
|
12
|
+
ratings:
|
13
|
+
paths:
|
14
|
+
- app/**
|
15
|
+
- lib/**
|
16
|
+
- "**.rb"
|
17
|
+
# - "**.go"
|
18
|
+
# You can globally exclude files from being analyzed by any engine using the
|
19
|
+
# exclude_paths key.
|
20
|
+
exclude_paths:
|
21
|
+
- spec/**
|
22
|
+
- vendor/**
|
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
+
# on 2015-07-16 19:09:22 +0200 using RuboCop version 0.32.1.
|
3
|
+
# The point is for the user to remove these configuration records
|
4
|
+
# one by one as the offenses are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
7
|
+
|
8
|
+
# Offense count: 9
|
9
|
+
# Configuration parameters: AllowURI, URISchemes.
|
10
|
+
Metrics/LineLength:
|
11
|
+
Max: 104
|
12
|
+
|
13
|
+
# Offense count: 1
|
14
|
+
# Cop supports --auto-correct.
|
15
|
+
Style/BlockComments:
|
16
|
+
Enabled: false
|
data/Guardfile
CHANGED
@@ -24,8 +24,8 @@
|
|
24
24
|
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
25
|
# * 'just' rspec: 'rspec'
|
26
26
|
|
27
|
-
guard :rspec, cmd:
|
28
|
-
require
|
27
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
28
|
+
require 'guard/rspec/dsl'
|
29
29
|
dsl = Guard::RSpec::Dsl.new(self)
|
30
30
|
|
31
31
|
# Feel free to open issues for suggestions and improvements
|
@@ -65,6 +65,6 @@ guard :rspec, cmd: "bundle exec rspec" do
|
|
65
65
|
# Turnip features and steps
|
66
66
|
watch(%r{^spec/acceptance/(.+)\.feature$})
|
67
67
|
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
68
|
-
Dir[File.join("**/#{m[1]}.feature")][0] ||
|
68
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance'
|
69
69
|
end
|
70
70
|
end
|
data/README.md
CHANGED
@@ -42,12 +42,10 @@ Moodle::Api.configuration.host = 'http://my_moodle_instance.com'
|
|
42
42
|
Moodle::Api.configuration.token = 'mytoken'
|
43
43
|
|
44
44
|
# Pass options hash to configure
|
45
|
-
Moodle::Api.configure({host: 'http://my_moodle_instance.com',
|
46
|
-
token: 'mytoken'})
|
45
|
+
Moodle::Api.configure({host: 'http://my_moodle_instance.com', token: 'mytoken'})
|
47
46
|
|
48
47
|
# The client can also be instantiated and used.
|
49
|
-
client = Moodle:Client.new({host: 'http://my_moodle_instance.com',
|
50
|
-
token: 'mytoken'})
|
48
|
+
client = Moodle:Client.new({host: 'http://my_moodle_instance.com', token: 'mytoken'})
|
51
49
|
|
52
50
|
client.make_request(:function_name_here, my_params)
|
53
51
|
```
|
@@ -67,8 +65,7 @@ New functions created in Moodle will automatically be available in the gem.
|
|
67
65
|
### Example
|
68
66
|
|
69
67
|
```ruby
|
70
|
-
Moodle::Api.configure({host: 'http://my_moodle_instance.com',
|
71
|
-
token: 'mytoken'})
|
68
|
+
Moodle::Api.configure({host: 'http://my_moodle_instance.com', token: 'mytoken'})
|
72
69
|
|
73
70
|
params = { 'criteria[0][key]' => 'firstname', 'criteria[0][value]' => 'Jon' }
|
74
71
|
|
@@ -79,10 +76,10 @@ Moodle::Api.core_user_get_users(params)
|
|
79
76
|
Moodle uses token authentication, but sometimes you might not have a token. Users are able to generate tokens automatically when calling services using basic authentication.
|
80
77
|
```ruby
|
81
78
|
Moodle::Api.configure({host: 'http://my_moodle_instance.com',
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
79
|
+
service: 'my_external_service', # ensure you include the shortname of the external service
|
80
|
+
username: 'jonsnow',
|
81
|
+
password: 'defendthewall'})
|
82
|
+
|
86
83
|
params = { 'criteria[0][key]' => 'firstname', 'criteria[0][value]' => 'Jon' }
|
87
84
|
|
88
85
|
Moodle::Api.core_user_get_users(params)
|
@@ -114,11 +111,12 @@ Note: regenerating vcr cassettes, some data will change which will break the tes
|
|
114
111
|
|
115
112
|
## Contributing
|
116
113
|
|
117
|
-
1. Fork it ( https://github.com/[my-github-username]/moodle/fork )
|
114
|
+
1. Fork it ( https://github.com/[my-github-username]/moodle-api/fork )
|
118
115
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
119
116
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
120
117
|
4. Push to the branch (`git push origin my-new-feature`)
|
121
118
|
5. Create a new Pull Request
|
119
|
+
6. Ensure your stuff is rubocop'd
|
122
120
|
|
123
121
|
I am always keen to learn so please feel free to create an issue with code reviews, suggestions and possible refactorings.
|
124
122
|
|
@@ -126,7 +124,5 @@ I am always keen to learn so please feel free to create an issue with code revie
|
|
126
124
|
|
127
125
|
- Add additional protocols
|
128
126
|
- Make cassettes easily rerunnable - will require a parser for the response to remove dynamic data or a funky regex in the specs.
|
127
|
+
- Moodle web services require you to know how the Moodle API expects params. They are pretty messy. It would be nice to leverage hashes and arrays and then parse them to into what moodle expects.
|
129
128
|
|
130
|
-
## Warning
|
131
|
-
|
132
|
-
This gem is still under heavy development.
|
data/Rakefile
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
2
|
require 'rspec/core/rake_task'
|
3
3
|
|
4
4
|
RSpec::Core::RakeTask.new(:spec)
|
5
5
|
|
6
|
-
task :
|
6
|
+
task default: :spec
|
7
7
|
|
8
8
|
task :console do
|
9
|
-
exec
|
9
|
+
exec 'pry -r moodle -I ./lib'
|
10
10
|
end
|
data/bin/console
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'moodle'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
9
|
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require
|
10
|
+
# require 'pry'
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start
|
@@ -2,7 +2,7 @@
|
|
2
2
|
http_interactions:
|
3
3
|
- request:
|
4
4
|
method: post
|
5
|
-
uri: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users_invalid&wstoken=
|
5
|
+
uri: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users_invalid&wstoken=mytoken
|
6
6
|
body:
|
7
7
|
encoding: US-ASCII
|
8
8
|
string: ''
|
@@ -43,6 +43,6 @@ http_interactions:
|
|
43
43
|
* FROM {external_functions} WHERE name = ?\n[array (\n 0 => ''core_user_get_users_invalid'',\n)]"}'
|
44
44
|
http_version: '1.1'
|
45
45
|
adapter_metadata:
|
46
|
-
effective_url: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users_invalid&wstoken=
|
46
|
+
effective_url: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users_invalid&wstoken=mytoken
|
47
47
|
recorded_at: Wed, 01 Jul 2015 12:33:57 GMT
|
48
48
|
recorded_with: VCR 2.9.3
|
@@ -2,7 +2,7 @@
|
|
2
2
|
http_interactions:
|
3
3
|
- request:
|
4
4
|
method: post
|
5
|
-
uri: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users&wstoken=
|
5
|
+
uri: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users&wstoken=mytoken
|
6
6
|
body:
|
7
7
|
encoding: US-ASCII
|
8
8
|
string: ''
|
@@ -56,6 +56,6 @@ http_interactions:
|
|
56
56
|
names checked","shortname":"certificatenameschecked"}],"preferences":[{"name":"auth_manual_passwordupdatetime","value":"1434626773"},{"name":"block7hidden","value":"0"},{"name":"block9hidden","value":"0"},{"name":"docked_block_instance_7","value":"0"},{"name":"docked_block_instance_9","value":"0"},{"name":"email_bounce_count","value":"0"},{"name":"email_send_count","value":"41"},{"name":"filepicker_recentlicense","value":"allrightsreserved"},{"name":"filepicker_recentrepository","value":"7"},{"name":"htmleditor","value":""},{"name":"login_failed_count_since_success","value":"7"},{"name":"message_beepnewmessage","value":"0"},{"name":"message_blocknoncontacts","value":"0"},{"name":"message_provider_enrol_manual_expiry_notification_loggedin","value":"email"},{"name":"message_provider_enrol_manual_expiry_notification_loggedoff","value":"email"},{"name":"message_provider_enrol_self_expiry_notification_loggedin","value":"email"},{"name":"message_provider_enrol_self_expiry_notification_loggedoff","value":"email"},{"name":"message_provider_mod_assign_assign_notification_loggedin","value":"email"},{"name":"message_provider_mod_assign_assign_notification_loggedoff","value":"email"},{"name":"message_provider_mod_forum_posts_loggedin","value":"email"},{"name":"message_provider_mod_forum_posts_loggedoff","value":"email"},{"name":"message_provider_mod_lesson_graded_essay_loggedin","value":"email"},{"name":"message_provider_mod_lesson_graded_essay_loggedoff","value":"email"},{"name":"message_provider_mod_quiz_attempt_overdue_loggedin","value":"email"},{"name":"message_provider_mod_quiz_attempt_overdue_loggedoff","value":"email"},{"name":"message_provider_mod_quiz_confirmation_loggedin","value":"email"},{"name":"message_provider_mod_quiz_confirmation_loggedoff","value":"email"},{"name":"message_provider_mod_quiz_submission_loggedin","value":"email"},{"name":"message_provider_mod_quiz_submission_loggedoff","value":"email"},{"name":"message_provider_moodle_availableupdate_loggedin","value":"email"},{"name":"message_provider_moodle_availableupdate_loggedoff","value":"email"},{"name":"message_provider_moodle_backup_loggedin","value":"email"},{"name":"message_provider_moodle_backup_loggedoff","value":"email"},{"name":"message_provider_moodle_badgecreatornotice_loggedin","value":"none"},{"name":"message_provider_moodle_badgecreatornotice_loggedoff","value":"email"},{"name":"message_provider_moodle_badgerecipientnotice_loggedin","value":"popup"},{"name":"message_provider_moodle_badgerecipientnotice_loggedoff","value":"popup,email"},{"name":"message_provider_moodle_courserequestapproved_loggedin","value":"email"},{"name":"message_provider_moodle_courserequestapproved_loggedoff","value":"email"},{"name":"message_provider_moodle_courserequested_loggedin","value":"email"},{"name":"message_provider_moodle_courserequested_loggedoff","value":"email"},{"name":"message_provider_moodle_courserequestrejected_loggedin","value":"email"},{"name":"message_provider_moodle_courserequestrejected_loggedoff","value":"email"},{"name":"message_provider_moodle_errors_loggedin","value":"email"},{"name":"message_provider_moodle_errors_loggedoff","value":"email"},{"name":"message_provider_moodle_instantmessage_loggedin","value":"popup"},{"name":"message_provider_moodle_instantmessage_loggedoff","value":"popup,email"},{"name":"message_provider_moodle_notices_loggedin","value":"email"},{"name":"message_provider_moodle_notices_loggedoff","value":"email"},{"name":"userselector_autoselectunique","value":"0"},{"name":"userselector_optionscollapsed","value":"0"},{"name":"userselector_preserveselected","value":"1"},{"name":"userselector_searchanywhere","value":"0"},{"name":"_lastloaded","value":1435753952}]}],"warnings":[]}'
|
57
57
|
http_version: '1.1'
|
58
58
|
adapter_metadata:
|
59
|
-
effective_url: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users&wstoken=
|
59
|
+
effective_url: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users&wstoken=mytoken
|
60
60
|
recorded_at: Wed, 01 Jul 2015 12:32:32 GMT
|
61
61
|
recorded_with: VCR 2.9.3
|
@@ -2,7 +2,7 @@
|
|
2
2
|
http_interactions:
|
3
3
|
- request:
|
4
4
|
method: post
|
5
|
-
uri: http://my_moodle_instance.com/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=local_getsmarter_get_quiz_grades&wstoken=
|
5
|
+
uri: http://my_moodle_instance.com/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=local_getsmarter_get_quiz_grades&wstoken=mytoken&courseid=92
|
6
6
|
body:
|
7
7
|
encoding: US-ASCII
|
8
8
|
string: ''
|
@@ -41,6 +41,6 @@ http_interactions:
|
|
41
41
|
string: "[]"
|
42
42
|
http_version: '1.1'
|
43
43
|
adapter_metadata:
|
44
|
-
effective_url: http://my_moodle_instance.com/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=local_getsmarter_get_quiz_grades&wstoken=
|
44
|
+
effective_url: http://my_moodle_instance.com/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=local_getsmarter_get_quiz_grades&wstoken=mytoken&courseid=92
|
45
45
|
recorded_at: Thu, 02 Jul 2015 07:10:48 GMT
|
46
46
|
recorded_with: VCR 2.9.3
|
@@ -2,7 +2,7 @@
|
|
2
2
|
http_interactions:
|
3
3
|
- request:
|
4
4
|
method: post
|
5
|
-
uri: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users&wstoken=
|
5
|
+
uri: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users&wstoken=mytoken
|
6
6
|
body:
|
7
7
|
encoding: US-ASCII
|
8
8
|
string: ''
|
@@ -54,6 +54,6 @@ http_interactions:
|
|
54
54
|
Plugins > Web services > External services and Manage tokens."}'
|
55
55
|
http_version: '1.1'
|
56
56
|
adapter_metadata:
|
57
|
-
effective_url: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users&wstoken=
|
57
|
+
effective_url: http://my_moodle_instance.com/webservice/rest/server.php?criteria%5B0%5D%5Bkey%5D=firstname&criteria%5B0%5D%5Bvalue%5D=Jon&moodlewsrestformat=json&wsfunction=core_user_get_users&wstoken=mytoken
|
58
58
|
recorded_at: Wed, 01 Jul 2015 12:44:52 GMT
|
59
59
|
recorded_with: VCR 2.9.3
|
@@ -39,7 +39,7 @@ http_interactions:
|
|
39
39
|
- application/json; charset=utf-8
|
40
40
|
body:
|
41
41
|
encoding: UTF-8
|
42
|
-
string: '{"token":"
|
42
|
+
string: '{"token":"mytoken"}'
|
43
43
|
http_version: '1.1'
|
44
44
|
adapter_metadata:
|
45
45
|
effective_url: http://my_moodle_instance.com/login/token.php?username=ryan.mes%40email.co.za&password=mypassword&service=phoenix_integration
|
data/lib/moodle/api.rb
CHANGED
@@ -12,18 +12,21 @@ rescue
|
|
12
12
|
end
|
13
13
|
|
14
14
|
module Moodle
|
15
|
+
# Handles instantiating the client and determining how
|
16
|
+
# method calls should be delegated to the client
|
15
17
|
module Api
|
16
18
|
class << self
|
17
|
-
def method_missing
|
19
|
+
def method_missing(method, *args, &block)
|
18
20
|
if client.respond_to?(method)
|
19
21
|
client.send(method, *args, &block)
|
20
22
|
else
|
21
|
-
|
23
|
+
# assume method name is moodle external service
|
24
|
+
client.make_request(method, args.first)
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
25
|
-
def respond_to?(method, include_all=false)
|
26
|
-
|
28
|
+
def respond_to?(method, include_all = false)
|
29
|
+
client.respond_to?(method, include_all) || super
|
27
30
|
end
|
28
31
|
|
29
32
|
def client
|
data/lib/moodle/api/client.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
module Moodle
|
2
2
|
module Api
|
3
|
+
# The client is responsible for making requests
|
4
|
+
# and delegating config setup to configuration
|
3
5
|
class Client
|
4
6
|
attr_reader :web_service_name, :filter_params
|
5
|
-
attr_writer :token_service
|
7
|
+
attr_writer :token_service, :configuration
|
6
8
|
|
7
|
-
def initialize
|
9
|
+
def initialize(options = {})
|
8
10
|
configure(options)
|
9
11
|
end
|
10
12
|
|
11
|
-
def make_request
|
13
|
+
def make_request(web_service_name, filter_params = {})
|
12
14
|
@web_service_name = web_service_name
|
13
15
|
@filter_params = filter_params
|
14
16
|
|
@@ -17,14 +19,14 @@ module Moodle
|
|
17
19
|
|
18
20
|
def request_params
|
19
21
|
{
|
20
|
-
params: filter_params.merge!(
|
21
|
-
|
22
|
-
|
22
|
+
params: filter_params.merge!(moodlewsrestformat: configuration.format,
|
23
|
+
wsfunction: web_service_name,
|
24
|
+
wstoken: configuration.token),
|
23
25
|
headers: { 'Accept' => 'json' }
|
24
26
|
}
|
25
27
|
end
|
26
28
|
|
27
|
-
def configure
|
29
|
+
def configure(options = {}, &block)
|
28
30
|
configuration.configure(options, &block)
|
29
31
|
end
|
30
32
|
|
@@ -1,13 +1,15 @@
|
|
1
1
|
module Moodle
|
2
2
|
module Api
|
3
|
+
# Handles all configuration settings required
|
4
|
+
# to interaction with a Moodle API
|
3
5
|
class Configuration
|
4
6
|
attr_writer :token_service
|
5
7
|
attr_accessor :host,
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
:username,
|
9
|
+
:password,
|
10
|
+
:service,
|
11
|
+
:token,
|
12
|
+
:format
|
11
13
|
|
12
14
|
DEFAULT_FORMAT = :json
|
13
15
|
|
@@ -15,7 +17,7 @@ module Moodle
|
|
15
17
|
|
16
18
|
DEFAULT_TOKEN_API_URL = '/login/token.php'
|
17
19
|
|
18
|
-
def initialize
|
20
|
+
def initialize(options = {})
|
19
21
|
@format = DEFAULT_FORMAT
|
20
22
|
|
21
23
|
configure(options)
|
@@ -38,7 +40,7 @@ module Moodle
|
|
38
40
|
"#{host}#{DEFAULT_TOKEN_API_URL}"
|
39
41
|
end
|
40
42
|
|
41
|
-
def configure
|
43
|
+
def configure(options = {}, &block)
|
42
44
|
options.each { |key, value| instance_variable_set("@#{key}", value) }
|
43
45
|
block.call(self) if block_given?
|
44
46
|
end
|
data/lib/moodle/api/request.rb
CHANGED
@@ -3,10 +3,14 @@ require 'json'
|
|
3
3
|
|
4
4
|
module Moodle
|
5
5
|
module Api
|
6
|
+
# Handles making the request to the Moodle API and interpreting the
|
7
|
+
# results. The Moodle API is not consistent in the way it returns
|
8
|
+
# responses so the type of response is determined, successful responses
|
9
|
+
# are returned, failures are parsed and raised in a generic fashion.
|
6
10
|
class Request
|
7
11
|
attr_reader :response
|
8
12
|
|
9
|
-
def post
|
13
|
+
def post(path, options = {})
|
10
14
|
@response = Typhoeus.post(path, options)
|
11
15
|
resolve_response if response.success?
|
12
16
|
end
|
@@ -19,9 +23,9 @@ module Moodle
|
|
19
23
|
|
20
24
|
def request_raised_exception?
|
21
25
|
if external_services_api_exception?
|
22
|
-
|
26
|
+
fail MoodleError, response_body['message']
|
23
27
|
elsif token_service_api_exception?
|
24
|
-
|
28
|
+
fail MoodleError, response_body['error']
|
25
29
|
end
|
26
30
|
end
|
27
31
|
|
@@ -39,11 +43,9 @@ module Moodle
|
|
39
43
|
|
40
44
|
# API calls that return null are considered successful
|
41
45
|
def response_body
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
response.body
|
46
|
-
end
|
46
|
+
JSON.parse(response.body)
|
47
|
+
rescue JSON::ParserError
|
48
|
+
response.body
|
47
49
|
end
|
48
50
|
end
|
49
51
|
end
|
@@ -1,14 +1,20 @@
|
|
1
1
|
module Moodle
|
2
2
|
module Api
|
3
|
+
# Generates tokens using username, password and service
|
4
|
+
# Used for accessing Moodle API
|
3
5
|
class TokenGenerator
|
4
6
|
attr_reader :configuration
|
5
7
|
|
6
|
-
def initialize
|
8
|
+
def initialize(configuration)
|
7
9
|
@configuration = configuration
|
8
10
|
end
|
9
11
|
|
10
12
|
def call
|
11
|
-
|
13
|
+
if raise_token_exception?
|
14
|
+
fail ArgumentError,
|
15
|
+
'Username and password are required to generate a token'
|
16
|
+
end
|
17
|
+
|
12
18
|
generate_token
|
13
19
|
end
|
14
20
|
|
@@ -17,7 +23,7 @@ module Moodle
|
|
17
23
|
def generate_token
|
18
24
|
response = Request.new.post(configuration.token_api_url,
|
19
25
|
params: request_params,
|
20
|
-
headers: { 'Accept' =>
|
26
|
+
headers: { 'Accept' => 'json' })
|
21
27
|
response['token']
|
22
28
|
end
|
23
29
|
|
data/lib/moodle/api/version.rb
CHANGED
data/moodle-api.gemspec
CHANGED
@@ -4,36 +4,38 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'moodle/api/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'moodle-api'
|
8
8
|
spec.version = Moodle::Api::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['Ryan-Neal Mes']
|
10
|
+
spec.email = ['ryan.mes@gmail.com']
|
11
11
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
12
|
+
spec.summary = 'Moodle web service API wrapper.'
|
13
|
+
spec.description = 'Wraps Moodle API and exposes web services that have been made external.'
|
14
|
+
spec.homepage = 'https://github.com/get-smarter/moodle-api'
|
15
|
+
spec.license = 'MIT'
|
16
16
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
18
|
# delete this section to allow pushing this gem to any host.
|
19
19
|
if spec.respond_to?(:metadata)
|
20
|
-
spec.metadata['allowed_push_host'] =
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
21
|
else
|
22
|
-
|
22
|
+
fail 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
23
23
|
end
|
24
24
|
|
25
25
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
-
spec.bindir =
|
26
|
+
spec.bindir = 'exe'
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
-
spec.require_paths = [
|
28
|
+
spec.require_paths = ['lib']
|
29
29
|
|
30
|
-
spec.add_development_dependency
|
31
|
-
spec.add_development_dependency
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.9'
|
31
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
32
32
|
spec.add_development_dependency 'rspec'
|
33
33
|
spec.add_development_dependency 'guard-rspec'
|
34
34
|
spec.add_development_dependency 'pry'
|
35
35
|
spec.add_development_dependency 'vcr'
|
36
36
|
spec.add_development_dependency 'codeclimate-test-reporter'
|
37
|
+
spec.add_development_dependency 'rubocop'
|
38
|
+
spec.add_development_dependency 'rubocop-rspec'
|
37
39
|
|
38
40
|
spec.add_dependency 'typhoeus'
|
39
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moodle-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.4'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan-Neal Mes
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,6 +108,34 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop-rspec
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
111
139
|
- !ruby/object:Gem::Dependency
|
112
140
|
name: typhoeus
|
113
141
|
requirement: !ruby/object:Gem::Requirement
|
@@ -129,8 +157,11 @@ executables: []
|
|
129
157
|
extensions: []
|
130
158
|
extra_rdoc_files: []
|
131
159
|
files:
|
160
|
+
- ".codeclimate.yml"
|
132
161
|
- ".gitignore"
|
133
162
|
- ".rspec"
|
163
|
+
- ".rubocop.yml"
|
164
|
+
- ".rubocop_todo.yml"
|
134
165
|
- ".ruby-version"
|
135
166
|
- ".travis.yml"
|
136
167
|
- Gemfile
|
@@ -184,7 +215,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
215
|
version: '0'
|
185
216
|
requirements: []
|
186
217
|
rubyforge_project:
|
187
|
-
rubygems_version: 2.4.
|
218
|
+
rubygems_version: 2.4.8
|
188
219
|
signing_key:
|
189
220
|
specification_version: 4
|
190
221
|
summary: Moodle web service API wrapper.
|