<commit_before>require "zlib"
require "stringio"
require "httpi/dime"
require "rack/utils"

module HTTPI

  # = HTTPI::Response
  #
  # Represents an HTTP response and contains various response details.
  class Response

    # Range of HTTP response codes considered to be successful.
    SuccessfulResponseCodes = 200..299

    # Initializer expects an HTTP response +code+, +headers+ and +body+.
    def initialize(code, headers, body)
      self.code = code.to_i
      self.headers = Rack::Utils::HeaderHash.new(headers)
      self.raw_body = body
    end

    attr_accessor :code, :headers, :raw_body, :attachments

    # Returns whether the HTTP response is considered successful.
    def error?
      !SuccessfulResponseCodes.include? code.to_i
    end

    def attachments
      decode_body unless @body
      @attachments ||= []
    end

    # Returns the HTTP response body.
    def body
      decode_body unless @body
      @body
    end

    attr_writer :body

  private

    def decode_body
      body = gzipped_response? ? decoded_gzip_body : raw_body
      @body = dime_response? ? decoded_dime_body(body) : body
    end

    # Returns whether the response is gzipped.
    def gzipped_response?
      headers["Content-Encoding"] == "gzip" || raw_body[0..1] == "\x1f\x8b"
    end

    # Returns whether this is a DIME response.
    def dime_response?
      headers['Content-Type'] == 'application/dime'
    end

    # Returns the gzip decoded response body.
    def decoded_gzip_body
      gzip = Zlib::GzipReader.new StringIO.new(raw_body)
      gzip.read
    ensure
      gzip.close
    end<commit_msg>code style<commit_after>require "zlib"
require "stringio"
require "httpi/dime"
require "rack/utils"

module HTTPI

  # = HTTPI::Response
  #
  # Represents an HTTP response and contains various response details.
  class Response

    # Range of HTTP response codes considered to be successful.
    SuccessfulResponseCodes = 200..299

    # Initializer expects an HTTP response +code+, +headers+ and +body+.
    def initialize(code, headers, body)
      self.code = code.to_i
      self.headers = Rack::Utils::HeaderHash.new(headers)
      self.raw_body = body
    end

    attr_accessor :code, :headers, :raw_body, :attachments

    # Returns whether the HTTP response is considered successful.
    def error?
      !SuccessfulResponseCodes.include? code.to_i
    end

    def attachments
      decode_body unless @body
      @attachments ||= []
    end

    # Returns the HTTP response body.
    def body
      decode_body unless @body
      @body
    end

    attr_writer :body

  private

    def decode_body
      body = gzipped_response? ? decoded_gzip_body : raw_body
      @body = dime_response? ? decoded_dime_body(body) : body
    end

    # Returns whether the response is gzipped.
    def gzipped_response?
      headers["Content-Encoding"] == "gzip" || raw_body[0..1] == "\x1f\x8b"
    end

    # Returns whether this is a DIME response.
    def dime_response?
      headers["Content-Type"] == "application/dime"
    end

    # Returns the gzip decoded response body.
    def decoded_gzip_body
      gzip = Zlib::GzipReader.new StringIO.new(raw_body)
      gzip.read
    ensure
      gzip.close
    end
{'fraction_non_alphanumeric': 0.0848034664190653, 'fraction_numerical': 0.0061900340451872485, 'mean_word_length': 2.811320754716981, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1419665', 'n_tokens_mistral': 1028, 'n_tokens_neox': 979, 'n_words': 288}