rocking_chair 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,7 +40,7 @@ module RockingChair
40
40
  end
41
41
  end
42
42
 
43
- def self.put(uri, payload, headers={})
43
+ def self.put(uri, payload=nil, headers={})
44
44
  puts "PUT: #{uri.inspect}: #{payload.inspect} #{headers.inspect}" if @_rocking_chair_debug
45
45
  url, parameters = RockingChair::Server.normalize_url(uri)
46
46
  if url.match(/\A(#{URL_PARAMETER})\Z/)
@@ -59,18 +59,16 @@ module RockingChair
59
59
 
60
60
  def []=(doc_id, document, options ={})
61
61
  # options are ignored for now: batch, bulk
62
- json = nil
63
62
  begin
64
- json = JSON.parse(document, :create_additions => false)
65
- raise "is not a Hash" unless json.is_a?(Hash)
63
+ document = normalize_payload(document)
66
64
  rescue Exception => e
67
- raise RockingChair::Error.new(500, 'InvalidJSON', "the document is not a valid JSON object: #{e}")
65
+ raise RockingChair::Error.new(500, 'InvalidJSON', "the document #{doc_id} is not a valid JSON or Hash object: #{e}")
68
66
  end
69
67
 
70
68
  if exists?(doc_id)
71
- update(doc_id, json)
69
+ update(doc_id, document)
72
70
  else
73
- insert(doc_id, json)
71
+ insert(doc_id, document)
74
72
  end
75
73
  end
76
74
 
@@ -97,19 +95,20 @@ module RockingChair
97
95
  original.delete('_rev')
98
96
  end
99
97
 
100
- self.store(new_id, original.to_json)
98
+ self.store(new_id, original)
101
99
  end
102
100
 
103
101
  def bulk(documents)
104
- documents = JSON.parse(documents, :create_additions => false)
105
102
  response = []
106
- documents['docs'].each do |doc|
103
+ documents = normalize_payload(documents)
104
+ docs = documents[:docs] || documents['docs']
105
+ docs.each do |doc|
107
106
  begin
108
107
  if exists?(doc['_id']) && doc['_deleted'].to_s == 'true'
109
108
  self.delete(doc['_id'], doc['_rev'])
110
109
  state = {'id' => doc['_id'], 'rev' => doc['_rev']}
111
110
  else
112
- state = JSON.parse(self.store(doc['_id'], doc.to_json))
111
+ state = JSON.parse(self.store(doc['_id'], doc))
113
112
  end
114
113
  response << {'id' => state['id'], 'rev' => state['rev']}
115
114
  rescue RockingChair::Error => e
@@ -133,6 +132,12 @@ module RockingChair
133
132
 
134
133
  protected
135
134
 
135
+ def normalize_payload(doc)
136
+ doc = JSON.parse(doc, :create_additions => false) unless doc.is_a?(Hash)
137
+ raise "is not a Hash" unless doc.is_a?(Hash)
138
+ doc
139
+ end
140
+
136
141
  def state_tuple(_id, _rev)
137
142
  {"ok" => true, "id" => _id, "rev" => _rev }.to_json
138
143
  end
@@ -22,7 +22,7 @@ module RockingChair
22
22
  when 404
23
23
  raise RestClient::ResourceNotFound
24
24
  when 409
25
- raise HttpAbstraction::Conflict
25
+ raise RestClient::Conflict
26
26
  else
27
27
  raise "Unknown error code: #{code.inspect}"
28
28
  end
@@ -15,26 +15,27 @@ module RockingChair
15
15
  end
16
16
 
17
17
  def get(uri, headers=nil)
18
- http_adapter.get(uri, headers)
18
+ JSON.parse(http_adapter.get(uri, headers))
19
19
  end
20
20
 
21
21
  def post(uri, payload, headers=nil)
22
- http_adapter.post(uri, payload, headers)
22
+ JSON.parse(http_adapter.post(uri, payload, headers))
23
23
  end
24
24
 
25
- def put(uri, payload, headers=nil)
26
- http_adapter.put(uri, payload, headers)
25
+ def put(uri, payload=nil, headers=nil)
26
+ JSON.parse(http_adapter.put(uri, payload, headers))
27
27
  end
28
28
 
29
29
  def delete(uri, headers=nil)
30
- http_adapter.delete(uri, headers)
30
+ JSON.parse(http_adapter.delete(uri, headers))
31
31
  end
32
32
 
33
- def copy(uri, headers)
34
- http_adapter.copy(uri, headers)
33
+ def copy(uri, destination)
34
+ JSON.parse(http_adapter.copy(uri, default_headers.merge('Destination' => destination)))
35
35
  end
36
36
 
37
37
  end
38
38
  end
39
39
 
40
- HttpAbstraction.extend(RockingChair::HttpAdapter)
40
+ #::RestAPI.extend(RockingChair::HttpAdapter)
41
+ CouchRest.extend(RockingChair::HttpAdapter)
@@ -55,6 +55,7 @@ module RockingChair
55
55
 
56
56
  def self.store(db_name, doc_id, document, options)
57
57
  return respond_with_error(404) unless databases.has_key?(db_name)
58
+
58
59
  databases[db_name].store(doc_id, document, options)
59
60
  rescue RockingChair::Error => e
60
61
  e.raise_rest_client_error
@@ -236,7 +236,7 @@ class CouchRestTest < Test::Unit::TestCase
236
236
 
237
237
  should "raise 409 on a revision conflict" do
238
238
  @db.save_doc({'_id' => 'new-item', 'content' => 'here'})
239
- assert_raise(HttpAbstraction::Conflict) do
239
+ assert_raise(RestClient::Conflict) do
240
240
  @db.save_doc({'_id' => 'new-item', 'content' => 'better', '_rev' => 'wrong-revision'})
241
241
  end
242
242
 
@@ -275,7 +275,7 @@ class CouchRestTest < Test::Unit::TestCase
275
275
 
276
276
  should "fail with conflich if the rev does not matche" do
277
277
  @db.save_doc({'a' => 'b', '_id' => 'delete_me'})
278
- assert_raise(HttpAbstraction::Conflict) do
278
+ assert_raise(RestClient::Conflict) do
279
279
  @db.delete_doc({'a' => 'b', '_id' => 'delete_me', '_rev' => 'wrong-revision'})
280
280
  end
281
281
  assert_nothing_raised do
@@ -310,7 +310,7 @@ class CouchRestTest < Test::Unit::TestCase
310
310
 
311
311
  should "not copy with overwrite if the revision does not match" do
312
312
  @db.save_doc({'1' => '2', '_id' => 'destination'})
313
- assert_raise(HttpAbstraction::Conflict) do
313
+ assert_raise(RestClient::Conflict) do
314
314
  @db.copy_doc({'a' => 'b', '_id' => 'original', '_rev' => '123'}, 'destination?rev=not-here')
315
315
  end
316
316
  end
@@ -337,7 +337,7 @@ class CouchRestTest < Test::Unit::TestCase
337
337
 
338
338
  should "not copy with overwrite if the revision does not match" do
339
339
  @db.save_doc({'1' => '2', '_id' => 'destination'})
340
- assert_raise(HttpAbstraction::Conflict) do
340
+ assert_raise(RestClient::Conflict) do
341
341
  @db.copy_doc({'a' => 'b', '_id' => 'original', '_rev' => '123'}, {'1' => '2', '_id' => 'destination', '_rev' => 'missing'})
342
342
  end
343
343
  end
data/test/test_helper.rb CHANGED
@@ -6,6 +6,7 @@ require 'shoulda'
6
6
  require 'mocha'
7
7
  require 'json/ext'
8
8
  require 'active_support/inflector'
9
+ require 'couchrest_extended_document'
9
10
 
10
11
  require File.dirname(__FILE__) + "/fixtures/extended_couch_rest_fixtures"
11
12
  require File.dirname(__FILE__) + "/fixtures/simply_stored_fixtures"
data/test/view_test.rb CHANGED
@@ -16,7 +16,7 @@ class ViewTest < Test::Unit::TestCase
16
16
  'reduce' => "function(key, values){ return values.length }",
17
17
  "map" => "function(doc) { if(doc.ruby_class && doc.ruby_class == 'Instance') { emit(doc['created_at'], null); } }"
18
18
  }
19
- }}.to_json
19
+ }}
20
20
 
