Permalink
Browse files

make RockingChair work with the latest CouchRest and RestClient

  • Loading branch information...
1 parent b6e39da commit 760e77852010b066b10df645b94daf02cac56291 @jweiss committed Sep 30, 2010
@@ -40,7 +40,7 @@ def self.post(uri, payload, headers={})
end
end
- def self.put(uri, payload, headers={})
+ def self.put(uri, payload=nil, headers={})
puts "PUT: #{uri.inspect}: #{payload.inspect} #{headers.inspect}" if @_rocking_chair_debug
url, parameters = RockingChair::Server.normalize_url(uri)
if url.match(/\A(#{URL_PARAMETER})\Z/)
@@ -59,18 +59,16 @@ def load(doc_id, options = {})
def []=(doc_id, document, options ={})
# options are ignored for now: batch, bulk
- json = nil
begin
- json = JSON.parse(document, :create_additions => false)
- raise "is not a Hash" unless json.is_a?(Hash)
+ document = normalize_payload(document)
rescue Exception => e
- raise RockingChair::Error.new(500, 'InvalidJSON', "the document is not a valid JSON object: #{e}")
+ raise RockingChair::Error.new(500, 'InvalidJSON', "the document #{doc_id} is not a valid JSON or Hash object: #{e}")
end
if exists?(doc_id)
- update(doc_id, json)
+ update(doc_id, document)
else
- insert(doc_id, json)
+ insert(doc_id, document)
end
end
@@ -97,19 +95,20 @@ def copy(original_id, new_id, rev=nil)
original.delete('_rev')
end
- self.store(new_id, original.to_json)
+ self.store(new_id, original)
end
def bulk(documents)
- documents = JSON.parse(documents, :create_additions => false)
response = []
- documents['docs'].each do |doc|
+ documents = normalize_payload(documents)
+ docs = documents[:docs] || documents['docs']
+ docs.each do |doc|
begin
if exists?(doc['_id']) && doc['_deleted'].to_s == 'true'
self.delete(doc['_id'], doc['_rev'])
state = {'id' => doc['_id'], 'rev' => doc['_rev']}
else
- state = JSON.parse(self.store(doc['_id'], doc.to_json))
+ state = JSON.parse(self.store(doc['_id'], doc))
end
response << {'id' => state['id'], 'rev' => state['rev']}
rescue RockingChair::Error => e
@@ -133,6 +132,12 @@ def view(design_doc_name, view_name, options = {})
protected
+ def normalize_payload(doc)
+ doc = JSON.parse(doc, :create_additions => false) unless doc.is_a?(Hash)
+ raise "is not a Hash" unless doc.is_a?(Hash)
+ doc
+ end
+
def state_tuple(_id, _rev)
{"ok" => true, "id" => _id, "rev" => _rev }.to_json
end
@@ -22,7 +22,7 @@ def raise_rest_client_error
when 404
raise RestClient::ResourceNotFound
when 409
- raise HttpAbstraction::Conflict
+ raise RestClient::Conflict
else
raise "Unknown error code: #{code.inspect}"
end
@@ -15,26 +15,27 @@ def http_adapter
end
def get(uri, headers=nil)
- http_adapter.get(uri, headers)
+ JSON.parse(http_adapter.get(uri, headers))
end
def post(uri, payload, headers=nil)
- http_adapter.post(uri, payload, headers)
+ JSON.parse(http_adapter.post(uri, payload, headers))
end
- def put(uri, payload, headers=nil)
- http_adapter.put(uri, payload, headers)
+ def put(uri, payload=nil, headers=nil)
+ JSON.parse(http_adapter.put(uri, payload, headers))
end
def delete(uri, headers=nil)
- http_adapter.delete(uri, headers)
+ JSON.parse(http_adapter.delete(uri, headers))
end
- def copy(uri, headers)
- http_adapter.copy(uri, headers)
+ def copy(uri, destination)
+ JSON.parse(http_adapter.copy(uri, default_headers.merge('Destination' => destination)))
end
end
end
-HttpAbstraction.extend(RockingChair::HttpAdapter)
+#::RestAPI.extend(RockingChair::HttpAdapter)
+CouchRest.extend(RockingChair::HttpAdapter)
@@ -55,6 +55,7 @@ def self.delete_db(name)
def self.store(db_name, doc_id, document, options)
return respond_with_error(404) unless databases.has_key?(db_name)
+
databases[db_name].store(doc_id, document, options)
rescue RockingChair::Error => e
e.raise_rest_client_error
View
@@ -236,7 +236,7 @@ class CouchRestTest < Test::Unit::TestCase
should "raise 409 on a revision conflict" do
@db.save_doc({'_id' => 'new-item', 'content' => 'here'})
- assert_raise(HttpAbstraction::Conflict) do
+ assert_raise(RestClient::Conflict) do
@db.save_doc({'_id' => 'new-item', 'content' => 'better', '_rev' => 'wrong-revision'})
end
@@ -275,7 +275,7 @@ class CouchRestTest < Test::Unit::TestCase
should "fail with conflich if the rev does not matche" do
@db.save_doc({'a' => 'b', '_id' => 'delete_me'})
- assert_raise(HttpAbstraction::Conflict) do
+ assert_raise(RestClient::Conflict) do
@db.delete_doc({'a' => 'b', '_id' => 'delete_me', '_rev' => 'wrong-revision'})
end
assert_nothing_raised do
@@ -310,7 +310,7 @@ class CouchRestTest < Test::Unit::TestCase
should "not copy with overwrite if the revision does not match" do
@db.save_doc({'1' => '2', '_id' => 'destination'})
- assert_raise(HttpAbstraction::Conflict) do
+ assert_raise(RestClient::Conflict) do
@db.copy_doc({'a' => 'b', '_id' => 'original', '_rev' => '123'}, 'destination?rev=not-here')
end
end
@@ -337,7 +337,7 @@ class CouchRestTest < Test::Unit::TestCase
should "not copy with overwrite if the revision does not match" do
@db.save_doc({'1' => '2', '_id' => 'destination'})
- assert_raise(HttpAbstraction::Conflict) do
+ assert_raise(RestClient::Conflict) do
@db.copy_doc({'a' => 'b', '_id' => 'original', '_rev' => '123'}, {'1' => '2', '_id' => 'destination', '_rev' => 'missing'})
end
end
View
@@ -6,6 +6,7 @@
require 'mocha'
require 'json/ext'
require 'active_support/inflector'
+require 'couchrest_extended_document'
require File.dirname(__FILE__) + "/fixtures/extended_couch_rest_fixtures"
require File.dirname(__FILE__) + "/fixtures/simply_stored_fixtures"
View
@@ -16,7 +16,7 @@ class ViewTest < Test::Unit::TestCase
'reduce' => "function(key, values){ return values.length }",
"map" => "function(doc) { if(doc.ruby_class && doc.ruby_class == 'Instance') { emit(doc['created_at'], null); } }"
}
- }}.to_json
+ }}
@db.stubs(:rev).returns('the-rev')
@@ -31,7 +31,7 @@ class ViewTest < Test::Unit::TestCase
'reduce' => "function(key, values){ return values.length }",
"map" => "function(doc) { if(doc.ruby_class && doc.ruby_class == 'Instance') { emit(doc['created_at'], null); } }"
}
- }}.to_json
+ }}
assert_nothing_raised do
JSON.parse(@db.view('user', 'by_firstname', {}))
@@ -57,7 +57,7 @@ class ViewTest < Test::Unit::TestCase
'reduce' => "function(key, values){ return values.length }",
"map" => "function(doc) {\n if(doc.ruby_class && doc.ruby_class == 'Instance') {\n emit(doc['created_at'], null);\n }\n }"
}
- }}.to_json
+ }}
@db.stubs(:rev).returns('the-rev')
end
@@ -77,9 +77,9 @@ class ViewTest < Test::Unit::TestCase
context "when querying by_attr_and_attr views" do
should "return all keys if no key is given" do
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
assert_equal({
"total_rows" => 3,
@@ -101,9 +101,9 @@ class ViewTest < Test::Unit::TestCase
end
should "return all docs if no key is given and we asked to include the docs" do
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
assert_equal({
"total_rows" => 3,
@@ -143,9 +143,9 @@ class ViewTest < Test::Unit::TestCase
end
should "return matching elements" do
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
assert_equal({
"total_rows" => 2,
@@ -175,9 +175,9 @@ class ViewTest < Test::Unit::TestCase
end
should "only return items with the correct klass matcher" do
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'Project'}.to_json
- @db['user_2'] = {"firstname" => 'Alf', 'lastname' => 'Michaels'}.to_json
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'Project'}
+ @db['user_2'] = {"firstname" => 'Alf', 'lastname' => 'Michaels'}
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
assert_equal({
"total_rows" => 1,
@@ -197,9 +197,9 @@ class ViewTest < Test::Unit::TestCase
end
should "support multiple attributes" do
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
assert_equal({
"total_rows" => 1,
@@ -219,9 +219,9 @@ class ViewTest < Test::Unit::TestCase
end
should "support startkey and endkey parameters" do
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
assert_equal(JSON.parse({
"total_rows" => 2,
@@ -253,9 +253,9 @@ class ViewTest < Test::Unit::TestCase
end
should "support startkey/endkey combined with startkey_docid/endkey_docid parameters" do
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
assert_equal(JSON.parse({
"total_rows" => 2,
@@ -280,8 +280,8 @@ class ViewTest < Test::Unit::TestCase
context "belongs_to" do
should "load parent" do
- @db['project_1'] = {"title" => 'alpha', 'ruby_class' => 'Project'}.to_json
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'project_id' => 'project_1', 'ruby_class' => 'User'}.to_json
+ @db['project_1'] = {"title" => 'alpha', 'ruby_class' => 'Project'}
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'project_id' => 'project_1', 'ruby_class' => 'User'}
assert_equal({
"total_rows" => 1,
@@ -304,9 +304,9 @@ class ViewTest < Test::Unit::TestCase
context "all_documents" do
should "load all documents of the matching class" do
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
- @db['project_1'] = {"title" => 'Alpha', 'ruby_class' => 'Project'}.to_json
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
+ @db['project_1'] = {"title" => 'Alpha', 'ruby_class' => 'Project'}
assert_equal({
"total_rows" => 2,
@@ -336,8 +336,8 @@ class ViewTest < Test::Unit::TestCase
end
should "limit the results if asked to" do
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
assert_equal({
"total_rows" => 2,
@@ -357,8 +357,8 @@ class ViewTest < Test::Unit::TestCase
end
should "count the objects with reduce" do
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
assert_equal({
"rows" => [{ "key" => nil, "value" => 2}]

0 comments on commit 760e778

Please sign in to comment.