class ApiTransformer::FrontendResponseBuilder

Processes the response block

Attributes

success[R]

Public Class Methods

new(env, options, block, route) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 6
def initialize(env, options, block, route)
  @env = env
  @options = options
  @block = block
  @route = route

  @frontend_response = FrontendResponse.new
end

Public Instance Methods

array(key, klass, array_data) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 65
def array(key, klass, array_data)
  value = array_data.map { |data| object_hash(klass, data) }
  @frontend_response.set key, value
end
attribute(key, value) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 57
def attribute(key, value)
  @frontend_response.set key, value
end
body(value) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 24
def body(value)
  @frontend_response.body = value
end
content_type(value) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 45
def content_type(value)
  @frontend_response.content_type = value
end
do_streaming() click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 82
def do_streaming
  @streaming_block.call if @streaming_block
end
failure(run = true, &block) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 37
def failure(run = true, &block)
  run && !@failure && @failure = block
end
header(key, value) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 53
def header(key, value)
  @frontend_response.set_header(key, value)
end
object(key, klass, object_data) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 61
def object(key, klass, object_data)
  @frontend_response.set key, object_hash(klass, object_data)
end
send_body() click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 70
def send_body
  stream_write @frontend_response.body
end
status(value) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 41
def status(value)
  @frontend_response.status = value
end
status_and_headers(backend_responses) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 15
def status_and_headers(backend_responses)
  @route[:helper_blocks].each { |block| instance_eval(&block) }
  instance_exec(backend_responses, &@block)
  add_failure_handlers(backend_responses)
  handle_success_or_failure(backend_responses)

  [@frontend_response.status, @frontend_response.headers]
end
stream(&block) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 74
def stream(&block)
  if block
    @streaming_block = block
  else
    fail "a block is required when streaming"
  end
end
stream_close() click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 90
def stream_close
  @env.stream_close
end
stream_write(data) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 86
def stream_write(data)
  @env.stream_send data
end
streaming?() click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 28
def streaming?
  options[:streaming]
end

Private Instance Methods

add_failure_handlers(backend_responses) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 96
def add_failure_handlers(backend_responses)
  @route[:failure_handlers].each do |block|
    backend_responses.values.each do |backend_response|
      instance_exec(backend_response, &block)
    end
  end
end
handle_failure() click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 114
def handle_failure
  set_failure_defaults
  instance_eval(&@failure) if @failure
end
handle_success() click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 119
def handle_success
  set_success_defaults
  instance_eval(&@success) if @success
end
handle_success_or_failure(backend_responses) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 104
def handle_success_or_failure(backend_responses)
  if @failure
    handle_failure
  elsif backend_responses.all? { |_, resp| resp.success? }
    handle_success
  else
    unhandled_failure(backend_responses)
  end
end
object_hash(klass, data) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 130
def object_hash(klass, data)
  klass.new(data).to_hash
end
set_failure_defaults() click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 138
def set_failure_defaults
  @frontend_response.status ||= 400
end
set_success_defaults() click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 134
def set_success_defaults
  @frontend_response.status ||= 200
end
unhandled_failure(backend_responses) click to toggle source
# File lib/api_transformer/frontend_response_builder.rb, line 124
def unhandled_failure(backend_responses)
  first_failure = backend_responses.values.first { |resp| !resp.success }
  status first_failure.status
  @frontend_response.body = first_failure.body
end