mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
let Sinatra::RespondWith use Sinatra::JSON
This commit is contained in:
parent
e06d6417bf
commit
284b4b6d72
2 changed files with 20 additions and 8 deletions
|
@ -1,5 +1,5 @@
|
|||
require 'sinatra/base'
|
||||
require 'json' unless Hash.method_defined? :to_json
|
||||
require 'sinatra/json'
|
||||
|
||||
module Sinatra
|
||||
##
|
||||
|
@ -125,6 +125,8 @@ module Sinatra
|
|||
end
|
||||
|
||||
module Helpers
|
||||
include Sinatra::JSON
|
||||
|
||||
def respond_with(template, object = nil, &block)
|
||||
object, template = template, nil unless Symbol === template
|
||||
format = Format.new(self)
|
||||
|
@ -142,6 +144,7 @@ module Sinatra
|
|||
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))
|
||||
end
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
require 'backports'
|
||||
require_relative 'spec_helper'
|
||||
require_relative 'okjson'
|
||||
|
||||
describe Sinatra::RespondWith do
|
||||
def provides(*args)
|
||||
|
@ -161,7 +162,7 @@ describe Sinatra::RespondWith do
|
|||
describe "default behavior" do
|
||||
it 'converts objects to json out of the box' do
|
||||
respond_with 'a' => 'b'
|
||||
req(:json).body.should == {'a' => 'b'}.to_json
|
||||
OkJson.decode(req(:json).body).should == {'a' => 'b'}
|
||||
end
|
||||
|
||||
it 'handles multiple routes correctly' do
|
||||
|
@ -169,9 +170,9 @@ describe Sinatra::RespondWith do
|
|||
get('/') { respond_with 'a' => 'b' }
|
||||
get('/:name') { respond_with 'a' => params[:name] }
|
||||
end
|
||||
req('/', :json).body.should == {'a' => 'b'}.to_json
|
||||
req('/b', :json).body.should == {'a' => 'b'}.to_json
|
||||
req('/c', :json).body.should == {'a' => 'c'}.to_json
|
||||
OkJson.decode(req('/', :json).body).should == {'a' => 'b'}
|
||||
OkJson.decode(req('/b', :json).body).should == {'a' => 'b'}
|
||||
OkJson.decode(req('/c', :json).body).should == {'a' => 'c'}
|
||||
end
|
||||
|
||||
it "calls to_EXT if available" do
|
||||
|
@ -206,17 +207,25 @@ describe Sinatra::RespondWith do
|
|||
body.should be_empty
|
||||
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'
|
||||
req(:json).should be_ok
|
||||
body.should == {:name => 'World'}.to_json
|
||||
OkJson.decode(body).should == {'name' => 'World'}
|
||||
end
|
||||
|
||||
it 'favors templates over to_EXT' do
|
||||
it 'favors templates over #json' do
|
||||
respond_with :bar, :name => 'World'
|
||||
req(:json).should be_ok
|
||||
body.should == 'json!'
|
||||
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
|
||||
|
||||
describe 'customizing' do
|
||||
|
|
Loading…
Add table
Reference in a new issue