<commit_before>require 'umakadata/http_helper'
require 'umakadata/logging/log'

module Umakadata
  module Criteria
    module ContentNegotiation

      include Umakadata::HTTPHelper

      def check_content_negotiation(uri, prefix, content_type, logger: nil)
        query = <<-"SPARQL"
SELECT
  ?s
WHERE {
        GRAPH ?g {
           ?s ?p ?o
           FILTER regex(?s, "^#{prefix}")
        } .
      }
LIMIT 1
SPARQL
        results = nil
        [:post, :get].each do |method|
          request_log = Umakadata::Logging::Log.new
          logger.push request_log unless logger.nil?
          results = Umakadata::SparqlHelper.query(uri, query, logger: request_log, options: {method: method})
          if results.nil?
            request_log.result = "failed to retrieve subject starts with #{prefix}"
          else
            request_log.result = "Success to pick up a subject which starts with #{prefix}"
            break
          end
        end
        return false if results.nil?

        uri = results.first[:s]

        http_log = Umakadata::Logging::Log.new
        logger.push http_log unless logger.nil?
        args = {:headers => {'Accept' => content_type}}
        request = URI(uri)
        response = http_get_recursive(request, args, logger: http_log)
        if !response.is_a?(Net::HTTPSuccess)
          http_log.result = '#{uri} does not return 200 HTTP response'
          logger.result = "#{uri} does not support #{content_type} in the content negotiation" unless logger.nil?
          return false
        else
          http_log.result = 'The endpoint returns 200 HTTP response.'
        end

        result = response.content_type == content_type
        logger.result = "#{uri} #{result ? 'supports' : 'does not support'} #{content_type} in the content negotiation" unless logger.nil?

        return result
      end
    end
  end
end
<commit_msg>Fix the problem that check HTTP subject fails if the index was out of range.<commit_after>require 'umakadata/http_helper'
require 'umakadata/logging/log'

module Umakadata
  module Criteria
    module ContentNegotiation

      include Umakadata::HTTPHelper

      def check_content_negotiation(uri, prefix, content_type, logger: nil)
        query = <<-"SPARQL"
SELECT
  ?s
WHERE {
        GRAPH ?g {
           ?s ?p ?o
           FILTER regex(?s, "^#{prefix}")
        } .
      }
LIMIT 1
SPARQL
        results = nil
        [:post, :get].each do |method|
          request_log = Umakadata::Logging::Log.new
          logger.push request_log unless logger.nil?
          results = Umakadata::SparqlHelper.query(uri, query, logger: request_log, options: {method: method})
          if results.nil?
            request_log.result = "failed to retrieve subject starts with #{prefix}"
          else
            request_log.result = "Success to pick up a subject which starts with #{prefix}"
            break
          end
        end
        return false if results.nil? or results.first.nil?

        uri = results.first[:s]

        http_log = Umakadata::Logging::Log.new
        logger.push http_log unless logger.nil?
        args = {:headers => {'Accept' => content_type}}
        request = URI(uri)
        response = http_get_recursive(request, args, logger: http_log)
        if !response.is_a?(Net::HTTPSuccess)
          http_log.result = '#{uri} does not return 200 HTTP response'
          logger.result = "#{uri} does not support #{content_type} in the content negotiation" unless logger.nil?
          return false
        else
          http_log.result = 'The endpoint returns 200 HTTP response.'
        end

        result = response.content_type == content_type
        logger.result = "#{uri} #{result ? 'supports' : 'does not support'} #{content_type} in the content negotiation" unless logger.nil?

        return result
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.09696655431682655, 'fraction_numerical': 0.003629764065335753, 'mean_word_length': 2.3489583333333335, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5013527', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1079, 'n_words': 332}