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

.from_json(json_response) ⇒ Object



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

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

Instance Method Details

#to_hashObject



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

def to_hash
  hash = {}
  self.instance_variables.each do |var|
    hash[var.to_s.delete("@")] = self.instance_variable_get var
  end
  return hash
end