Sha256: 0d60c4c9a172fedfaadd6a9143576b509edc1e3e067e79ba146d1ea3255caf3d

Contents?: true

Size: 1001 Bytes

Versions: 2

Compression:

Stored size: 1001 Bytes

Contents

require "json"

class Tynn
  # Public: Adds helper methods for json generation.
  #
  # Examples
  #
  #   require "tynn"
  #   require "tynn/json"
  #
  #   Tynn.helpers(Tynn::JSON)
  #
  module JSON
    CONTENT_TYPE = "application/json".freeze

    module InstanceMethods
      # 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
      #
      def json(data)
        res.headers[Rack::CONTENT_TYPE] = Tynn::JSON::CONTENT_TYPE

        res.write(data.to_json)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tynn-1.0.0 lib/tynn/json.rb
tynn-1.0.0.rc3 lib/tynn/json.rb