<commit_before>
module Bixby
class CommandResponse

  include Jsonify

  attr_accessor :status, :stdout, :stderr

  # Create a new CommandResponse from the given from_json_response
  #
  # @param [JsonResponse] res
  #
  # @return [CommandResponse]
  def self.from_json_response(res)
    cr = CommandResponse.new(res.data)
    if res.message then
      cr.status ||= 255
      cr.stderr = res.message
    end
    return cr
  end

  # Create a JsonResponse from this CommandResponse
  #
  # @return [JsonResponse]
  def to_json_response
    return JsonResponse.new((status == 0 ? "success" : "fail"), nil, self)
  end

  def initialize(params = nil)
    return if params.nil? or params.empty?
    params.each{ |k,v| self.send("#{k}=", v) if self.respond_to? "#{k}=" }
  end

  def success?
    @status.to_i == 0
  end

  def fail?
    not success?
  end
  alias_method :error?, :fail?

  def raise!
    if fail? then
      msg = stdout || ""
      msg += "\n" if !(stdout.nil? or stdout.empty?)
      msg += stderr || ""
      raise CommandException.new(msg, msg)
    end
  end

  def decode # :nocov:
    MultiJson.load(@stdout)
  end # :nocov:

  def decode_stderr # :nocov:<commit_msg>ease CommandResponse creation from shellout<commit_after>
module Bixby
class CommandResponse

  include Jsonify

  attr_accessor :status, :stdout, :stderr

  # Create a new CommandResponse from the given from_json_response
  #
  # @param [JsonResponse] res
  #
  # @return [CommandResponse]
  def self.from_json_response(res)
    cr = CommandResponse.new(res.data)
    if res.message then
      cr.status ||= 255
      cr.stderr ||= res.message
    end
    return cr
  end

  # Create a JsonResponse from this CommandResponse
  #
  # @return [JsonResponse]
  def to_json_response
    return JsonResponse.new((status == 0 ? "success" : "fail"), nil, self)
  end

  def initialize(params = nil)
    if params.kind_of? Hash then
      params.each{ |k,v| self.send("#{k}=", v) if self.respond_to? "#{k}=" }

    elsif params.class.to_s == "Mixlib::ShellOut" then
      @status = params.exitstatus
      @stdout = params.stdout
      @stderr = params.stderr
    end
  end

  def success?
    @status.to_i == 0
  end

  def fail?
    not success?
  end
  alias_method :error?, :fail?

  def raise!
    if fail? then
      msg = stdout || ""
      msg += "\n" if !(stdout.nil? or stdout.empty?)
      msg += stderr || ""
      raise CommandException.new(msg, msg)
    end
  end

  def decode # :nocov:
    MultiJson.load(@stdout)
  end # :nocov:

  def decode_stderr # :nocov:
{'fraction_non_alphanumeric': 0.11697965571205007, 'fraction_numerical': 0.003912363067292645, 'mean_word_length': 2.8509036144578315, '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': '231788', 'n_tokens_mistral': 878, 'n_tokens_neox': 856, 'n_words': 243}