Cleanup the code a little
This commit is contained in:
@@ -8,6 +8,10 @@ module RemoteStorage
|
|||||||
@client ||= ::Riak::Client.new(LiquorCabinet.config['riak'].symbolize_keys)
|
@client ||= ::Riak::Client.new(LiquorCabinet.config['riak'].symbolize_keys)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def data_bucket
|
||||||
|
@data_bucket ||= client.bucket("user_data")
|
||||||
|
end
|
||||||
|
|
||||||
def authorize_request(user, category, token)
|
def authorize_request(user, category, token)
|
||||||
return true if category == "public" && env["REQUEST_METHOD"] == "GET"
|
return true if category == "public" && env["REQUEST_METHOD"] == "GET"
|
||||||
|
|
||||||
@@ -19,7 +23,7 @@ module RemoteStorage
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get_data(user, category, key)
|
def get_data(user, category, key)
|
||||||
object = client.bucket("user_data").get("#{user}:#{category}:#{key}")
|
object = data_bucket.get("#{user}:#{category}:#{key}")
|
||||||
headers["Content-Type"] = object.content_type
|
headers["Content-Type"] = object.content_type
|
||||||
case object.content_type
|
case object.content_type
|
||||||
when "application/json"
|
when "application/json"
|
||||||
@@ -32,7 +36,7 @@ module RemoteStorage
|
|||||||
end
|
end
|
||||||
|
|
||||||
def put_data(user, category, key, data, content_type=nil)
|
def put_data(user, category, key, data, content_type=nil)
|
||||||
object = client.bucket("user_data").new("#{user}:#{category}:#{key}")
|
object = data_bucket.new("#{user}:#{category}:#{key}")
|
||||||
object.content_type = 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"
|
data = JSON.parse(data) if content_type == "application/json"
|
||||||
if serializer_for(object.content_type)
|
if serializer_for(object.content_type)
|
||||||
@@ -47,7 +51,7 @@ module RemoteStorage
|
|||||||
end
|
end
|
||||||
|
|
||||||
def delete_data(user, category, key)
|
def delete_data(user, category, key)
|
||||||
riak_response = client.bucket("user_data").delete("#{user}:#{category}:#{key}")
|
riak_response = data_bucket.delete("#{user}:#{category}:#{key}")
|
||||||
halt riak_response[:code]
|
halt riak_response[:code]
|
||||||
rescue ::Riak::HTTPFailedRequest
|
rescue ::Riak::HTTPFailedRequest
|
||||||
halt 404
|
halt 404
|
||||||
|
|||||||
@@ -94,9 +94,12 @@ describe "App with Riak backend" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "PUT" do
|
describe "PUT" do
|
||||||
|
before do
|
||||||
|
header "Authorization", "Bearer 123"
|
||||||
|
end
|
||||||
|
|
||||||
describe "with implicit content type" do
|
describe "with implicit content type" do
|
||||||
before do
|
before do
|
||||||
header "Authorization", "Bearer 123"
|
|
||||||
put "/jimmy/documents/bar", "another text"
|
put "/jimmy/documents/bar", "another text"
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -117,7 +120,6 @@ describe "App with Riak backend" do
|
|||||||
|
|
||||||
describe "with explicit content type" do
|
describe "with explicit content type" do
|
||||||
before do
|
before do
|
||||||
header "Authorization", "Bearer 123"
|
|
||||||
header "Content-Type", "application/json"
|
header "Content-Type", "application/json"
|
||||||
put "/jimmy/documents/jason", '{"foo": "bar", "unhosted": 1}'
|
put "/jimmy/documents/jason", '{"foo": "bar", "unhosted": 1}'
|
||||||
end
|
end
|
||||||
@@ -143,7 +145,6 @@ describe "App with Riak backend" do
|
|||||||
|
|
||||||
describe "with arbitrary content type" do
|
describe "with arbitrary content type" do
|
||||||
before do
|
before do
|
||||||
header "Authorization", "Bearer 123"
|
|
||||||
header "Content-Type", "text/magic"
|
header "Content-Type", "text/magic"
|
||||||
put "/jimmy/documents/magic", "pure magic"
|
put "/jimmy/documents/magic", "pure magic"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user