<commit_before>require 'json/pure'
require 'coderay'
require 'curb'

class Command < ActiveRecord::Base
  serialize :param_keys, Array
  serialize :param_values, Array
  serialize :header_keys, Array
  serialize :header_values, Array

  validates_inclusion_of :method, :in => %w(GET POST PUT DELETE)
  validates_format_of :url, :with => URI::regexp(%w(http https))

  def after_initialize
    data = []
    if param_keys
      param_keys.each_with_index do |key, i|
        data << "#{key}=#{param_values[i]}" if key.present?
      end
    end

    @data = data.join("&")
    @command = make_command(url, @data)
    case method
      when "POST"
        @command.post_body = @data
      when "PUT"
        @command.put_data = @data
    end

    @command.useragent = "Whurl/1.0"

    if header_keys
      header_keys.each_with_index do |key, i|
        @command.headers[key] = header_values[i] if key.present?
      end
    end
  end

  def send_request
    if method == "PUT"
      @command.send("http_#{method.downcase}", @data)
    else
      @command.send("http_#{method.downcase}")
    end
  end

  def body_str
    @command.body_str
  end

  def header_str
    @command.header_str
  end

  def response_html(options = {:line_numbers => :table})
    case @command.content_type
      when /xml/
        CodeRay.scan(@command.body_str, :xml).div(options)
      when /html/
        CodeRay.scan(@command.body_str, :html).div(options)
      when /json/
        CodeRay.scan(JSON.pretty_generate(JSON.parse(@command.body_str)), :json).div(options)
      else
        @command.body_str
    end
  end

  def header_html
    CodeRay.scan(@command.header_str, :txt).div(:line_numbers => nil)
  end
<commit_msg>support image type responses<commit_after>require 'json/pure'
require 'coderay'
require 'curb'

class Command < ActiveRecord::Base
  serialize :param_keys, Array
  serialize :param_values, Array
  serialize :header_keys, Array
  serialize :header_values, Array

  validates_inclusion_of :method, :in => %w(GET POST PUT DELETE)
  validates_format_of :url, :with => URI::regexp(%w(http https))

  def after_initialize
    data = []
    if param_keys
      param_keys.each_with_index do |key, i|
        data << "#{key}=#{param_values[i]}" if key.present?
      end
    end

    @data = data.join("&")
    @command = make_command(url, @data)
    case method
      when "POST"
        @command.post_body = @data
      when "PUT"
        @command.put_data = @data
    end

    @command.useragent = "Whurl/1.0"

    if header_keys
      header_keys.each_with_index do |key, i|
        @command.headers[key] = header_values[i] if key.present?
      end
    end
  end

  def send_request
    if method == "PUT"
      @command.send("http_#{method.downcase}", @data)
    else
      @command.send("http_#{method.downcase}")
    end
  end

  def body_str
    @command.body_str
  end

  def header_str
    @command.header_str
  end

  def response_html(options = {:line_numbers => :table})
    case @command.content_type
      when /xml/
        CodeRay.scan(@command.body_str, :xml).div(options)
      when /html/
        CodeRay.scan(@command.body_str, :html).div(options)
      when /json/
        CodeRay.scan(JSON.pretty_generate(JSON.parse(@command.body_str)), :json).div(options)
      when /jpg|jpeg|gif|png|bmp|binary/
        "<img src=\"data:binary/octet;base64,#{Base64.encode64(@command.body_str)}\" />"
      else
        @command.body_str
    end
  end

  def header_html
    CodeRay.scan(@command.header_str, :txt).div(:line_numbers => nil)
  end

{'fraction_non_alphanumeric': 0.12461322081575246, 'fraction_numerical': 0.0028129395218002813, 'mean_word_length': 2.929281767955801, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '3422579', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1205, 'n_words': 259}