Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
Include CourseraConnector with corresponding OAuth changes
Browse files Browse the repository at this point in the history
Add api_support_state to MoocProviders
Remove first draft from api_connection
move user_settings.coffee
Update Gemfile
#128
  • Loading branch information
MrSerth committed May 6, 2015
1 parent 6826c13 commit d38495d
Show file tree
Hide file tree
Showing 32 changed files with 407 additions and 239 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ gem 'rails_config'

gem 'newrelic_rpm'

gem 'oauth2'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
Expand Down
18 changes: 15 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ GEM
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
faraday (0.9.1)
multipart-post (>= 1.2, < 3)
ffi (1.9.8)
globalid (0.3.5)
activesupport (>= 4.1.0)
Expand All @@ -131,18 +133,27 @@ GEM
jquery-ui-rails (5.0.3)
railties (>= 3.2.16)
json (1.8.2)
loofah (2.0.1)
jwt (1.4.1)
loofah (2.0.2)
nokogiri (>= 1.5.9)
mail (2.6.3)
mime-types (>= 1.16, < 3)
mime-types (2.5)
mini_portile (0.6.2)
minitest (5.6.1)
multi_json (1.11.0)
multi_xml (0.5.5)
multipart-post (2.0.0)
netrc (0.10.3)
newrelic_rpm (3.11.2.286)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
oauth2 (1.0.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (~> 1.2)
orm_adapter (0.5.0)
parser (2.2.2.2)
ast (>= 1.1, < 3.0)
Expand All @@ -155,7 +166,7 @@ GEM
cliver (~> 0.3.1)
multi_json (~> 1.0)
websocket-driver (>= 0.2.0)
powerpack (0.1.0)
powerpack (0.1.1)
quiet_assets (1.1.0)
railties (>= 3.1, < 5.0)
rack (1.6.0)
Expand Down Expand Up @@ -219,7 +230,7 @@ GEM
rspec-mocks (~> 3.2.0)
rspec-support (~> 3.2.0)
rspec-support (3.2.2)
rubocop (0.30.1)
rubocop (0.31.0)
astrolabe (~> 1.3)
parser (>= 2.2.2.1, < 3.0)
powerpack (~> 0.1)
Expand Down Expand Up @@ -323,6 +334,7 @@ DEPENDENCIES
jquery-rails
jquery-ui-rails
newrelic_rpm
oauth2
passenger
pg
poltergeist
Expand Down
89 changes: 3 additions & 86 deletions app/assets/javascripts/statistics.coffee
Original file line number Diff line number Diff line change
@@ -1,86 +1,3 @@

ready = ->
$('#load-account-settings-button').on 'click', (event) -> loadAccountSettings(event)
$('#load-mooc-provider-settings-button').on 'click', (event) -> loadMoocProviderSettings(event)
$('#sync-user-mooc_provider-connection-button').on 'click', (event) -> synchronizeUserMoocProviderConnection(event)
return

$(document).ready(ready)

loadAccountSettings = (event) ->
button = $(event.target)
user_id = button.data('user_id')
url = "/users/#{user_id}/account_settings.json"
$.ajax
url: url
method: 'GET'
error: (jqXHR, textStatus, errorThrown) ->
console.log('error_synchronize')
alert(I18n.t('global.ajax_failed'))
success: (data, textStatus, jqXHR) ->
$("div.settings-container").html(data.partial)
event.preventDefault()

loadMoocProviderSettings = (event) ->
button = $(event.target)
user_id = button.data('user_id')
url = "/users/#{user_id}/mooc_provider_settings.json"
$.ajax
url: url
method: 'GET'
error: (jqXHR, textStatus, errorThrown) ->
console.log('error_synchronize')
alert(I18n.t('global.ajax_failed'))
success: (data, textStatus, jqXHR) ->
$("div.settings-container").html(data.partial)
event.preventDefault()

synchronizeUserMoocProviderConnection = (event) ->
button = $(event.target)
user_id = button.data('user_id')
mooc_provider = button.data('mooc_provider')
email = $("#input-email-#{mooc_provider}").val()
password = $("#input-password-#{mooc_provider}").val()
url = "/users/#{user_id}/set_mooc_provider_connection.json"
$.ajax
url: url
method: 'GET'
data:{email:email, password: password, mooc_provider:mooc_provider}
error: (jqXHR, textStatus, errorThrown) ->
console.log('error_synchronize')
alert(I18n.t('global.ajax_failed'))
success: (data, textStatus, jqXHR) ->
if data.status == true
$("#panel-#{mooc_provider}").removeClass("panel-default")
$("#panel-#{mooc_provider}").removeClass("panel-danger")
$("#panel-#{mooc_provider}").addClass("panel-success")
$("#div-error-#{mooc_provider}").text("")
else
$("#div-error-#{mooc_provider}").text("Error!")
event.preventDefault()

revokeUserMoocProviderConnection = (event) ->
button = $(event.target)
user_id = button.data('user_id')
mooc_provider = button.data('mooc_provider')
url = "/users/#{user_id}/revoke_mooc_provider_connection.json"
$.ajax
url: url
method: 'GET'
data:{mooc_provider:mooc_provider}
error: (jqXHR, textStatus, errorThrown) ->
console.log('error_synchronize')
alert(I18n.t('global.ajax_failed'))
success: (data, textStatus, jqXHR) ->
if data.status == true
$("#panel-#{mooc_provider}").removeClass("panel-success")
$("#panel-#{mooc_provider}").removeClass("panel-danger")
$("#panel-#{mooc_provider}").addClass("panel-default")
$("#div-error-#{mooc_provider}").text("")
else
$("#div-error-#{mooc_provider}").text("Error!")
event.preventDefault()

@bindClickEvents = () ->
$('button[id="sync-user-mooc_provider-connection-button"]').on 'click', (event) -> synchronizeUserMoocProviderConnection(event)
$('button[id="revoke-user-mooc_provider-connection-button"]').on 'click', (event) -> revokeUserMoocProviderConnection(event)
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
85 changes: 85 additions & 0 deletions app/assets/javascripts/user_settings.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

ready = ->
$('#load-account-settings-button').on 'click', (event) -> loadAccountSettings(event)
$('#load-mooc-provider-settings-button').on 'click', (event) -> loadMoocProviderSettings(event)
return

$(document).ready(ready)

loadAccountSettings = (event) ->
button = $(event.target)
user_id = button.data('user_id')
url = "/users/#{user_id}/account_settings.json"
$.ajax
url: url
method: 'GET'
error: (jqXHR, textStatus, errorThrown) ->
console.log('error_synchronize')
alert(I18n.t('global.ajax_failed'))
success: (data, textStatus, jqXHR) ->
$("div.settings-container").html(data.partial)
event.preventDefault()

loadMoocProviderSettings = (event) ->
button = $(event.target)
user_id = button.data('user_id')
url = "/users/#{user_id}/mooc_provider_settings.json"
$.ajax
url: url
method: 'GET'
error: (jqXHR, textStatus, errorThrown) ->
console.log('error_synchronize')
alert(I18n.t('global.ajax_failed'))
success: (data, textStatus, jqXHR) ->
$("div.settings-container").html(data.partial)
event.preventDefault()

synchronizeNaiveUserMoocProviderConnection = (event) ->
button = $(event.target)
user_id = button.data('user_id')
mooc_provider = button.data('mooc_provider')
email = $("#input-email-#{mooc_provider}").val()
password = $("#input-password-#{mooc_provider}").val()
url = "/users/#{user_id}/set_mooc_provider_connection.json"
$.ajax
url: url
method: 'GET'
data:{email:email, password: password, mooc_provider:mooc_provider}
error: (jqXHR, textStatus, errorThrown) ->
console.log('error_synchronize')
alert(I18n.t('global.ajax_failed'))
success: (data, textStatus, jqXHR) ->
if data.status == true
$("#panel-#{mooc_provider}").removeClass("panel-default")
$("#panel-#{mooc_provider}").removeClass("panel-danger")
$("#panel-#{mooc_provider}").addClass("panel-success")
$("#div-error-#{mooc_provider}").text("")
else
$("#div-error-#{mooc_provider}").text("Error!")
event.preventDefault()

revokeNaiveUserMoocProviderConnection = (event) ->
button = $(event.target)
user_id = button.data('user_id')
mooc_provider = button.data('mooc_provider')
url = "/users/#{user_id}/revoke_mooc_provider_connection.json"
$.ajax
url: url
method: 'GET'
data:{mooc_provider:mooc_provider}
error: (jqXHR, textStatus, errorThrown) ->
console.log('error_synchronize')
alert(I18n.t('global.ajax_failed'))
success: (data, textStatus, jqXHR) ->
if data.status == true
$("#panel-#{mooc_provider}").removeClass("panel-success")
$("#panel-#{mooc_provider}").removeClass("panel-danger")
$("#panel-#{mooc_provider}").addClass("panel-default")
$("#div-error-#{mooc_provider}").text("")
else
$("#div-error-#{mooc_provider}").text("Error!")
event.preventDefault()

@bindClickEvents = () ->
$('button[id="sync-naive-user-mooc_provider-connection-button"]').on 'click', (event) -> synchronizeNaiveUserMoocProviderConnection(event)
$('button[id="revoke-naive-user-mooc_provider-connection-button"]').on 'click', (event) -> revokeNaiveUserMoocProviderConnection(event)
6 changes: 0 additions & 6 deletions app/controllers/api_connection_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ def send_request
redirect_to api_connection_index_path
end

def send_user_request
OpenSAPConnector.new.initialize_connection(current_user, email: params[:email], password: params[:password])
OpenHPIConnector.new.initialize_connection(current_user, email: params[:email], password: params[:password])
redirect_to api_connection_index_path
end

def update_user
UserWorker.perform_async [current_user.id]
redirect_to api_connection_index_path
Expand Down
36 changes: 34 additions & 2 deletions app/controllers/concerns/abstract_mooc_provider_connector.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- encoding : utf-8 -*-
require 'rest_client'
require 'oauth2'

class AbstractMoocProviderConnector
def initialize_connection(user, credentials)
Expand Down Expand Up @@ -60,6 +61,10 @@ def connection_to_mooc_provider?(user)
user.mooc_providers.where(id: mooc_provider).present?
end

def oauth_link
raise NotImplementedError
end

private

def mooc_provider
Expand All @@ -86,9 +91,36 @@ def fetch_user_data(user)
handle_enrollments_response response_data, user
end

def get_authentication_token(user)
def get_access_token(user)
connection = MoocProviderUser.find_by(user_id: user, mooc_provider_id: mooc_provider)
connection.present? ? connection.authentication_token : nil
return unless connection.present?
if connection.mooc_provider.api_support_state == 'naive'
connection.access_token
elsif connection.mooc_provider.api_support_state == 'oauth'
if connection.access_token_valid_until > Time.zone.now
connection.access_token
else
# if connection.refresh_token.present?
# # TODO: refresh_access_token(user)
# else
# # TODO: get new access_token with user redirection
# end
nil
end
else
nil
end
end

def mooc_provider_user_connection(user)
if connection_to_mooc_provider? user
connection = MoocProviderUser.find_by(user_id: user, mooc_provider_id: mooc_provider)
else
connection = MoocProviderUser.new
connection.user_id = user.id
connection.mooc_provider_id = mooc_provider.id
end
connection
end

def get_enrollments_for_user(_user)
Expand Down
14 changes: 6 additions & 8 deletions app/controllers/concerns/abstract_xikolo_connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,26 @@ def send_connection_request(user, credentials)
response = RestClient.post(authentication_url, request_parameters, accept: 'application/vnd.xikoloapplication/vnd.xikolo.v1, application/json', authorization: 'token=\"78783786789\"')
json_response = JSON.parse response
return unless json_response['token'].present?
connection = MoocProviderUser.new
connection.authentication_token = json_response['token']
connection.user_id = user.id
connection.mooc_provider_id = mooc_provider.id
connection.save
connection = mooc_provider_user_connection user
connection.access_token = json_response['token']
connection.save!
end

def send_enrollment_for_course(user, course)
token_string = "Token token=#{get_authentication_token user}"
token_string = "Token token=#{get_access_token user}"
api_url = self.class::ROOT_API + ENROLLMENTS_API
request_parameters = "course_id=#{course.provider_course_id}"
RestClient.post(api_url, request_parameters, accept: 'application/vnd.xikoloapplication/vnd.xikolo.v1, application/json', authorization: token_string)
end

def send_unenrollment_for_course(user, course)
token_string = "Token token=#{get_authentication_token user}"
token_string = "Token token=#{get_access_token user}"
api_url = self.class::ROOT_API + ENROLLMENTS_API + course.provider_course_id
RestClient.delete(api_url, accept: 'application/vnd.xikoloapplication/vnd.xikolo.v1, application/json', authorization: token_string)
end

def get_enrollments_for_user(user)
token_string = "Token token=#{get_authentication_token user}"
token_string = "Token token=#{get_access_token user}"
api_url = self.class::ROOT_API + ENROLLMENTS_API
response = RestClient.get(api_url, accept: 'application/vnd.xikoloapplication/vnd.xikolo.v1, application/json', authorization: token_string)
JSON.parse response
Expand Down
Loading

0 comments on commit d38495d

Please sign in to comment.