Class: IMS::LTI::ToolConsumer

Inherits:
ToolBase
  • Object
show all
Defined in:
lib/ims/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

Attributes inherited from ToolBase

#consumer_key, #consumer_secret

Attributes included from RequestValidator

#oauth_signature_validator

Attributes included from LaunchParams

#custom_params, #ext_params, #non_spec_params

Instance Method Summary collapse

Methods inherited from ToolBase

#post_service_request

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

Parameters:

  • consumer_key (String)

    The OAuth consumer key

  • consumer_secret (String)

    The OAuth consumer secret

  • params (Hash) (defaults to: {})

    Set the launch parameters as described in LaunchParams



11
12
13
14
# File 'lib/ims/lti/tool_consumer.rb', line 11

def initialize(consumer_key, consumer_secret, params={})
  super(consumer_key, consumer_secret, params)
  @launch_url = params['launch_url']
end

Instance Attribute Details

#launch_urlObject

Returns the value of attribute launch_url.



4
5
6
# File 'lib/ims/lti/tool_consumer.rb', line 4

def launch_url
  @launch_url
end

#nonceObject

Returns the value of attribute nonce.



4
5
6
# File 'lib/ims/lti/tool_consumer.rb', line 4

def nonce
  @nonce
end

#timestampObject

Returns the value of attribute timestamp.



4
5
6
# File 'lib/ims/lti/tool_consumer.rb', line 4

def timestamp
  @timestamp
end

Instance Method Details

#generate_launch_dataObject

Generate the launch data including the necessary OAuth information



39
40
41
42
43
44
45
46
47
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
# File 'lib/ims/lti/tool_consumer.rb', line 39

def generate_launch_data
  raise IMS::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
  options = {
          :scheme => 'body',
          :timestamp => @timestamp,
          :nonce => @nonce
  }
  request = consumer.create_signed_request(:post, path, nil, options, 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

Returns:

  • (Boolean)


32
33
34
# File 'lib/ims/lti/tool_consumer.rb', line 32

def has_required_params?
  @consumer_key && @consumer_secret && @resource_link_id && @launch_url
end

#process_post_request(post_request) ⇒ Object



16
17
18
19
# File 'lib/ims/lti/tool_consumer.rb', line 16

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

Parameters:



24
25
26
27
28
29
# File 'lib/ims/lti/tool_consumer.rb', line 24

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