Created
July 25, 2021 14:07
-
-
Save tachyons/690819798dd3fe06e5e4c5b88feb7fdb to your computer and use it in GitHub Desktop.
assignment service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LtiScoreSubmission | |
def initialize(assignment: , lis_result_sourced_id:, score: , lis_outcome_service_url: ) | |
@assignment = assignment | |
@lis_result_sourced_id = lis_result_sourced_id | |
@score = score | |
@lis_outcome_service_url = lis_outcome_service_url | |
@oauth_consumer_key = assignment.oauth_consumer_key | |
@lti_shared_secret = assignment.lti_shared_secret | |
end | |
def call | |
response = token.post(lis_outcome_service_url, score_body.to_xml, 'Content-Type' => 'application/xml') | |
if response.body.match(/\bsuccess\b/) | |
puts "score submitted" | |
return true | |
else | |
puts "score submission failed" | |
return false | |
end | |
end | |
private | |
attr_reader :lis_result_sourced_id, :assignment, :score, :lis_outcome_service_url | |
def oauth_token | |
consumer = OAuth::Consumer.new(assignment.oauth_consumer_key, assignment.lti_shared_secret) | |
OAuth::AccessToken.new(consumer) | |
end | |
def score_body | |
Nokogiri::XML::Builder.new do |xml| | |
xml.imsx_POXEnvelopeRequest(xmlns: "http://www.imsglobal.org/lis/oms1p0/pox") do | |
xml.imsx_POXHeader do | |
xml.imsx_POXRequestHeaderInfo do | |
xml.imsx_version "V1.0" | |
xml.imsx_messageIdentifier message_identifier | |
end | |
end | |
xml.imsx_POXBody do | |
xml.replaceResultRequest do | |
xml.resultRecord do | |
xml.sourcedGUID do | |
xml.sourcedId lis_result_sourced_id | |
end | |
xml.result do | |
xml.resultScore do | |
xml.language "en" | |
xml.textString score | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
def message_identifier | |
(SecureRandom.random_number(9e7) + 1e7).to_i # generating a 8 digit random number like 12341234 | |
end | |
end | |
# Spec | |
# | |
Rspec.describe LtiScoreSubmission do | |
it 'Sends request with proper xml structure' do | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment