Skip to content

Commit

Permalink
make RockingChair work with the latest CouchRest and RestClient
Browse files Browse the repository at this point in the history
  • Loading branch information
jweiss committed Sep 30, 2010
1 parent b6e39da commit 760e778
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 57 deletions.
2 changes: 1 addition & 1 deletion lib/rocking_chair/couch_rest_http_adapter.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.post(uri, payload, headers={})
end end
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 puts "PUT: #{uri.inspect}: #{payload.inspect} #{headers.inspect}" if @_rocking_chair_debug
url, parameters = RockingChair::Server.normalize_url(uri) url, parameters = RockingChair::Server.normalize_url(uri)
if url.match(/\A(#{URL_PARAMETER})\Z/) if url.match(/\A(#{URL_PARAMETER})\Z/)
Expand Down
25 changes: 15 additions & 10 deletions lib/rocking_chair/database.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -59,18 +59,16 @@ def load(doc_id, options = {})


def []=(doc_id, document, options ={}) def []=(doc_id, document, options ={})
# options are ignored for now: batch, bulk # options are ignored for now: batch, bulk
json = nil
begin begin
json = JSON.parse(document, :create_additions => false) document = normalize_payload(document)
raise "is not a Hash" unless json.is_a?(Hash)
rescue Exception => e 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 end


if exists?(doc_id) if exists?(doc_id)
update(doc_id, json) update(doc_id, document)
else else
insert(doc_id, json) insert(doc_id, document)
end end
end end


Expand All @@ -97,19 +95,20 @@ def copy(original_id, new_id, rev=nil)
original.delete('_rev') original.delete('_rev')
end end


self.store(new_id, original.to_json) self.store(new_id, original)
end end


def bulk(documents) def bulk(documents)
documents = JSON.parse(documents, :create_additions => false)
response = [] response = []
documents['docs'].each do |doc| documents = normalize_payload(documents)
docs = documents[:docs] || documents['docs']
docs.each do |doc|
begin begin
if exists?(doc['_id']) && doc['_deleted'].to_s == 'true' if exists?(doc['_id']) && doc['_deleted'].to_s == 'true'
self.delete(doc['_id'], doc['_rev']) self.delete(doc['_id'], doc['_rev'])
state = {'id' => doc['_id'], 'rev' => doc['_rev']} state = {'id' => doc['_id'], 'rev' => doc['_rev']}
else else
state = JSON.parse(self.store(doc['_id'], doc.to_json)) state = JSON.parse(self.store(doc['_id'], doc))
end end
response << {'id' => state['id'], 'rev' => state['rev']} response << {'id' => state['id'], 'rev' => state['rev']}
rescue RockingChair::Error => e rescue RockingChair::Error => e
Expand All @@ -133,6 +132,12 @@ def view(design_doc_name, view_name, options = {})


protected 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) def state_tuple(_id, _rev)
{"ok" => true, "id" => _id, "rev" => _rev }.to_json {"ok" => true, "id" => _id, "rev" => _rev }.to_json
end end
Expand Down
2 changes: 1 addition & 1 deletion lib/rocking_chair/error.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def raise_rest_client_error
when 404 when 404
raise RestClient::ResourceNotFound raise RestClient::ResourceNotFound
when 409 when 409
raise HttpAbstraction::Conflict raise RestClient::Conflict
else else
raise "Unknown error code: #{code.inspect}" raise "Unknown error code: #{code.inspect}"
end end
Expand Down
17 changes: 9 additions & 8 deletions lib/rocking_chair/http_adapter.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@ def http_adapter
end end


def get(uri, headers=nil) def get(uri, headers=nil)
http_adapter.get(uri, headers) JSON.parse(http_adapter.get(uri, headers))
end end


def post(uri, payload, headers=nil) def post(uri, payload, headers=nil)
http_adapter.post(uri, payload, headers) JSON.parse(http_adapter.post(uri, payload, headers))
end end


def put(uri, payload, headers=nil) def put(uri, payload=nil, headers=nil)
http_adapter.put(uri, payload, headers) JSON.parse(http_adapter.put(uri, payload, headers))
end end


def delete(uri, headers=nil) def delete(uri, headers=nil)
http_adapter.delete(uri, headers) JSON.parse(http_adapter.delete(uri, headers))
end end


def copy(uri, headers) def copy(uri, destination)
http_adapter.copy(uri, headers) JSON.parse(http_adapter.copy(uri, default_headers.merge('Destination' => destination)))
end end


end end
end end


HttpAbstraction.extend(RockingChair::HttpAdapter) #::RestAPI.extend(RockingChair::HttpAdapter)
CouchRest.extend(RockingChair::HttpAdapter)
1 change: 1 addition & 0 deletions lib/rocking_chair/server.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def self.delete_db(name)


def self.store(db_name, doc_id, document, options) def self.store(db_name, doc_id, document, options)
return respond_with_error(404) unless databases.has_key?(db_name) return respond_with_error(404) unless databases.has_key?(db_name)

databases[db_name].store(doc_id, document, options) databases[db_name].store(doc_id, document, options)
rescue RockingChair::Error => e rescue RockingChair::Error => e
e.raise_rest_client_error e.raise_rest_client_error
Expand Down
8 changes: 4 additions & 4 deletions test/couch_rest_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class CouchRestTest < Test::Unit::TestCase


should "raise 409 on a revision conflict" do should "raise 409 on a revision conflict" do
@db.save_doc({'_id' => 'new-item', 'content' => 'here'}) @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'}) @db.save_doc({'_id' => 'new-item', 'content' => 'better', '_rev' => 'wrong-revision'})
end end


Expand Down Expand Up @@ -275,7 +275,7 @@ class CouchRestTest < Test::Unit::TestCase


should "fail with conflich if the rev does not matche" do should "fail with conflich if the rev does not matche" do
@db.save_doc({'a' => 'b', '_id' => 'delete_me'}) @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'}) @db.delete_doc({'a' => 'b', '_id' => 'delete_me', '_rev' => 'wrong-revision'})
end end
assert_nothing_raised do assert_nothing_raised do
Expand Down Expand Up @@ -310,7 +310,7 @@ class CouchRestTest < Test::Unit::TestCase


should "not copy with overwrite if the revision does not match" do should "not copy with overwrite if the revision does not match" do
@db.save_doc({'1' => '2', '_id' => 'destination'}) @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') @db.copy_doc({'a' => 'b', '_id' => 'original', '_rev' => '123'}, 'destination?rev=not-here')
end end
end end
Expand All @@ -337,7 +337,7 @@ class CouchRestTest < Test::Unit::TestCase


should "not copy with overwrite if the revision does not match" do should "not copy with overwrite if the revision does not match" do
@db.save_doc({'1' => '2', '_id' => 'destination'}) @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'}) @db.copy_doc({'a' => 'b', '_id' => 'original', '_rev' => '123'}, {'1' => '2', '_id' => 'destination', '_rev' => 'missing'})
end end
end end
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'mocha' require 'mocha'
require 'json/ext' require 'json/ext'
require 'active_support/inflector' require 'active_support/inflector'
require 'couchrest_extended_document'


require File.dirname(__FILE__) + "/fixtures/extended_couch_rest_fixtures" require File.dirname(__FILE__) + "/fixtures/extended_couch_rest_fixtures"
require File.dirname(__FILE__) + "/fixtures/simply_stored_fixtures" require File.dirname(__FILE__) + "/fixtures/simply_stored_fixtures"
Expand Down
66 changes: 33 additions & 33 deletions test/view_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ViewTest < Test::Unit::TestCase
'reduce' => "function(key, values){ return values.length }", 'reduce' => "function(key, values){ return values.length }",
"map" => "function(doc) { if(doc.ruby_class && doc.ruby_class == 'Instance') { emit(doc['created_at'], null); } }" "map" => "function(doc) { if(doc.ruby_class && doc.ruby_class == 'Instance') { emit(doc['created_at'], null); } }"
} }
}}.to_json }}


