<commit_before>class Aws4Signer
  class Signature
    def initialize(access_key_id, secret_access_key, region, service, verb, uri, headers, body)
      @access_key_id = access_key_id
      @secret_access_key = secret_access_key
      @region = region
      @service = service
      @verb = verb
      @uri = uri
      @headers = headers.dup
      @body = body

      @headers.each do |name, value|
        if value.kind_of?(Array)
          @headers[name] = value.first
        end
      end

      @headers["x-amz-date"] ||= @headers.delete("X-Amz-Date")
      unless @headers["x-amz-date"]
        @date = Time.now.utc
        @headers["x-amz-date"] = @date.strftime("%Y%m%dT%H%M%SZ")
      end
      @headers["Host"] ||= @headers.delete("host") || uri.host
    end

    attr_reader :region, :service, :verb, :uri, :headers, :body, :access_key_id, :secret_access_key

    def attach_to_http_request(req)
      headers.each do |name, value|
        req[name] = value
      end

      req["x-amz-content-sha256"] = Digest::SHA2.hexdigest(req.body || '', 256)

      req["Authorization"] = "AWS4-HMAC-SHA256 " \
        "Credential=#{@access_key_id}/#{scope}," \
        "SignedHeaders=#{signed_headers}," \
        "Signature=#{signature}"

      req
    end

    def date
      @date ||= begin
        time = Time.strptime(headers["x-amz-date"],"%Y%m%dT%H%M%SZ")
        time += time.utc_offset
        time.utc
      end
    end

    def canonical_headers
      @canonical_headers ||= begin
        signed = []
        hs = headers.sort_by { |name, _| name.downcase }.flat_map { |name, value|
          next if name == "Authorization"
          next if name == "Date"

          signed << name.downcase
          case value
          when Array
            value.each do |v|
              "#{name.downcase}:#{v.to_s.strip}\n"
            end
          else
            "#{name.downcase}:#{value.to_s.strip}\n"<commit_msg>Extract to a method<commit_after>class Aws4Signer
  class Signature
    def initialize(access_key_id, secret_access_key, region, service, verb, uri, headers, body)
      @access_key_id = access_key_id
      @secret_access_key = secret_access_key
      @region = region
      @service = service
      @verb = verb
      @uri = uri
      @headers = headers.dup
      @body = body

      @headers.each do |name, value|
        if value.kind_of?(Array)
          @headers[name] = value.first
        end
      end

      @headers["x-amz-date"] ||= @headers.delete("X-Amz-Date")
      unless @headers["x-amz-date"]
        @date = Time.now.utc
        @headers["x-amz-date"] = @date.strftime("%Y%m%dT%H%M%SZ")
      end
      @headers["Host"] ||= @headers.delete("host") || uri.host
    end

    attr_reader :region, :service, :verb, :uri, :headers, :body, :access_key_id, :secret_access_key

    def attach_to_http_request(req)
      headers.each do |name, value|
        req[name] = value
      end

      req["x-amz-content-sha256"] = Digest::SHA2.hexdigest(req.body || '', 256)
      req["Authorization"] = authorization_header

      req
    end

    def authorization_header
      "AWS4-HMAC-SHA256 " \
        "Credential=#{@access_key_id}/#{scope}," \
        "SignedHeaders=#{signed_headers}," \
        "Signature=#{signature}"
    end

    def date
      @date ||= begin
        time = Time.strptime(headers["x-amz-date"],"%Y%m%dT%H%M%SZ")
        time += time.utc_offset
        time.utc
      end
    end

    def canonical_headers
      @canonical_headers ||= begin
        signed = []
        hs = headers.sort_by { |name, _| name.downcase }.flat_map { |name, value|
          next if name == "Authorization"
          next if name == "Date"

          signed << name.downcase
          case value
          when Array
            value.each do |v|
              "#{name.downcase}:#{v.to_s.strip}\n"
            end
          else
            "#{name.downcase}:#{value.to_s.strip}\n"
{'fraction_non_alphanumeric': 0.14096016343207354, 'fraction_numerical': 0.006128702757916241, 'mean_word_length': 2.1690938511326863, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1526557', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1304, 'n_words': 254}