Sha256: 0a85a01b8b17d02cde3fadb8a25bd7d99e8656e42618b1b511ab2750b32cb0f6

Contents?: true

Size: 973 Bytes

Versions: 2

Compression:

Stored size: 973 Bytes

Contents

require 'tilt'
Rabl.register!

module Grape
  module Middleware
    class Formatter
      alias :old_after :after

      def after
        status, headers, bodies = *@app_response
        current_endpoint = env['api.endpoint']

        rabl(current_endpoint) do |template|
          engine = ::Tilt.new(File.join(env['api.tilt.root'], template))
          rendered = engine.render(current_endpoint, {})
          Rack::Response.new(rendered, status, headers).to_a
        end
      end

      def rabl(endpoint)
        if template = rablable?(endpoint)
          yield template
        else
          old_after
        end
      end

      def rablable?(endpoint)
        if template = endpoint.options[:route_options][:rabl]
          set_view_root unless env['api.tilt.root']
          template
        else
          false
        end
      end
  
      def set_view_root
        raise "Use Rack::Config to set 'api.tilt.root' in config.ru"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
grape-rabl-0.0.2 lib/grape-rabl/formatter.rb
grape-rabl-0.0.1 lib/grape-rabl/formatter.rb