Class: AJIMS::LTI::ToolConsumer
- Inherits:
-
Object
- Object
- AJIMS::LTI::ToolConsumer
- Includes:
- Extensions::Base, LaunchParams, RequestValidator
- Defined in:
- lib/ajims/lti/tool_consumer.rb
Overview
Class for implementing an LTI Tool Consumer
Constant Summary
Constants included from LaunchParams
LaunchParams::LAUNCH_DATA_PARAMETERS
Instance Attribute Summary collapse
-
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
-
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
-
#launch_url ⇒ Object
Returns the value of attribute launch_url.
-
#nonce ⇒ Object
Returns the value of attribute nonce.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Attributes included from RequestValidator
Attributes included from LaunchParams
#custom_params, #ext_params, #non_spec_params
Instance Method Summary collapse
-
#generate_launch_data ⇒ Object
Generate the launch data including the necessary OAuth information.
-
#has_required_params? ⇒ Boolean
Check if the required parameters for a tool launch are set.
-
#initialize(consumer_key, consumer_secret, params = {}) ⇒ ToolConsumer
constructor
Create a new ToolConsumer.
- #process_post_request(post_request) ⇒ Object
-
#set_config(config) ⇒ Object
Set launch data from a ToolConfig.
Methods included from RequestValidator
#request_oauth_nonce, #request_oauth_timestamp, #valid_request!, #valid_request?
Methods included from LaunchParams
#get_custom_param, #get_ext_param, #get_non_spec_param, #process_params, #roles=, #set_custom_param, #set_ext_param, #set_non_spec_param, #to_params
Methods included from Extensions::Base
#extend_outcome_request, #extend_outcome_response, #outcome_request_extensions, #outcome_response_extensions
Constructor Details
#initialize(consumer_key, consumer_secret, params = {}) ⇒ ToolConsumer
Create a new ToolConsumer
15 16 17 18 19 20 21 22 23 |
# File 'lib/ajims/lti/tool_consumer.rb', line 15 def initialize(consumer_key, consumer_secret, params={}) @consumer_key = consumer_key @consumer_secret = consumer_secret @custom_params = {} @ext_params = {} @non_spec_params = {} @launch_url = params['launch_url'] process_params(params) end |
Instance Attribute Details
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
8 9 10 |
# File 'lib/ajims/lti/tool_consumer.rb', line 8 def consumer_key @consumer_key end |
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
8 9 10 |
# File 'lib/ajims/lti/tool_consumer.rb', line 8 def consumer_secret @consumer_secret end |
#launch_url ⇒ Object
Returns the value of attribute launch_url.
8 9 10 |
# File 'lib/ajims/lti/tool_consumer.rb', line 8 def launch_url @launch_url end |
#nonce ⇒ Object
Returns the value of attribute nonce.
8 9 10 |
# File 'lib/ajims/lti/tool_consumer.rb', line 8 def nonce @nonce end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
8 9 10 |
# File 'lib/ajims/lti/tool_consumer.rb', line 8 def @timestamp end |
Instance Method Details
#generate_launch_data ⇒ Object
Generate the launch data including the necessary OAuth information
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ajims/lti/tool_consumer.rb', line 48 def generate_launch_data raise AJIMS::LTI::InvalidLTIConfigError, "Not all required params set for tool launch" unless has_required_params? params = self.to_params params['lti_version'] ||= 'LTI-1p0' params['lti_message_type'] ||= 'basic-lti-launch-request' uri = URI.parse(@launch_url) if uri.port == uri.default_port host = uri.host else host = "#{uri.host}:#{uri.port}" end consumer = OAuth::Consumer.new(@consumer_key, @consumer_secret, { :site => "#{uri.scheme}://#{host}", :signature_method => "HMAC-SHA1" }) path = uri.path path = '/' if path.empty? if uri.query && uri.query != '' CGI.parse(uri.query).each do |query_key, query_values| unless params[query_key] params[query_key] = query_values.first end end end = { :scheme => 'body', :timestamp => @timestamp, :nonce => @nonce } request = consumer.create_signed_request(:post, path, nil, , params) # the request is made by a html form in the user's browser, so we # want to revert the escapage and return the hash of post parameters ready # for embedding in a html view hash = {} request.body.split(/&/).each do |param| key, val = param.split(/=/).map { |v| CGI.unescape(v) } hash[key] = val end hash end |
#has_required_params? ⇒ Boolean
Check if the required parameters for a tool launch are set
41 42 43 |
# File 'lib/ajims/lti/tool_consumer.rb', line 41 def has_required_params? @consumer_key && @consumer_secret && @resource_link_id && @launch_url end |
#process_post_request(post_request) ⇒ Object
25 26 27 28 |
# File 'lib/ajims/lti/tool_consumer.rb', line 25 def process_post_request(post_request) request = extend_outcome_request(OutcomeRequest.new) request.process_post_request(post_request) end |
#set_config(config) ⇒ Object
Set launch data from a ToolConfig
33 34 35 36 37 38 |
# File 'lib/ajims/lti/tool_consumer.rb', line 33 def set_config(config) @launch_url ||= config.secure_launch_url @launch_url ||= config.launch_url # any parameters already set will take priority @custom_params = config.custom_params.merge(@custom_params) end |