Sha256: 5c4acc6ca2a9c39f56b2133892663c807efb276d9d53a1df01efe2610c333789

Contents?: true

Size: 1.69 KB

Versions: 12

Compression:

Stored size: 1.69 KB

Contents

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.to_hash)
  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:
    MultiJson.load(@stderr)
  end # :nocov:

  # Convert object to String, useful for debugging
  #
  # @return [String]
  def to_s # :nocov:
    s = []
    s << "CommandResponse:#{self.object_id}"
    s << "  status:   #{self.status}"
    s << "  stdout:   " + Debug.pretty_str(stdout)
    s << "  stderr:   " + Debug.pretty_str(stderr)
    s.join("\n")
  end # :nocov:

end # CommandResponse
end # Bixby

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
bixby-common-0.4.10 lib/bixby-common/command_response.rb
bixby-common-0.4.9 lib/bixby-common/command_response.rb
bixby-common-0.4.8 lib/bixby-common/command_response.rb
bixby-common-0.4.7 lib/bixby-common/command_response.rb
bixby-common-0.4.6 lib/bixby-common/command_response.rb
bixby-common-0.4.5 lib/bixby-common/command_response.rb
bixby-common-0.4.4 lib/bixby-common/command_response.rb
bixby-common-0.4.3 lib/bixby-common/command_response.rb
bixby-common-0.4.2 lib/bixby-common/command_response.rb
bixby-common-0.4.1 lib/bixby-common/command_response.rb
bixby-common-0.4.0 lib/bixby-common/command_response.rb
bixby-common-0.3.16 lib/bixby-common/command_response.rb