Class: Smooth::Response

Inherits:
Object show all
Defined in:
lib/smooth/response.rb

Direct Known Subclasses

ErrorResponse, Query::Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(outcome, serializer_options = {}) ⇒ Response

Returns a new instance of Response.



7
8
9
10
# File 'lib/smooth/response.rb', line 7

def initialize(outcome, serializer_options = {})
  @outcome = outcome
  @serializer_options = serializer_options
end

Instance Attribute Details

#command_actionObject

Returns the value of attribute command_action.



5
6
7
# File 'lib/smooth/response.rb', line 5

def command_action
  @command_action
end

#current_userObject

Returns the value of attribute current_user.



5
6
7
# File 'lib/smooth/response.rb', line 5

def current_user
  @current_user
end

#event_namespaceObject

Returns the value of attribute event_namespace.



5
6
7
# File 'lib/smooth/response.rb', line 5

def event_namespace
  @event_namespace
end

#objectObject

Returns the value of attribute object.



5
6
7
# File 'lib/smooth/response.rb', line 5

def object
  @object
end

#outcomeObject (readonly)

Returns the value of attribute outcome.



3
4
5
# File 'lib/smooth/response.rb', line 3

def outcome
  @outcome
end

#request_headersObject

Returns the value of attribute request_headers.



5
6
7
# File 'lib/smooth/response.rb', line 5

def request_headers
  @request_headers
end

#serializerObject

Returns the value of attribute serializer.



5
6
7
# File 'lib/smooth/response.rb', line 5

def serializer
  @serializer
end

#serializer_klassObject

Returns the value of attribute serializer_klass.



5
6
7
# File 'lib/smooth/response.rb', line 5

def serializer_klass
  @serializer_klass
end

#serializer_optionsObject (readonly)

Returns the value of attribute serializer_options.



3
4
5
# File 'lib/smooth/response.rb', line 3

def serializer_options
  @serializer_options
end

#successObject

Returns the value of attribute success.



5
6
7
# File 'lib/smooth/response.rb', line 5

def success
  @success
end

Instance Method Details

#bodyObject



30
31
32
# File 'lib/smooth/response.rb', line 30

def body
  serializer.new(object, options).to_json(options)
end

#headersObject



16
17
18
19
20
# File 'lib/smooth/response.rb', line 16

def headers
  {
    'Content-Type' => 'application/json'
  }
end

#optionsObject



22
23
24
25
26
27
28
# File 'lib/smooth/response.rb', line 22

def options
  if success? && serializer
    (@serializer_options || {}).merge(serializer: serializer, scope: current_user)
  else
    @serializer_options.merge(scope: current_user)
  end
end

#statusObject



48
49
50
51
52
53
54
55
# File 'lib/smooth/response.rb', line 48

def status
  case
  when success?
    200
  else
    400
  end
end

#success?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/smooth/response.rb', line 44

def success?
  @success || (outcome && outcome.success?)
end

#to_rackObject



12
13
14
# File 'lib/smooth/response.rb', line 12

def to_rack
  [status, headers, [body]]
end