class Rack::Transform::Transformers::Base::Response

Constants

CONTENT_LENGTH
CONTENT_TYPE
JSON_TYPE

Attributes

body[R]
header[R]
status[R]

Public Class Methods

call(status, header, body) click to toggle source
# File lib/rack/transform/transformers/base.rb, line 57
def self.call(status, header, body)
  new(status, header, body).process
end
new(status, header, body) click to toggle source
# File lib/rack/transform/transformers/base.rb, line 63
def initialize(status, header, body)
  @status = status
  @header = header.merge CONTENT_TYPE => JSON_TYPE
  @body = body
end

Public Instance Methods

process() click to toggle source
# File lib/rack/transform/transformers/base.rb, line 69
def process
  raise NotImplementedError
end

Private Instance Methods

dump_json(data) click to toggle source
# File lib/rack/transform/transformers/base.rb, line 87
def dump_json(data)
  JSON.dump(data)
end
load_json(data) click to toggle source
# File lib/rack/transform/transformers/base.rb, line 91
def load_json(data)
  JSON.load(data)
end
parsed_body() click to toggle source
# File lib/rack/transform/transformers/base.rb, line 83
def parsed_body
  @parsed_body ||= body.empty? ? [] : load_json(body.first)
end
respond_with(body, status: 200, header: @header) click to toggle source
# File lib/rack/transform/transformers/base.rb, line 75
def respond_with(body, status: 200, header: @header)
  formatted_body = body.is_a?(String) ? body : dump_json(body)

  header[CONTENT_LENGTH] = formatted_body.bytesize.to_s

  [status, header, [formatted_body]]
end