Skip to content

Commit

Permalink
Jsonify reponds with http cache header of the actual source uri
Browse files Browse the repository at this point in the history
  • Loading branch information
selvakn committed Jun 3, 2012
1 parent d9ce856 commit 90730cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/jsonify.rb
Expand Up @@ -7,7 +7,8 @@ def initialize(route, model)
def call(env)
path = env['REQUEST_PATH']
response = path == @route ? all : one(find_id(path))
[200, {"Content-Type" => "application/json"}, [response]]
header = @model.http_cache_header.merge("Content-Type" => "application/json")
[200, header, [response]]
end

private
Expand All @@ -23,4 +24,4 @@ def one(id)
def find_id(path)
path[path.rindex('/') + 1, path.size]
end
end
end
15 changes: 14 additions & 1 deletion spec/jsonify_spec.rb
Expand Up @@ -2,6 +2,11 @@
require 'test_models'

describe Jsonify do
before do
::Pizza.stubs(:http_cache_header).returns("cache-control" => "private")
::Pizza.stubs(:all).returns([])
end

it "should find all objects and convert to json for index url" do
pizzas = [{name: 'cheese'}, {name: 'chicken'}]
::Pizza.expects(:all).returns(pizzas)
Expand All @@ -25,4 +30,12 @@
header['Content-Type'].should == 'application/json'
response.first.should == pizza.to_json
end
end

it "should forward the http cache headers" do
::Pizza.expects(:http_cache_header).returns("cache-control"=>"private")

jsonify = Jsonify.new('/pizzas', ::Pizza)
status, header, response = jsonify.call('REQUEST_PATH' => '/pizzas')
header['cache-control'].should == "private"
end
end

0 comments on commit 90730cf

Please sign in to comment.