Skip to content

Commit 4f09883

Browse files
committed
add gem structure and first API endpoint
1 parent a2984b8 commit 4f09883

File tree

9 files changed

+192
-3
lines changed

9 files changed

+192
-3
lines changed

.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ build/
2727

2828
# for a library or gem, you might want to ignore these files since the code is
2929
# intended to run in multiple environments; otherwise, check them in:
30-
# Gemfile.lock
31-
# .ruby-version
32-
# .ruby-gemset
30+
Gemfile.lock
31+
.ruby-version
32+
.ruby-gemset
3333

3434
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
3535
.rvmrc

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source 'https://rubygems.org'
2+
gemspec

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'bundler/gem_tasks'

lib/moodle.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'httparty'
2+
3+
class Moodle
4+
include HTTParty
5+
6+
def initialize(token, url)
7+
@token = token
8+
self.class.base_uri url
9+
end
10+
11+
def site_info
12+
response = self.class.get(
13+
'/moodle/webservice/rest/server.php',
14+
{
15+
:query => {
16+
:wsfunction => 'core_webservice_get_site_info',
17+
:moodlewsrestformat => 'json',
18+
:wstoken => @token
19+
}
20+
}
21+
)
22+
response.parsed_response
23+
end
24+
end

moodle.gemspec

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
lib = File.expand_path('../lib', __FILE__)
2+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3+
4+
Gem::Specification.new do |spec|
5+
spec.name = 'moodle'
6+
spec.version = '0.0.1'
7+
spec.authors = ['Sam Giffney']
8+
spec.email = ['[email protected]']
9+
spec.summary = %q{ Interaction with Moodle API }
10+
spec.homepage = ''
11+
spec.license = ''
12+
13+
spec.files = `git ls-files -z`.split("\x0")
14+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16+
spec.require_paths = ['lib']
17+
18+
spec.add_development_dependency 'bundler', '~> 1.3'
19+
spec.add_development_dependency 'ruby-debug', '0.10.4'
20+
spec.add_development_dependency 'rake'
21+
spec.add_development_dependency 'rspec', '~> 3.0'
22+
spec.add_development_dependency 'webmock', '~> 1.0'
23+
spec.add_development_dependency 'vcr', '~> 2.9'
24+
25+
spec.add_dependency 'httparty', '~> 0.11.0'
26+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
recorded_with: VCR 2.9.3
3+
http_interactions:
4+
- request:
5+
method: get
6+
uri: http://elearning.bcanational.com/moodle/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=core_webservice_get_site_info&wstoken=invalid_moodle_token
7+
body:
8+
string: ""
9+
headers: {}
10+
11+
response:
12+
status:
13+
code: 200
14+
message: OK
15+
headers:
16+
X-Powered-By:
17+
- PHP/5.4.28
18+
Expires:
19+
- Thu, 01 Jan 1970 00:00:00 GMT
20+
Server:
21+
- Apache
22+
Transfer-Encoding:
23+
- chunked
24+
Access-Control-Allow-Origin:
25+
- "*"
26+
X-Frame-Options:
27+
- SAMEORIGIN
28+
Content-Type:
29+
- application/json
30+
Pragma:
31+
- no-cache
32+
Accept-Ranges:
33+
- none
34+
Cache-Control:
35+
- private, must-revalidate, pre-check=0, post-check=0, max-age=0
36+
Date:
37+
- Tue, 31 Mar 2015 23:33:59 GMT
38+
body:
39+
string: "{\"exception\":\"moodle_exception\",\"errorcode\":\"invalidtoken\",\"message\":\"Invalid token - token not found\"}"
40+
http_version:
41+
recorded_at: Tue, 31 Mar 2015 23:32:21 GMT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
recorded_with: VCR 2.9.3
3+
http_interactions:
4+
- request:
5+
method: get
6+
uri: http://elearning.bcanational.com/moodle/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=core_webservice_get_site_info&wstoken=2ca731f3b767e24029d5e4e363093590
7+
body:
8+
string: ""
9+
headers: {}
10+
11+
response:
12+
status:
13+
code: 200
14+
message: OK
15+
headers:
16+
X-Powered-By:
17+
- PHP/5.4.28
18+
Expires:
19+
- Thu, 01 Jan 1970 00:00:00 GMT
20+
Server:
21+
- Apache
22+
Transfer-Encoding:
23+
- chunked
24+
Access-Control-Allow-Origin:
25+
- "*"
26+
X-Frame-Options:
27+
- SAMEORIGIN
28+
Content-Type:
29+
- application/json
30+
Pragma:
31+
- no-cache
32+
Accept-Ranges:
33+
- none
34+
Cache-Control:
35+
- private, must-revalidate, pre-check=0, post-check=0, max-age=0
36+
Date:
37+
- Tue, 31 Mar 2015 23:33:59 GMT
38+
body:
39+
string: "{\"sitename\":\"BCA NATIONAL\",\"username\":\"jamesh\",\"firstname\":\"James\",\"lastname\":\"Hourn\",\"fullname\":\"James Hourn\",\"lang\":\"en\",\"userid\":8,\"siteurl\":\"http:\\/\\/elearning.bcanational.com\\/moodle\",\"userpictureurl\":\"http:\\/\\/elearning.bcanational.com\\/moodle\\/pluginfile.php\\/527\\/user\\/icon\\/f1\",\"functions\":[{\"name\":\"core_calendar_create_calendar_events\",\"version\":\"2014111003.02\"},{\"name\":\"core_calendar_delete_calendar_events\",\"version\":\"2014111003.02\"},{\"name\":\"core_calendar_get_calendar_events\",\"version\":\"2014111003.02\"},{\"name\":\"core_cohort_add_cohort_members\",\"version\":\"2014111003.02\"},{\"name\":\"core_cohort_create_cohorts\",\"version\":\"2014111003.02\"},{\"name\":\"core_cohort_delete_cohorts\",\"version\":\"2014111003.02\"},{\"name\":\"core_cohort_delete_cohort_members\",\"version\":\"2014111003.02\"},{\"name\":\"core_cohort_get_cohorts\",\"version\":\"2014111003.02\"},{\"name\":\"core_cohort_get_cohort_members\",\"version\":\"2014111003.02\"},{\"name\":\"core_cohort_update_cohorts\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_create_categories\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_create_courses\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_delete_categories\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_delete_courses\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_delete_modules\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_duplicate_course\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_get_categories\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_get_contents\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_get_courses\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_import_course\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_update_categories\",\"version\":\"2014111003.02\"},{\"name\":\"core_course_update_courses\",\"version\":\"2014111003.02\"},{\"name\":\"core_enrol_get_course_enrolment_methods\",\"version\":\"2014111003.02\"},{\"name\":\"core_enrol_get_enrolled_users\",\"version\":\"2014111003.02\"},{\"name\":\"core_enrol_get_enrolled_users_with_capability\",\"version\":\"2014111003.02\"},{\"name\":\"core_enrol_get_users_courses\",\"version\":\"2014111003.02\"},{\"name\":\"core_files_get_files\",\"version\":\"2014111003.02\"},{\"name\":\"core_files_upload\",\"version\":\"2014111003.02\"},{\"name\":\"core_get_component_strings\",\"version\":\"2014111003.02\"},{\"name\":\"core_get_string\",\"version\":\"2014111003.02\"},{\"name\":\"core_get_strings\",\"version\":\"2014111003.02\"},{\"name\":\"core_grades_get_grades\",\"version\":\"2014111003.02\"},{\"name\":\"core_grades_update_grades\",\"version\":\"2014111003.02\"},{\"name\":\"core_grade_get_definitions\",\"version\":\"2014111003.02\"},{\"name\":\"core_grading_get_definitions\",\"version\":\"2014111003.02\"},{\"name\":\"core_grading_get_gradingform_instances\",\"version\":\"2014111003.02\"},{\"name\":\"core_grading_save_definitions\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_add_group_members\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_assign_grouping\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_create_groupings\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_create_groups\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_delete_groupings\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_delete_groups\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_delete_group_members\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_get_course_groupings\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_get_course_groups\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_get_groupings\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_get_groups\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_get_group_members\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_unassign_grouping\",\"version\":\"2014111003.02\"},{\"name\":\"core_group_update_groupings\",\"version\":\"2014111003.02\"},{\"name\":\"core_message_block_contacts\",\"version\":\"2014111003.02\"},{\"name\":\"core_message_create_contacts\",\"version\":\"2014111003.02\"},{\"name\":\"core_message_delete_contacts\",\"version\":\"2014111003.02\"},{\"name\":\"core_message_get_contacts\",\"version\":\"2014111003.02\"},{\"name\":\"core_message_get_messages\",\"version\":\"2014111003.02\"},{\"name\":\"core_message_search_contacts\",\"version\":\"2014111003.02\"},{\"name\":\"core_message_send_instant_messages\",\"version\":\"2014111003.02\"},{\"name\":\"core_message_unblock_contacts\",\"version\":\"2014111003.02\"},{\"name\":\"core_notes_create_notes\",\"version\":\"2014111003.02\"},{\"name\":\"core_notes_delete_notes\",\"version\":\"2014111003.02\"},{\"name\":\"core_notes_get_notes\",\"version\":\"2014111003.02\"},{\"name\":\"core_notes_update_notes\",\"version\":\"2014111003.02\"},{\"name\":\"core_role_assign_roles\",\"version\":\"2014111003.02\"},{\"name\":\"core_role_unassign_roles\",\"version\":\"2014111003.02\"},{\"name\":\"core_user_add_user_device\",\"version\":\"2014111003.02\"},{\"name\":\"core_user_create_users\",\"version\":\"2014111003.02\"},{\"name\":\"core_user_delete_users\",\"version\":\"2014111003.02\"},{\"name\":\"core_user_get_course_user_profiles\",\"version\":\"2014111003.02\"},{\"name\":\"core_user_get_users\",\"version\":\"2014111003.02\"},{\"name\":\"core_user_get_users_by_field\",\"version\":\"2014111003.02\"},{\"name\":\"core_user_get_users_by_id\",\"version\":\"2014111003.02\"},{\"name\":\"core_user_update_users\",\"version\":\"2014111003.02\"},{\"name\":\"core_webservice_get_site_info\",\"version\":\"2014111003.02\"},{\"name\":\"enrol_manual_enrol_users\",\"version\":\"2014111000\"},{\"name\":\"enrol_self_get_instance_info\",\"version\":\"2014111000\"},{\"name\":\"message_airnotifier_are_notification_preferences_configured\",\"version\":\"2014111000\"},{\"name\":\"message_airnotifier_is_system_configured\",\"version\":\"2014111000\"},{\"name\":\"mod_assign_get_assignments\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_get_grades\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_get_submissions\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_get_user_flags\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_get_user_mappings\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_lock_submissions\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_reveal_identities\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_revert_submissions_to_draft\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_save_grade\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_save_grades\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_save_submission\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_save_user_extensions\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_set_user_flags\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_submit_for_grading\",\"version\":\"2014111001\"},{\"name\":\"mod_assign_unlock_submissions\",\"version\":\"2014111001\"},{\"name\":\"mod_forum_get_forums_by_courses\",\"version\":\"2014111000\"},{\"name\":\"mod_forum_get_forum_discussions\",\"version\":\"2014111000\"},{\"name\":\"mod_forum_get_forum_discussions_paginated\",\"version\":\"2014111000\"},{\"name\":\"mod_forum_get_forum_discussion_posts\",\"version\":\"2014111000\"},{\"name\":\"moodle_course_create_courses\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_course_get_courses\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_enrol_get_enrolled_users\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_enrol_get_users_courses\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_enrol_manual_enrol_users\",\"version\":\"2014111000\"},{\"name\":\"moodle_file_get_files\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_file_upload\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_group_add_groupmembers\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_group_create_groups\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_group_delete_groupmembers\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_group_delete_groups\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_group_get_course_groups\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_group_get_groupmembers\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_group_get_groups\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_message_send_instantmessages\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_notes_create_notes\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_role_assign\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_role_unassign\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_user_create_users\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_user_delete_users\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_user_get_course_participants_by_id\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_user_get_users_by_courseid\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_user_get_users_by_id\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_user_update_users\",\"version\":\"2014111003.02\"},{\"name\":\"moodle_webservice_get_siteinfo\",\"version\":\"2014111003.02\"}],\"downloadfiles\":1,\"uploadfiles\":1,\"release\":\"2.8.3+ (Build: 20150212)\",\"version\":\"2014111003.02\",\"mobilecssurl\":\"\"}"
40+
http_version:
41+
recorded_at: Tue, 31 Mar 2015 23:32:21 GMT

