Permalink
Browse files

Returning empty string in json response if provided object is nil

  • Loading branch information...
1 parent 7002b4a commit 7c70913c371ad53ccf3387dee7440f3c22025616 @gus gus committed Oct 2, 2009
Showing with 14 additions and 5 deletions.
  1. +2 −2 chicago.gemspec
  2. +2 −2 lib/chicago/responders.rb
  3. +10 −1 test/shoulda_tests/responders_test.rb
View
4 chicago.gemspec
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = "chicago"
- s.version = "0.3.3"
- s.date = "2009-07-05"
+ s.version = "0.3.4"
+ s.date = "2009-10-02"
s.summary = "Sinatra runtime and testing extensions used commonly by Thumblemonks"
s.email = %w[gus@gusg.us gabriel.gironda@gmail.com]
s.homepage = "http://github.com/thumblemonks/chicago/tree/master"
View
4 lib/chicago/responders.rb
@@ -2,10 +2,10 @@ module Sinatra
module Chicago
module Responders
- # Returns a JSON response for an object
+ # Returns a JSON response for an object. Will return an empty string if the provided object is nil.
def json_response(object)
content_type 'application/json'
- object.to_json
+ object ? object.to_json : ""
end
end # Responders
View
11 test/shoulda_tests/responders_test.rb
@@ -8,12 +8,21 @@ def app
status(201)
json_response({:foo => "bar"})
end
+
+ get "/no_bait" do
+ status(200)
+ json_response(nil)
+ end
}
end
context "json response" do
setup { get "/json_bait" }
- should_have_response_status 201
should_have_json_response({:foo => :bar})
end # json response
+
+ context "json response with null data" do
+ setup { get "/no_bait" }
+ should_have_json_response(/^$/)
+ end # json response
end

0 comments on commit 7c70913

Please sign in to comment.