Class: Mks::Common::MethodResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/mks/common/methodresponse.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success = nil, message = nil, data = nil, errors = [], total = nil) ⇒ MethodResponse

Returns a new instance of MethodResponse.



8
9
10
11
12
13
14
# File 'lib/mks/common/methodresponse.rb', line 8

def initialize(success = nil, message = nil, data = nil, errors = [], total = nil)
  @success = success
  @message = message
  @data = data
  @errors = errors
  @total = total
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/mks/common/methodresponse.rb', line 6

def data
  @data
end

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/mks/common/methodresponse.rb', line 6

def errors
  @errors
end

#messageObject

Returns the value of attribute message.



6
7
8
# File 'lib/mks/common/methodresponse.rb', line 6

def message
  @message
end

#successObject

Returns the value of attribute success.



6
7
8
# File 'lib/mks/common/methodresponse.rb', line 6

def success
  @success
end

#totalObject

Returns the value of attribute total.



6
7
8
# File 'lib/mks/common/methodresponse.rb', line 6

def total
  @total
end

Class Method Details

.error_response(data) ⇒ Object



27
28
29
30
# File 'lib/mks/common/methodresponse.rb', line 27

def self.error_response(data)
  errors = data.is_a?(String) ? [data] : data.errors
  return MethodResponse.new(false,nil, nil, errors, nil)
end

.failure_response(data) ⇒ Object



32
33
34
# File 'lib/mks/common/methodresponse.rb', line 32

def self.failure_response(data)
  data.errors.details
end

.success_response(data, msg = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/mks/common/methodresponse.rb', line 16

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
end