Module: Tynn::JSON::InstanceMethods

Defined in:
lib/tynn/json.rb

Instance Method Summary collapse

Instance Method Details

#json(data) ⇒ Object

Public: Calls to_json on data and writes the generated JSON object into the response body. Also, It automatically sets the Content-Type header to application/json.

data - Any object that responds to to_json.

Examples

Tynn.define do
on("hash") do
  json(foo: "bar")
end

on("array") do
  json([1, 2, 3])
end

on("to_json") do
  json(Model.first)
end
end


39
40
41
42
43
# File 'lib/tynn/json.rb', line 39

def json(data)
  res.headers[Rack::CONTENT_TYPE] = Tynn::JSON::CONTENT_TYPE

  res.write(data.to_json)
end