<commit_before>module Heroics
  # A link invokes requests with an HTTP server.
  class Link
    # Instantiate a link.
    #
    # @param url [String] The URL to use when making requests.  Include the
    #   username and password to use with HTTP basic auth.
    # @param path [String] The path to use when making requests.  Substrings
    #   that match `{(#/name)}` are replaced with user provided values when
    #   the link is invoked.
    # @param method [Symbol] A symbol representing the HTTP method to use when
    #   invoking the link.
    def initialize(url, path, method)
      @url = url
      @path = path
      @method = method
    end

    # Make a request to the server.
    #
    # @param parameters [Array] The list of parameters to inject into the
    #   path.  A request body can be passed as the final parameter and will
    #   always be converted to JSON before being transmitted.
    # @raise [ArgumentError] Raised if either too many or too few parameters
    #   were provided.
    # @return [String,Object] A string for text responses or an object for
    #   JSON responses.
    def run(*parameters)
      path, body = format_path(parameters)
      connection = Excon.new(@url)
      if body
        headers = {'Content-Type' => 'application/json'}
        body = MultiJson.dump(body)
      end
      response = connection.request(method: @method, path: path,
                                    headers: headers, body: body,
                                    expects: [200, 201])
      content_type = response.headers['Content-Type']
      # FIXME Correctly handle unsuccessful HTTP status codes. -jkakar
      if content_type && content_type.include?('application/json')
        MultiJson.load(response.body)
      elsif !response.body.empty?
        response.body
      end
    end

    private

    # Inject parameters into the path and return the body, if it exists.
    #
    # @param parameters [Array] The list of parameters to inject into the
    #   path.
    # @raise [ArgumentError] Raised if either too many or too few parameters
    #   were provided.
    # @return [String,Object] A path and request body pair.  The body value is
    #   nil if a payload wasn't included in the list of parameters.
    def format_path(parameters)
      parameter_regex = /\{\([%\/a-zA-Z0-9]*\)\}/
      parameter_size = @path.scan(parameter_regex).size
      too_few_parameters = parameter_size > parameters.size
      # FIXME We should use the schema to detect when a request body is
      # permitted and do the calculation correctly here. -jkakar<commit_msg>- Remove unnecessary comment.<commit_after>module Heroics
  # A link invokes requests with an HTTP server.
  class Link
    # Instantiate a link.
    #
    # @param url [String] The URL to use when making requests.  Include the
    #   username and password to use with HTTP basic auth.
    # @param path [String] The path to use when making requests.  Substrings
    #   that match `{(#/name)}` are replaced with user provided values when
    #   the link is invoked.
    # @param method [Symbol] A symbol representing the HTTP method to use when
    #   invoking the link.
    def initialize(url, path, method)
      @url = url
      @path = path
      @method = method
    end

    # Make a request to the server.
    #
    # @param parameters [Array] The list of parameters to inject into the
    #   path.  A request body can be passed as the final parameter and will
    #   always be converted to JSON before being transmitted.
    # @raise [ArgumentError] Raised if either too many or too few parameters
    #   were provided.
    # @return [String,Object] A string for text responses or an object for
    #   JSON responses.
    def run(*parameters)
      path, body = format_path(parameters)
      connection = Excon.new(@url)
      if body
        headers = {'Content-Type' => 'application/json'}
        body = MultiJson.dump(body)
      end
      response = connection.request(method: @method, path: path,
                                    headers: headers, body: body,
                                    expects: [200, 201])
      content_type = response.headers['Content-Type']
      if content_type && content_type.include?('application/json')
        MultiJson.load(response.body)
      elsif !response.body.empty?
        response.body
      end
    end

    private

    # Inject parameters into the path and return the body, if it exists.
    #
    # @param parameters [Array] The list of parameters to inject into the
    #   path.
    # @raise [ArgumentError] Raised if either too many or too few parameters
    #   were provided.
    # @return [String,Object] A path and request body pair.  The body value is
    #   nil if a payload wasn't included in the list of parameters.
    def format_path(parameters)
      parameter_regex = /\{\([%\/a-zA-Z0-9]*\)\}/
      parameter_size = @path.scan(parameter_regex).size
      too_few_parameters = parameter_size > parameters.size
      # FIXME We should use the schema to detect when a request body is
      # permitted and do the calculation correctly here. -jkakar
{'fraction_non_alphanumeric': 0.07896276077208032, 'fraction_numerical': 0.003119516474946383, 'mean_word_length': 2.422281521014009, 'pattern_counts': {'":': 0, '<': 3, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2534415', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1357, 'n_words': 570}