lib/grape-rabl/formatter.rb in grape-rabl-0.0.5 vs lib/grape-rabl/formatter.rb in grape-rabl-0.0.6
- old
+ new
@@ -1,53 +1,53 @@
-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(view_path(template))
- rendered = engine.render(current_endpoint, {})
- headers['Content-Type'] = content_types[env['api.format']]
- Rack::Response.new(rendered, status, headers).to_a
- end
- end
-
- private
-
- def view_path(template)
- if template.split(".")[-1] == "rabl"
- File.join(env['api.tilt.root'], template)
- else
- File.join(env['api.tilt.root'], (template + ".rabl"))
- 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
+module Grape
+ module Formatter
+ module Rabl
+ class << self
+
+ attr_reader :env
+ attr_reader :endpoint
+
+ def call(object, env)
+
+ @env = env
+ @endpoint = env['api.endpoint']
+
+ if rablable?
+ rabl do |template|
+ engine = ::Tilt.new(view_path(template))
+ engine.render endpoint, {}
+ end
+ else
+ Grape::Formatter::Json.call object, env
+ end
+
+ end
+
+ private
+
+ def view_path(template)
+ if template.split(".")[-1] == "rabl"
+ File.join(env['api.tilt.root'], template)
+ else
+ File.join(env['api.tilt.root'], (template + ".rabl"))
+ end
+ end
+
+ def rablable?
+ !! endpoint.options[:route_options][:rabl]
+ end
+
+ def rabl
+ template = endpoint.options[:route_options][:rabl]
+ raise "missing rabl template" unless template
+ set_view_root unless env['api.tilt.root']
+ yield template
+ end
+
+ def set_view_root
+ raise "Use Rack::Config to set 'api.tilt.root' in config.ru"
+ end
+
+ end
+ end
+ end
+end