lib/mks/common/methodresponse.rb in mks_common-1.0.10 vs lib/mks/common/methodresponse.rb in mks_common-1.1.0
- old
+ new
@@ -3,35 +3,33 @@
module Mks
module Common
class MethodResponse
attr_accessor :success, :message, :data, :errors, :total
- def initialize(success = nil, message = nil, data = nil, errors = [], total = nil)
+ 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)
- names = %w(ActiveRecord::Relation ActiveRecord::Associations::CollectionProxy)
- return MethodResponse.new(true, msg, nil, nil, nil) if data.nil?
- if data.kind_of?(Array) || names.include?(data.class.name)
- return MethodResponse.new(true, msg, [], nil, nil) if data.count.zero?
- return MethodResponse.new(true, msg, data[0].json(data), nil, data.count)
- else
- return MethodResponse.new(true, msg, data.json, nil, nil)
- end
+ def self.from_json(json_response)
+ response = JSON.parse json_response
+ success = response['success'] if response.has_key?('success')
+ message = response['message'] if response.has_key?('message')
+ data = response['data'] if response.has_key?('data')
+ errors = response['errors'] if response.has_key?('errors')
+ total = response['total'] if response.has_key?('total')
+ MethodResponse.new(success, message, data, errors, total)
end
- def self.error_response(data)
- errors = data.is_a?(String) ? [data] : data.errors
- return MethodResponse.new(false,nil, nil, errors, nil)
+ def to_hash
+ hash = {}
+ self.instance_variables.each do |var|
+ hash[var.to_s.delete("@")] = self.instance_variable_get var
+ end
+ return hash
end
-
- def self.failure_response(data)
- data.errors.details
- end
end
end
-end
\ No newline at end of file
+end