let Sinatra::RespondWith use Sinatra::JSON

This commit is contained in:
Konstantin Haase 2011-05-09 08:03:34 +02:00
parent e06d6417bf
commit 284b4b6d72
2 changed files with 20 additions and 8 deletions

View File

@ -1,5 +1,5 @@
require 'sinatra/base' require 'sinatra/base'
require 'json' unless Hash.method_defined? :to_json require 'sinatra/json'
module Sinatra module Sinatra
## ##
@ -125,6 +125,8 @@ module Sinatra
end end
module Helpers module Helpers
include Sinatra::JSON
def respond_with(template, object = nil, &block) def respond_with(template, object = nil, &block)
object, template = template, nil unless Symbol === template object, template = template, nil unless Symbol === template
format = Format.new(self) format = Format.new(self)
@ -142,6 +144,7 @@ module Sinatra
end end
if object if object
exts.each do |ext| exts.each do |ext|
halt json(object) if ext == :json
next unless meth = "to_#{ext}" and object.respond_to? meth next unless meth = "to_#{ext}" and object.respond_to? meth
halt(*object.send(meth)) halt(*object.send(meth))
end end

View File

@ -1,5 +1,6 @@
require 'backports' require 'backports'
require_relative 'spec_helper' require_relative 'spec_helper'
require_relative 'okjson'
describe Sinatra::RespondWith do describe Sinatra::RespondWith do
def provides(*args) def provides(*args)
@ -161,7 +162,7 @@ describe Sinatra::RespondWith do
describe "default behavior" do describe "default behavior" do
it 'converts objects to json out of the box' do it 'converts objects to json out of the box' do
respond_with 'a' => 'b' respond_with 'a' => 'b'
req(:json).body.should == {'a' => 'b'}.to_json OkJson.decode(req(:json).body).should == {'a' => 'b'}
end end
it 'handles multiple routes correctly' do it 'handles multiple routes correctly' do
@ -169,9 +170,9 @@ describe Sinatra::RespondWith do
get('/') { respond_with 'a' => 'b' } get('/') { respond_with 'a' => 'b' }
get('/:name') { respond_with 'a' => params[:name] } get('/:name') { respond_with 'a' => params[:name] }
end end
req('/', :json).body.should == {'a' => 'b'}.to_json OkJson.decode(req('/', :json).body).should == {'a' => 'b'}
req('/b', :json).body.should == {'a' => 'b'}.to_json OkJson.decode(req('/b', :json).body).should == {'a' => 'b'}
req('/c', :json).body.should == {'a' => 'c'}.to_json OkJson.decode(req('/c', :json).body).should == {'a' => 'c'}
end end
it "calls to_EXT if available" do it "calls to_EXT if available" do
@ -206,17 +207,25 @@ describe Sinatra::RespondWith do
body.should be_empty body.should be_empty
end end
it 'falls back to to_EXT if no template is found' do it 'falls back to #json if no template is found' do
respond_with :foo, :name => 'World' respond_with :foo, :name => 'World'
req(:json).should be_ok req(:json).should be_ok
body.should == {:name => 'World'}.to_json OkJson.decode(body).should == {'name' => 'World'}
end end
it 'favors templates over to_EXT' do it 'favors templates over #json' do
respond_with :bar, :name => 'World' respond_with :bar, :name => 'World'
req(:json).should be_ok req(:json).should be_ok
body.should == 'json!' body.should == 'json!'
end end
it 'falls back to to_EXT if no template is found' do
object = {:name => 'World'}
def object.to_pdf; "hi" end
respond_with :foo, object
req(:pdf).should be_ok
body.should == "hi"
end
end end
describe 'customizing' do describe 'customizing' do