From 50601b5b71fbfbe2eb35bc4280bbe5142d408bd3 Mon Sep 17 00:00:00 2001 From: Garret Alfert Date: Fri, 14 Sep 2012 17:20:57 +0200 Subject: [PATCH] Cleanup the code a little --- lib/remote_storage/riak.rb | 10 +++++++--- spec/riak_spec.rb | 7 ++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/remote_storage/riak.rb b/lib/remote_storage/riak.rb index 1e7949a..9b54361 100644 --- a/lib/remote_storage/riak.rb +++ b/lib/remote_storage/riak.rb @@ -8,6 +8,10 @@ module RemoteStorage @client ||= ::Riak::Client.new(LiquorCabinet.config['riak'].symbolize_keys) end + def data_bucket + @data_bucket ||= client.bucket("user_data") + end + def authorize_request(user, category, token) return true if category == "public" && env["REQUEST_METHOD"] == "GET" @@ -19,7 +23,7 @@ module RemoteStorage end 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 case object.content_type when "application/json" @@ -32,7 +36,7 @@ module RemoteStorage end 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" data = JSON.parse(data) if content_type == "application/json" if serializer_for(object.content_type) @@ -47,7 +51,7 @@ module RemoteStorage end 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] rescue ::Riak::HTTPFailedRequest halt 404 diff --git a/spec/riak_spec.rb b/spec/riak_spec.rb index eb2015a..f2b0bbe 100644 --- a/spec/riak_spec.rb +++ b/spec/riak_spec.rb @@ -94,9 +94,12 @@ describe "App with Riak backend" do end describe "PUT" do + before do + header "Authorization", "Bearer 123" + end + describe "with implicit content type" do before do - header "Authorization", "Bearer 123" put "/jimmy/documents/bar", "another text" end @@ -117,7 +120,6 @@ describe "App with Riak backend" do 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 @@ -143,7 +145,6 @@ describe "App with Riak backend" do describe "with arbitrary content type" do before do - header "Authorization", "Bearer 123" header "Content-Type", "text/magic" put "/jimmy/documents/magic", "pure magic" end