@db.stubs(:rev).returns('the-rev') @db.stubs(:rev).returns('the-rev')


Expand All @@ -31,7 +31,7 @@ class ViewTest < Test::Unit::TestCase
'reduce' => "function(key, values){ return values.length }", 'reduce' => "function(key, values){ return values.length }",
"map" => "function(doc) { if(doc.ruby_class && doc.ruby_class == 'Instance') { emit(doc['created_at'], null); } }" "map" => "function(doc) { if(doc.ruby_class && doc.ruby_class == 'Instance') { emit(doc['created_at'], null); } }"
} }
}}.to_json }}


assert_nothing_raised do assert_nothing_raised do
JSON.parse(@db.view('user', 'by_firstname', {})) JSON.parse(@db.view('user', 'by_firstname', {}))
Expand All @@ -57,7 +57,7 @@ class ViewTest < Test::Unit::TestCase
'reduce' => "function(key, values){ return values.length }", '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 }" "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') @db.stubs(:rev).returns('the-rev')
end end
Expand All @@ -77,9 +77,9 @@ class ViewTest < Test::Unit::TestCase
context "when querying by_attr_and_attr views" do context "when querying by_attr_and_attr views" do


should "return all keys if no key is given" 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_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}


assert_equal({ assert_equal({
"total_rows" => 3, "total_rows" => 3,
Expand All @@ -101,9 +101,9 @@ class ViewTest < Test::Unit::TestCase
end end


should "return all docs if no key is given and we asked to include the docs" do 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_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}


assert_equal({ assert_equal({
"total_rows" => 3, "total_rows" => 3,
Expand Down Expand Up @@ -143,9 +143,9 @@ class ViewTest < Test::Unit::TestCase
end end


should "return matching elements" do should "return matching elements" do
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', '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'}.to_json @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}


assert_equal({ assert_equal({
"total_rows" => 2, "total_rows" => 2,
Expand Down Expand Up @@ -175,9 +175,9 @@ class ViewTest < Test::Unit::TestCase
end end


should "only return items with the correct klass matcher" do should "only return items with the correct klass matcher" do
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'Project'}.to_json @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'Project'}
@db['user_2'] = {"firstname" => 'Alf', 'lastname' => 'Michaels'}.to_json @db['user_2'] = {"firstname" => 'Alf', 'lastname' => 'Michaels'}
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}


assert_equal({ assert_equal({
"total_rows" => 1, "total_rows" => 1,
Expand All @@ -197,9 +197,9 @@ class ViewTest < Test::Unit::TestCase
end end


should "support multiple attributes" do should "support multiple attributes" do
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', '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'}.to_json @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}


assert_equal({ assert_equal({
"total_rows" => 1, "total_rows" => 1,
Expand All @@ -219,9 +219,9 @@ class ViewTest < Test::Unit::TestCase
end end


should "support startkey and endkey parameters" do should "support startkey and endkey parameters" do
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', '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'}.to_json @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}


assert_equal(JSON.parse({ assert_equal(JSON.parse({
"total_rows" => 2, "total_rows" => 2,
Expand Down Expand Up @@ -253,9 +253,9 @@ class ViewTest < Test::Unit::TestCase
end end


should "support startkey/endkey combined with startkey_docid/endkey_docid parameters" do 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_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
@db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
@db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}


assert_equal(JSON.parse({ assert_equal(JSON.parse({
"total_rows" => 2, "total_rows" => 2,
Expand All @@ -280,8 +280,8 @@ class ViewTest < Test::Unit::TestCase


context "belongs_to" do context "belongs_to" do
should "load parent" do should "load parent" do
@db['project_1'] = {"title" => 'alpha', 'ruby_class' => 'Project'}.to_json @db['project_1'] = {"title" => 'alpha', 'ruby_class' => 'Project'}
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'project_id' => 'project_1', 'ruby_class' => 'User'}.to_json @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'project_id' => 'project_1', 'ruby_class' => 'User'}


assert_equal({ assert_equal({
"total_rows" => 1, "total_rows" => 1,
Expand All @@ -304,9 +304,9 @@ class ViewTest < Test::Unit::TestCase


context "all_documents" do context "all_documents" do
should "load all documents of the matching class" do should "load all documents of the matching class" do
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', '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'}.to_json @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
@db['project_1'] = {"title" => 'Alpha', 'ruby_class' => 'Project'}.to_json @db['project_1'] = {"title" => 'Alpha', 'ruby_class' => 'Project'}


assert_equal({ assert_equal({
"total_rows" => 2, "total_rows" => 2,
Expand Down Expand Up @@ -336,8 +336,8 @@ class ViewTest < Test::Unit::TestCase
end end


should "limit the results if asked to" do should "limit the results if asked to" do
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', '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'}.to_json @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}


assert_equal({ assert_equal({
"total_rows" => 2, "total_rows" => 2,
Expand All @@ -357,8 +357,8 @@ class ViewTest < Test::Unit::TestCase
end end


should "count the objects with reduce" do should "count the objects with reduce" do
@db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', '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'}.to_json @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}


assert_equal({ assert_equal({
"rows" => [{ "key" => nil, "value" => 2}] "rows" => [{ "key" => nil, "value" => 2}]
Expand Down

0 comments on commit 760e778

Please sign in to comment.