21
21
  @db.stubs(:rev).returns('the-rev')
22
22
 
@@ -31,7 +31,7 @@ class ViewTest < Test::Unit::TestCase
31
31
  'reduce' => "function(key, values){ return values.length }",
32
32
  "map" => "function(doc) { if(doc.ruby_class && doc.ruby_class == 'Instance') { emit(doc['created_at'], null); } }"
33
33
  }
34
- }}.to_json
34
+ }}
35
35
 
36
36
  assert_nothing_raised do
37
37
  JSON.parse(@db.view('user', 'by_firstname', {}))
@@ -57,7 +57,7 @@ class ViewTest < Test::Unit::TestCase
57
57
  'reduce' => "function(key, values){ return values.length }",
58
58
  "map" => "function(doc) {\n if(doc.ruby_class && doc.ruby_class == 'Instance') {\n emit(doc['created_at'], null);\n }\n }"
59
59
  }
60
- }}.to_json
60
+ }}
61
61
 
62
62
  @db.stubs(:rev).returns('the-rev')
63
63
  end
@@ -77,9 +77,9 @@ class ViewTest < Test::Unit::TestCase
77
77
  context "when querying by_attr_and_attr views" do
78
78
 
79
79
  should "return all keys if no key is given" do
80
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
81
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
82
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
80
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
81
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
82
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
83
83
 
