Add support for custom content types

This commit is contained in:
Basti 2012-06-10 16:16:34 +02:00
parent 9d9fad77a0
commit 956f6fd3b3
4 changed files with 64 additions and 20 deletions

View File

@ -13,7 +13,7 @@ GEM
eventmachine (0.12.10) eventmachine (0.12.10)
i18n (0.6.0) i18n (0.6.0)
minitest (2.10.0) minitest (2.10.0)
multi_json (1.0.4) multi_json (1.3.6)
purdytest (1.0.0) purdytest (1.0.0)
minitest (~> 2.2) minitest (~> 2.2)
rack (1.3.5) rack (1.3.5)
@ -22,11 +22,11 @@ GEM
rack-test (0.6.1) rack-test (0.6.1)
rack (>= 1.0) rack (>= 1.0)
rake (0.9.2.2) rake (0.9.2.2)
riak-client (1.0.0) riak-client (1.0.3)
beefcake (~> 0.3.7) beefcake (~> 0.3.7)
builder (>= 2.1.2) builder (>= 2.1.2)
i18n (>= 0.4.0) i18n (>= 0.4.0)
multi_json (~> 1.0.0) multi_json (~> 1.0)
sinatra (1.3.1) sinatra (1.3.1)
rack (~> 1.3, >= 1.3.4) rack (~> 1.3, >= 1.3.4)
rack-protection (~> 1.1, >= 1.1.2) rack-protection (~> 1.1, >= 1.1.2)

View File

@ -1,4 +1,5 @@
require "riak" require "riak"
require "json"
module RemoteStorage module RemoteStorage
module Riak module Riak
@ -18,14 +19,22 @@ module RemoteStorage
end end
def get_data(user, category, key) def get_data(user, category, key)
client.bucket("user_data").get("#{user}:#{category}:#{key}").data object = client.bucket("user_data").get("#{user}:#{category}:#{key}")
headers["Content-Type"] = object.content_type
case object.content_type
when "application/json"
return object.data.to_json
else
return object.data
end
rescue ::Riak::HTTPFailedRequest rescue ::Riak::HTTPFailedRequest
halt 404 halt 404
end end
def put_data(user, category, key, data) def put_data(user, category, key, data, content_type=nil)
object = client.bucket("user_data").new("#{user}:#{category}:#{key}") object = client.bucket("user_data").new("#{user}:#{category}:#{key}")
object.content_type = "text/plain; charset=utf-8" object.content_type = content_type || "text/plain; charset=utf-8"
data = JSON.parse(data) if content_type == "application/json"
object.data = data object.data = data
object.indexes.merge!({:user_id_bin => [user]}) object.indexes.merge!({:user_id_bin => [user]})
object.store object.store

View File

@ -47,7 +47,14 @@ class LiquorCabinet < Sinatra::Base
put "/:user/:category/:key" do put "/:user/:category/:key" do
data = request.body.read data = request.body.read
put_data(@user, @category, @key, data)
if env['CONTENT_TYPE'] == "application/x-www-form-urlencoded"
content_type = "text/plain; charset=utf-8"
else
content_type = env['CONTENT_TYPE']
end
put_data(@user, @category, @key, data, content_type)
end end
delete "/:user/:category/:key" do delete "/:user/:category/:key" do

View File

@ -73,6 +73,7 @@ describe "App with Riak backend" do
end end
describe "PUT" do describe "PUT" do
describe "with implicit content type" do
before do before do
header "Authorization", "Bearer 123" header "Authorization", "Bearer 123"
put "/jimmy/documents/bar", "another text" put "/jimmy/documents/bar", "another text"
@ -93,6 +94,33 @@ describe "App with Riak backend" do
end end
end end
describe "with explicit content type" do
before do
header "Authorization", "Bearer 123"
header "Content-Type", "application/json"
put "/jimmy/documents/jason", '{"foo": "bar", "unhosted": 1}'
end
it "saves the value (as JSON)" do
last_response.status.must_equal 200
data_bucket.get("jimmy:documents:jason").data.must_be_kind_of Hash
data_bucket.get("jimmy:documents:jason").data.must_equal({"foo" => "bar", "unhosted" => 1})
end
it "uses the requested content type" do
data_bucket.get("jimmy:documents:jason").content_type.must_equal "application/json"
end
it "delivers the data correctly" do
header "Authorization", "Bearer 123"
get "/jimmy/documents/jason"
last_response.body.must_equal '{"foo":"bar","unhosted":1}'
last_response.content_type.must_equal "application/json"
end
end
end
describe "DELETE" do describe "DELETE" do
it "removes the key" do it "removes the key" do
header "Authorization", "Bearer 123" header "Authorization", "Bearer 123"