lib/sinatra/respond_with.rb in sinatra-contrib-1.3.2 vs lib/sinatra/respond_with.rb in sinatra-contrib-1.4.0
- old
+ new
@@ -1,13 +1,13 @@
-require 'sinatra/base'
require 'sinatra/json'
+require 'sinatra/base'
module Sinatra
- ##
+ #
# = Sinatra::RespondWith
#
- # This extensions lets Sinatra automatically choose what template to render or
+ # These extensions let Sinatra automatically choose what template to render or
# action to perform depending on the request's Accept header.
#
# Example:
#
# # Without Sinatra::RespondWith
@@ -51,22 +51,22 @@
# (+data.to_json+).
#
# == Security
#
# Since methods are triggered based on client input, this can lead to security
- # issues (but not as seviere as those might apear in the first place: keep in
- # mind that only known file extensions are used). You therefore should limit
+ # issues (but not as severe as those might appear in the first place: keep in
+ # mind that only known file extensions are used). You should limit
# the possible formats you serve.
#
# This is possible with the +provides+ condition:
#
# get '/', :provides => [:html, :json, :xml, :atom] do
# respond_with :index, :name => 'example'
# end
#
# However, since you have to set +provides+ for every route, this extension
- # adds a app global (class method) `respond_to`, that let's you define content
+ # adds an app global (class method) `respond_to`, that lets you define content
# types for all routes:
#
# respond_to :html, :json, :xml, :atom
# get('/a') { respond_with :index, :name => 'a' }
# get('/b') { respond_with :index, :name => 'b' }
@@ -116,13 +116,13 @@
end
end
@app.halt 406
end
- def method_missing(meth, *args, &block)
- return super if args.any? or block.nil? or not @app.mime_type(meth)
- on(meth, &block)
+ def method_missing(method, *args, &block)
+ return super if args.any? or block.nil? or not @app.mime_type(method)
+ on(method, &block)
end
end
module Helpers
include Sinatra::JSON
@@ -136,19 +136,22 @@
if template
args = template_cache.fetch(type, template) { template_for(template, exts) }
if args.any?
locals = { :object => object }
locals.merge! object.to_hash if object.respond_to? :to_hash
- args << { :locals => locals }
- halt send(*args)
+
+ renderer = args.first
+ options = args[1..-1] + [{:locals => locals}]
+
+ halt send(renderer, *options)
end
end
if object
exts.each do |ext|
halt json(object) if ext == :json
- next unless meth = "to_#{ext}" and object.respond_to? meth
- halt(*object.send(meth))
+ next unless object.respond_to? method = "to_#{ext}"
+ halt(*object.send(method))
end
end
false
end
format.finish(&block)
@@ -159,11 +162,11 @@
end
private
def template_for(name, exts)
- # in production this is cached, so don't worry to much about runtime
+ # in production this is cached, so don't worry too much about runtime
possible = []
settings.template_engines[:all].each do |engine|
exts.each { |ext| possible << [engine, "#{name}.#{ext}"] }
end
exts.each do |ext|
@@ -222,9 +225,10 @@
ENGINES = {
:css => [:less, :sass, :scss],
:xml => [:builder, :nokogiri],
:js => [:coffee],
+ :json => [:yajl],
:html => [:erb, :erubis, :haml, :slim, :liquid, :radius, :mab, :markdown,
:textile, :rdoc],
:all => Sinatra::Templates.instance_methods.map(&:to_sym) + [:mab] -
[:find_template, :markaby]
}