Class: Api::ResponseBuilder::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/api/response_builder/main.rb

Overview

Class which helps in building whole api response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, config = {}, params = {}) ⇒ Main

Returns a new instance of Main.



11
12
13
14
15
16
17
# File 'lib/api/response_builder/main.rb', line 11

def initialize(resource, config = {}, params = {})
  @resource = resource
  @config = config
  @response = {}
  @params = params
  set_response
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/api/response_builder/main.rb', line 6

def config
  @config
end

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'lib/api/response_builder/main.rb', line 6

def params
  @params
end

#resourceObject

Returns the value of attribute resource.



6
7
8
# File 'lib/api/response_builder/main.rb', line 6

def resource
  @resource
end

#responseObject

Returns the value of attribute response.



6
7
8
# File 'lib/api/response_builder/main.rb', line 6

def response
  @response
end

Instance Method Details

#prepare_responseObject



31
32
33
34
# File 'lib/api/response_builder/main.rb', line 31

def prepare_response
  status_msg = ::Api::ResponseBuilder::Status.new(resource, config).status_message
  @response = status_msg.present? ? status_msg : {}
end

#set_responseObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/api/response_builder/main.rb', line 19

def set_response
  prepare_response
  response_data = ::Api::ResponseBuilder::Data.new(resource, config).data
  messages = ::Api::ResponseBuilder::Messages.new(resource, config).messages
  status_code = ::Api::ResponseBuilder::StatusCode.new(resource, config).status_code

  @response[:body] = response_data if response_data.present?
  @response[:messages] = messages if messages.present?
  @response[:status_code] = status_code
  @response[:meta] = (config[:meta].to_h)
end