84
84
  assert_equal({
85
85
  "total_rows" => 3,
@@ -101,9 +101,9 @@ class ViewTest < Test::Unit::TestCase
101
101
  end
102
102
 
103
103
  should "return all docs if no key is given and we asked to include the docs" do
104
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
105
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
106
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
104
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
105
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
106
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
107
107
 
108
108
  assert_equal({
109
109
  "total_rows" => 3,
@@ -143,9 +143,9 @@ class ViewTest < Test::Unit::TestCase
143
143
  end
144
144
 
145
145
  should "return matching elements" do
146
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
147
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
148
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
146
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
147
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
148
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
149
149
 
150
150
  assert_equal({
151
151
  "total_rows" => 2,
@@ -175,9 +175,9 @@ class ViewTest < Test::Unit::TestCase
175
175
  end
176
176
 
177
177
  should "only return items with the correct klass matcher" do
178
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'Project'}.to_json
179
- @db['user_2'] = {"firstname" => 'Alf', 'lastname' => 'Michaels'}.to_json
180
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
178
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'Project'}
179
+ @db['user_2'] = {"firstname" => 'Alf', 'lastname' => 'Michaels'}
180
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
181
181
 
182
182
  assert_equal({
183
183
  "total_rows" => 1,
@@ -197,9 +197,9 @@ class ViewTest < Test::Unit::TestCase
197
197
  end
198
198
 
199
199
  should "support multiple attributes" do
200
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
201
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
202
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
200
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
201
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
202
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
203
203
 
204
204
  assert_equal({
205
205
  "total_rows" => 1,
@@ -219,9 +219,9 @@ class ViewTest < Test::Unit::TestCase
219
219
  end
220
220
 
221
221
  should "support startkey and endkey parameters" do
222
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
223
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
224
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
222
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
223
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
224
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
225
225
 
226
226
  assert_equal(JSON.parse({
227
227
  "total_rows" => 2,
@@ -253,9 +253,9 @@ class ViewTest < Test::Unit::TestCase
253
253
  end
254
254
 
255
255
  should "support startkey/endkey combined with startkey_docid/endkey_docid parameters" do
256
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
257
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
258
- @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}.to_json
256
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
257
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
258
+ @db['user_3'] = {"firstname" => 'Alf', 'lastname' => 'Horst', 'ruby_class' => 'User'}
259
259
 
260
260
  assert_equal(JSON.parse({
261
261
  "total_rows" => 2,
@@ -280,8 +280,8 @@ class ViewTest < Test::Unit::TestCase
280
280
 
281
281
  context "belongs_to" do
282
282
  should "load parent" do
283
- @db['project_1'] = {"title" => 'alpha', 'ruby_class' => 'Project'}.to_json
284
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'project_id' => 'project_1', 'ruby_class' => 'User'}.to_json
283
+ @db['project_1'] = {"title" => 'alpha', 'ruby_class' => 'Project'}
284
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'project_id' => 'project_1', 'ruby_class' => 'User'}
285
285
 
286
286
  assert_equal({
287
287
  "total_rows" => 1,
@@ -304,9 +304,9 @@ class ViewTest < Test::Unit::TestCase
304
304
 
305
305
  context "all_documents" do
306
306
  should "load all documents of the matching class" do
307
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
308
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
309
- @db['project_1'] = {"title" => 'Alpha', 'ruby_class' => 'Project'}.to_json
307
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
308
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
309
+ @db['project_1'] = {"title" => 'Alpha', 'ruby_class' => 'Project'}
310
310
 
311
311
  assert_equal({
312
312
  "total_rows" => 2,
@@ -336,8 +336,8 @@ class ViewTest < Test::Unit::TestCase
336
336
  end
337
337
 
338
338
  should "limit the results if asked to" do
339
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
340
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
339
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
340
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
341
341
 
342
342
  assert_equal({
343
343
  "total_rows" => 2,
@@ -357,8 +357,8 @@ class ViewTest < Test::Unit::TestCase
357
357
  end
358
358
 
359
359
  should "count the objects with reduce" do
360
- @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}.to_json
361
- @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}.to_json
360
+ @db['user_1'] = {"firstname" => 'Alf', 'lastname' => 'Bert', 'ruby_class' => 'User'}
361
+ @db['user_2'] = {"firstname" => 'Carl', 'lastname' => 'Alf', 'ruby_class' => 'User'}
362
362
 
363
363
  assert_equal({
364
364
  "rows" => [{ "key" => nil, "value" => 2}]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rocking_chair
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jonathan Weiss
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-14 00:00:00 +02:00
18
+ date: 2010-09-30 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -34,6 +34,38 @@ dependencies:
34
34
  version: 0.1.12
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rest-client
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 13
46
+ segments:
47
+ - 1
48
+ - 6
49
+ - 1
50
+ version: 1.6.1
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: couchrest
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 21
62
+ segments:
63
+ - 1
64
+ - 0
65
+ - 1
66
+ version: 1.0.1
67
+ type: :runtime
68
+ version_requirements: *id003
37
69
  description: In-memory CouchDB for Couchrest and SimplyStored. Works for database and document API, by_attribute views, and for SimplyStored generated views
38
70
  email: jw@innerewut.de
39
71
  executables: []