spec/lib/moodle_spec.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'spec_helper'
2+
3+
describe Moodle do
4+
let(:url) { ENV['MOODLE_URL'] }
5+
let(:token) { ENV['MOODLE_TOKEN'] }
6+
let(:moodle) { Moodle.new(token, url) }
7+
8+
describe '#site_info', :vcr => {
9+
:match_requests_on => [:body, :headers], :record => :once
10+
} do
11+
let(:result) { moodle.site_info }
12+
13+
context 'when invalid token' do
14+
let(:token) { 'invalid_moodle_token' }
15+
16+
specify do
17+
expect(result).to be_a Hash
18+
expect(result['errorcode']).to eq 'invalidtoken'
19+
end
20+
end
21+
22+
context 'when valid token' do
23+
specify do
24+
expect(result).to be_a Hash
25+
expect(result['release']).to eq '2.8.3+ (Build: 20150212)'
26+
end
27+
end
28+
end
29+
end

spec/spec_helper.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'bundler/setup'
2+
Bundler.require(:default, :development)
3+
4+
RSpec.configure do |config|
5+
config.run_all_when_everything_filtered = true
6+
config.filter_run :focus
7+
config.filter_run_excluding :perf => true
8+
config.order = 'random'
9+
end
10+
11+
require 'webmock/rspec'
12+
RSpec.configure do |config|
13+
config.before(:each) do
14+
WebMock.disable_net_connect!
15+
end
16+
end
17+
18+
require 'vcr'
19+
VCR.configure do |c|
20+
c.cassette_library_dir = 'spec/cassettes'
21+
c.hook_into :webmock
22+
c.configure_rspec_metadata!
23+
end
24+
25+
require File.expand_path('../../lib/moodle.rb', __FILE__)

0 commit comments

Comments
 (0)