require 'json' module Mks module Common class MethodResponse attr_accessor :success, :message, :data, :errors, :total def initialize(success = nil, message = nil, data = nil, errors = [], total = nil) @success = success @message = message @data = data @errors = errors @total = total end def self.success_response(data, msg = nil) if data.kind_of?(Array) || data.class.name == 'ActiveRecord::Relation' MethodResponse.new(true, msg, data[0].json(data), nil, data.count) else MethodResponse.new(true, msg, data.json, nil, nil) end end def self.failure_response(data) data.errors.details end end end end