<commit_before>module Lti2Tp
  class Registration < ActiveRecord::Base

    END_REGISTRATION_ID_NAME = 'X-IMS-EndRegistration-ID'

    def create_tool_proxy tool_consumer_profile, tool_proxy_guid, disposition
      tool_provider_registry = Rails.application.config.tool_provider_registry
      tool_proxy = {}
      tool_proxy['@context'] = [
        'http://purl.imsglobal.org/ctx/lti/v2/ToolProxy'
      ]
      tool_proxy['@type'] = 'ToolProxy'
      tool_proxy['@id'] = "ToolProxyProposal_at_#{Time.now.utc.iso8601}"

      tool_proxy['lti_version'] = 'LTI-2p0'
      tool_proxy['tool_proxy_guid'] = tool_proxy_guid
      # Fails conformance since it's undefined and checking is too strict
      tool_proxy['disposition'] = disposition if disposition == 'reregister'
      tool_proxy['tool_consumer_profile'] = self.tc_profile_url
      tool_proxy['tool_profile'] = JSON.load( tool_profile_json )
      tool_proxy['security_contract'] = resolve_security_contract( tool_consumer_profile )

      tool_proxy_wrapper = JsonWrapper.new( tool_proxy )
      tool_proxy_wrapper.root
    end

    def get_tool_consumer_profile()
      tcp_response = invoke_unsigned_service(self.tc_profile_url, 'get', {}, {}, nil, Rails.application.config.wire_log, "Get Tool Consumer Profile")
      JSON.load( tcp_response.body )
    end

    def prepare_tool_proxy disposition, tool_proxy_guid
      tool_consumer_profile = JSON.load( self.tool_consumer_profile_json )
      tool_proxy = create_tool_proxy(tool_consumer_profile, tool_proxy_guid, disposition)
      if tool_proxy
        self.end_registration_id = UUID.generate
        if disposition == 'register'
          self.tool_proxy_json = tool_proxy.to_json
        else
          self.proposed_tool_proxy_json = tool_proxy.to_json
        end
        self.save!

        service_offered = nil
        tool_consumer_profile['service_offered'].select do |entry|
          if match_services( entry['@id'] , ':ToolProxy.collection' )
            service_offered = entry
            break
          end
        end
        if service_offered.nil?
          status = create_status(false, nil, "No matching service definition")
          return_url = self.launch_presentation_return_url + '?status=failure'
          (redirect_to return_url) and return
        end

        (tool_proxy_response, err_code, err_msg) = register_tool_proxy service_offered, "post", disposition
        unless err_code == 201
          status = create_status(false, nil, "#{err_code}-#{err_msg}")
          return status
        end
        if disposition == 'register'
          # get guid from the response returned by the TC
          tool_proxy['tool_proxy_guid'] = tool_proxy_response['tool_proxy_guid']


          # substitute tool_proxy_guid now in the Proxy where needed
          tool_proxy_wrapper = JsonWrapper.new( tool_proxy )<commit_msg>allow blank disposition<commit_after>module Lti2Tp
  class Registration < ActiveRecord::Base

    END_REGISTRATION_ID_NAME = 'X-IMS-EndRegistration-ID'

    def create_tool_proxy tool_consumer_profile, tool_proxy_guid, disposition
      tool_provider_registry = Rails.application.config.tool_provider_registry
      tool_proxy = {}
      tool_proxy['@context'] = [
        'http://purl.imsglobal.org/ctx/lti/v2/ToolProxy'
      ]
      tool_proxy['@type'] = 'ToolProxy'
      tool_proxy['@id'] = "ToolProxyProposal_at_#{Time.now.utc.iso8601}"

      tool_proxy['lti_version'] = 'LTI-2p0'
      tool_proxy['tool_proxy_guid'] = tool_proxy_guid
      # Fails conformance since it's undefined and checking is too strict
      tool_proxy['disposition'] = disposition if disposition == 'reregister'
      tool_proxy['tool_consumer_profile'] = self.tc_profile_url
      tool_proxy['tool_profile'] = JSON.load( tool_profile_json )
      tool_proxy['security_contract'] = resolve_security_contract( tool_consumer_profile )

      tool_proxy_wrapper = JsonWrapper.new( tool_proxy )
      tool_proxy_wrapper.root
    end

    def get_tool_consumer_profile()
      tcp_response = invoke_unsigned_service(self.tc_profile_url, 'get', {}, {}, nil, Rails.application.config.wire_log, "Get Tool Consumer Profile")
      JSON.load( tcp_response.body )
    end

    def prepare_tool_proxy disposition, tool_proxy_guid
      tool_consumer_profile = JSON.load( self.tool_consumer_profile_json )
      tool_proxy = create_tool_proxy(tool_consumer_profile, tool_proxy_guid, disposition)
      if tool_proxy
        self.end_registration_id = UUID.generate
        if disposition == 'register'
          self.tool_proxy_json = tool_proxy.to_json
        else
          self.proposed_tool_proxy_json = tool_proxy.to_json
        end
        self.save!

        service_offered = nil
        tool_consumer_profile['service_offered'].select do |entry|
          if match_services( entry['@id'] , ':ToolProxy.collection' )
            service_offered = entry
            break
          end
        end
        if service_offered.nil?
          status = create_status(false, nil, "No matching service definition")
          return_url = self.launch_presentation_return_url + '?status=failure'
          (redirect_to return_url) and return
        end

        (tool_proxy_response, err_code, err_msg) = register_tool_proxy service_offered, "post", disposition
        unless err_code == 201
          status = create_status(false, nil, "#{err_code}-#{err_msg}")
          return status
        end
        if disposition.blank? || disposition == 'register' 
          # get guid from the response returned by the TC
          tool_proxy['tool_proxy_guid'] = tool_proxy_response['tool_proxy_guid']


          # substitute tool_proxy_guid now in the Proxy where needed
          tool_proxy_wrapper = JsonWrapper.new( tool_proxy )
{'fraction_non_alphanumeric': 0.08670019074041962, 'fraction_numerical': 0.0038148083925784635, 'mean_word_length': 3.182741116751269, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7475694', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1663, 'n_words': 333}