<commit_before>
require "http-dump/version"
require 'webmock'

class HTTPDump
  class << self
    attr_accessor :output, :quiet_format
    def enable!(options = {})
      WebMock.enable!(options)
      WebMock.allow_net_connect!(options)
      WebMock.after_request do |request_signature, response|
        output.puts self.format(request_signature, response)
      end
      @enabled = true
    end

    def disable!(options = {})
      WebMock.reset_callbacks
      WebMock.disable_net_connect!(options)
      WebMock.disable!(options)
      @enabled = false
    end

    def dump(options = {}, &block)
      enabled = @enabled
      enable!(options) unless enabled
      block.call
    ensure
      disable!(options) unless enabled
    end

    def output
      @output || STDOUT
    end

    def format(request_signature, response)
      res = []
      res << "> #{request_signature}"
      res << "< #{response.status.join(' ')}"
      response.headers.each {|key, val| res << "< #{key}: #{val}" } if response.headers
      body = response.body
      unless body.empty?
        res << "<"
        if quiet_format && body.size > 100
          head = body[0..50]
          res << head.split("\n")[0]
          res << "* ... Response body is #{body.bytesize} bytes."
          tail = body[-50..-1]
          res << tail.split("\n")[-1]
        else
          res << body
        end
      end
      res.join("\n")
    end
  end
end

__END__

#HTTPDump.enable!
require 'open-uri'
HTTPDump.quiet_formatt = true
HTTPDump.dump {
  open('http://example.com').read
}
open('http://example.com').read

HTTPDump.dump {
  open('http://b.hatena.ne.jp/').read
}
<commit_msg>Remove comments<commit_after>
require "http-dump/version"
require 'webmock'

class HTTPDump
  class << self
    attr_accessor :output, :quiet_format
    def enable!(options = {})
      WebMock.enable!(options)
      WebMock.allow_net_connect!(options)
      WebMock.after_request do |request_signature, response|
        output.puts self.format(request_signature, response)
      end
      @enabled = true
    end

    def disable!(options = {})
      WebMock.reset_callbacks
      WebMock.disable_net_connect!(options)
      WebMock.disable!(options)
      @enabled = false
    end

    def dump(options = {}, &block)
      enabled = @enabled
      enable!(options) unless enabled
      block.call
    ensure
      disable!(options) unless enabled
    end

    def output
      @output || STDOUT
    end

    def format(request_signature, response)
      res = []
      res << "> #{request_signature}"
      res << "< #{response.status.join(' ')}"
      response.headers.each {|key, val| res << "< #{key}: #{val}" } if response.headers
      body = response.body
      unless body.empty?
        res << "<"
        if quiet_format && body.size > 100
          head = body[0..50]
          res << head.split("\n")[0]
          res << "* ... Response body is #{body.bytesize} bytes."
          tail = body[-50..-1]
          res << tail.split("\n")[-1]
        else
          res << body
        end
      end
      res.join("\n")
    end
  end
end

{'fraction_non_alphanumeric': 0.13624678663239073, 'fraction_numerical': 0.007069408740359897, 'mean_word_length': 2.212590299277606, 'pattern_counts': {'":': 0, '<': 45, '<?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': '4578044', 'n_tokens_mistral': 961, 'n_tokens_neox': 948, 'n_words': 216}