akdubya-cushion 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cushion.rb +2 -2
- data/lib/cushion/database.rb +68 -26
- data/lib/cushion/server.rb +11 -9
- data/spec/database_spec.rb +6 -8
- data/spec/server_spec.rb +7 -7
- metadata +1 -1
data/lib/cushion.rb
CHANGED
@@ -2,7 +2,7 @@ require 'json'
|
|
2
2
|
require 'restclient'
|
3
3
|
require 'uri'
|
4
4
|
|
5
|
-
dir = File.dirname(__FILE__) + '/cushion/'
|
5
|
+
dir = File.expand_path(File.dirname(__FILE__)) + '/cushion/'
|
6
6
|
|
7
7
|
require dir + 'core_ext'
|
8
8
|
require dir + 'server'
|
@@ -13,7 +13,7 @@ require dir + 'design'
|
|
13
13
|
DEFAULT_COUCH_HOST = "http://127.0.0.1:5984"
|
14
14
|
|
15
15
|
module Cushion
|
16
|
-
VERSION = '0.5.
|
16
|
+
VERSION = '0.5.2'
|
17
17
|
|
18
18
|
class << self
|
19
19
|
|
data/lib/cushion/database.rb
CHANGED
@@ -19,7 +19,7 @@ module Cushion
|
|
19
19
|
|
20
20
|
# Compacts this database.
|
21
21
|
def compact(headers = {})
|
22
|
-
|
22
|
+
post("_compact", nil, headers)
|
23
23
|
end
|
24
24
|
|
25
25
|
# Creates this database on the server.
|
@@ -28,7 +28,7 @@ module Cushion
|
|
28
28
|
end
|
29
29
|
|
30
30
|
# Deletes this database from the server.
|
31
|
-
def
|
31
|
+
def drop(headers = {})
|
32
32
|
server.delete(@name, headers)
|
33
33
|
end
|
34
34
|
|
@@ -39,11 +39,11 @@ module Cushion
|
|
39
39
|
def all_docs(params = {})
|
40
40
|
keys = params.delete(:keys)
|
41
41
|
opts = params.delete(:headers) || {}
|
42
|
-
path = Cushion.paramify_url("
|
42
|
+
path = Cushion.paramify_url("_all_docs", params)
|
43
43
|
if keys
|
44
|
-
|
44
|
+
post(path, {:keys => keys}, opts)
|
45
45
|
else
|
46
|
-
|
46
|
+
get(path, opts)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -52,8 +52,8 @@ module Cushion
|
|
52
52
|
def open_doc(id, params = {})
|
53
53
|
opts = params.delete(:headers) || {}
|
54
54
|
slug = Cushion.escape_docid(id)
|
55
|
-
path = Cushion.paramify_url("#{
|
56
|
-
|
55
|
+
path = Cushion.paramify_url("#{slug}", params)
|
56
|
+
get(path, opts)
|
57
57
|
end
|
58
58
|
|
59
59
|
# Retrieves a single document by +id+ and returns a <tt>Cushion::Document</tt>
|
@@ -79,17 +79,17 @@ module Cushion
|
|
79
79
|
end
|
80
80
|
if params['_id']
|
81
81
|
slug = Cushion.escape_docid(params['_id'])
|
82
|
-
|
82
|
+
put("#{slug}", params, opts)
|
83
83
|
else
|
84
84
|
slug = params['_id'] = server.next_uuid
|
85
|
-
|
85
|
+
put("#{slug}", params, opts)
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
89
|
# Deletes a single document by +id+ and +rev+.
|
90
90
|
def delete_doc(id, rev, headers = {})
|
91
91
|
slug = Cushion.escape_docid(id)
|
92
|
-
|
92
|
+
delete("#{slug}?rev=#{rev}", headers)
|
93
93
|
end
|
94
94
|
|
95
95
|
# Creates, updates and deletes multiple documents in this database according
|
@@ -130,7 +130,7 @@ module Cushion
|
|
130
130
|
doc['_id'] = nextid if nextid
|
131
131
|
end
|
132
132
|
end
|
133
|
-
|
133
|
+
post("_bulk_docs", {:docs => docs}, headers)
|
134
134
|
end
|
135
135
|
|
136
136
|
# A convenience method for deleting multiple documents at once. Just pass in
|
@@ -155,7 +155,7 @@ module Cushion
|
|
155
155
|
# db.purge(doc_revs)
|
156
156
|
#
|
157
157
|
def purge(doc_revs, headers = {})
|
158
|
-
|
158
|
+
post("_purge", doc_revs, headers)
|
159
159
|
end
|
160
160
|
|
161
161
|
# Copies the document identified by +source_id+ to a new or existing document
|
@@ -171,7 +171,7 @@ module Cushion
|
|
171
171
|
else
|
172
172
|
dest_id
|
173
173
|
end
|
174
|
-
|
174
|
+
copy("#{slug}", destination, headers)
|
175
175
|
end
|
176
176
|
|
177
177
|
# Moves the document identified by +source_id+ and +src_rev+ to a new or
|
@@ -188,7 +188,7 @@ module Cushion
|
|
188
188
|
else
|
189
189
|
dest_id
|
190
190
|
end
|
191
|
-
|
191
|
+
move("#{slug}?rev=#{src_rev}", destination, headers)
|
192
192
|
end
|
193
193
|
|
194
194
|
# Supply +funcs+ to create a temporary view and run a query against it. Set
|
@@ -207,8 +207,8 @@ module Cushion
|
|
207
207
|
keys = params.delete(:keys)
|
208
208
|
headers = params.delete(:headers) || {}
|
209
209
|
funcs = funcs.merge({:keys => keys}) if keys
|
210
|
-
path = Cushion.paramify_url("
|
211
|
-
|
210
|
+
path = Cushion.paramify_url("_temp_view", params)
|
211
|
+
post(path, funcs, headers)
|
212
212
|
end
|
213
213
|
|
214
214
|
# Query a saved view identified by +view+. Set the +keys+ param to perform a
|
@@ -221,11 +221,11 @@ module Cushion
|
|
221
221
|
def view(view, params = {})
|
222
222
|
keys = params.delete(:keys)
|
223
223
|
headers = params.delete(:headers) || {}
|
224
|
-
path = Cushion.paramify_url("
|
224
|
+
path = Cushion.paramify_url("_view/#{view}", params)
|
225
225
|
if keys
|
226
|
-
|
226
|
+
post(path, {:keys => keys}, headers)
|
227
227
|
else
|
228
|
-
|
228
|
+
get(path, headers)
|
229
229
|
end
|
230
230
|
end
|
231
231
|
|
@@ -237,8 +237,8 @@ module Cushion
|
|
237
237
|
defaults = { :accept => "text/html;text/plain;*/*" }
|
238
238
|
headers = params.delete(:headers) || {}
|
239
239
|
slug = Cushion.escape_docid(id)
|
240
|
-
path = Cushion.paramify_url("
|
241
|
-
|
240
|
+
path = Cushion.paramify_url("_show/#{design}/#{show_template}/#{slug}", params)
|
241
|
+
get(path, defaults.merge(headers))
|
242
242
|
end
|
243
243
|
|
244
244
|
# Query the list template identified by +design+, +list_template+ and +view+.
|
@@ -248,8 +248,8 @@ module Cushion
|
|
248
248
|
def list(design, list_template, view, params = {})
|
249
249
|
defaults = { :accept => "text/html;text/plain;*/*" }
|
250
250
|
headers = params.delete(:headers) || {}
|
251
|
-
path = Cushion.paramify_url("
|
252
|
-
|
251
|
+
path = Cushion.paramify_url("_list/#{design}/#{list_template}/#{view}", params)
|
252
|
+
get(path, defaults.merge(headers))
|
253
253
|
end
|
254
254
|
|
255
255
|
# Issues a request to the CouchDB external server identified by +process+.
|
@@ -258,7 +258,7 @@ module Cushion
|
|
258
258
|
#
|
259
259
|
def external(verb, process_path, params = {})
|
260
260
|
path = Cushion.paramify_url("_#{process_path}", params[:query])
|
261
|
-
|
261
|
+
request(verb, path, :body => params[:body], :headers => params[:headers])
|
262
262
|
end
|
263
263
|
|
264
264
|
# Uses open-uri to open the attachment identified by +id+ and +filename+.
|
@@ -280,7 +280,7 @@ module Cushion
|
|
280
280
|
defaults = { :content_type => "application/octet-stream" }
|
281
281
|
slug = Cushion.escape_docid(id)
|
282
282
|
fname = CGI.escape(filename)
|
283
|
-
|
283
|
+
put("#{slug}/#{fname}?rev=#{rev}", data, defaults.merge(headers))
|
284
284
|
end
|
285
285
|
|
286
286
|
# Deletes an attachment under the document identified by +id+ and +rev+ with
|
@@ -289,7 +289,7 @@ module Cushion
|
|
289
289
|
def delete_attachment(id, rev, filename, headers = {})
|
290
290
|
slug = Cushion.escape_docid(id)
|
291
291
|
fname = CGI.escape(filename)
|
292
|
-
|
292
|
+
delete("#{slug}/#{fname}?rev=#{rev}", headers)
|
293
293
|
end
|
294
294
|
|
295
295
|
# Renames an attachment under the document identified by +id+ and +rev+.
|
@@ -306,5 +306,47 @@ module Cushion
|
|
306
306
|
end
|
307
307
|
end
|
308
308
|
|
309
|
+
# Issues a HEAD request to this database. See Cushion::Server#head.
|
310
|
+
def head(path, headers = {})
|
311
|
+
server.head("#{@name}/#{path}", headers)
|
312
|
+
end
|
313
|
+
|
314
|
+
# Issues a GET request to this database. See Cushion::Server#get.
|
315
|
+
def get(path, headers = {})
|
316
|
+
server.get("#{@name}/#{path}", headers)
|
317
|
+
end
|
318
|
+
|
319
|
+
# Issues a POST request to this database. See Cushion::Server#post.
|
320
|
+
def post(path, body, headers = {})
|
321
|
+
server.post("#{@name}/#{path}", body, headers)
|
322
|
+
end
|
323
|
+
|
324
|
+
# Issues a PUT request to this database. See Cushion::Server#put.
|
325
|
+
def put(path, body, headers = {})
|
326
|
+
server.put("#{@name}/#{path}", body, headers)
|
327
|
+
end
|
328
|
+
|
329
|
+
# Issues a DELETE request to this database. See Cushion::Server#delete.
|
330
|
+
def delete(path, headers = {})
|
331
|
+
server.delete("#{@name}/#{path}", headers)
|
332
|
+
end
|
333
|
+
|
334
|
+
# Issues a COPY request to this database. See Cushion::Server#copy.
|
335
|
+
def copy(source, destination, headers = {})
|
336
|
+
server.copy("#{@name}/#{source}", destination, headers)
|
337
|
+
end
|
338
|
+
|
339
|
+
# Issues a MOVE request to this database. See Cushion::Server#move.
|
340
|
+
def move(source, destination, headers = {})
|
341
|
+
server.move("#{@name}/#{source}", destination, headers)
|
342
|
+
end
|
343
|
+
|
344
|
+
# Issues a generic request to this database. See Cushion::Server#request.
|
345
|
+
def request(verb, path, params)
|
346
|
+
server.request(verb, "#{@name}/#{path}", params)
|
347
|
+
end
|
348
|
+
|
349
|
+
## EXPERIMENTAL ##
|
350
|
+
|
309
351
|
end
|
310
352
|
end
|
data/lib/cushion/server.rb
CHANGED
@@ -58,8 +58,8 @@ module Cushion
|
|
58
58
|
end
|
59
59
|
|
60
60
|
# Deletes the database identified by +name+ from this server.
|
61
|
-
def
|
62
|
-
db(name).
|
61
|
+
def drop(name, headers = {})
|
62
|
+
db(name).drop(headers)
|
63
63
|
end
|
64
64
|
|
65
65
|
# Deletes and re-creates the database identified by +name+.
|
@@ -67,7 +67,7 @@ module Cushion
|
|
67
67
|
def recreate(name)
|
68
68
|
rdb = db(name)
|
69
69
|
begin
|
70
|
-
rdb.
|
70
|
+
rdb.drop
|
71
71
|
rescue RestClient::ResourceNotFound
|
72
72
|
nil
|
73
73
|
end
|
@@ -105,7 +105,7 @@ module Cushion
|
|
105
105
|
end
|
106
106
|
|
107
107
|
# Issues a GET request to the CouchDB server. Returns a parsed response body
|
108
|
-
# if the +accept+
|
108
|
+
# if the +accept+ option is set to 'application/json' (the default), otherwise
|
109
109
|
# returns the raw RestClient response body.
|
110
110
|
def get(path, headers = {})
|
111
111
|
defaults = { :accept => "application/json" }
|
@@ -114,7 +114,7 @@ module Cushion
|
|
114
114
|
|
115
115
|
# Issues a POST request to the CouchDB server. Parses the request body if
|
116
116
|
# the +content_type+ option is set to 'application/json' (the default).
|
117
|
-
# Returns a parsed response body if the +accept+
|
117
|
+
# Returns a parsed response body if the +accept+ option is set to
|
118
118
|
# 'application/json' (the default), otherwise returns the raw
|
119
119
|
# RestClient response body.
|
120
120
|
def post(path, body, headers = {})
|
@@ -135,7 +135,7 @@ module Cushion
|
|
135
135
|
end
|
136
136
|
|
137
137
|
# Issues a DELETE request to the CouchDB server. Returns a parsed response body
|
138
|
-
# if the +accept+
|
138
|
+
# if the +accept+ option is set to 'application/json' (the default), otherwise
|
139
139
|
# returns the raw RestClient response body.
|
140
140
|
def delete(path, headers = {})
|
141
141
|
defaults = { :accept => "application/json" }
|
@@ -143,7 +143,7 @@ module Cushion
|
|
143
143
|
end
|
144
144
|
|
145
145
|
# Issues a COPY request to the CouchDB server, copying a document from +source+
|
146
|
-
# to +destination+. Returns a parsed response body if the +accept+
|
146
|
+
# to +destination+. Returns a parsed response body if the +accept+ option
|
147
147
|
# is set to 'application/json' (the default), otherwise returns the raw
|
148
148
|
# RestClient response body.
|
149
149
|
def copy(source, destination, headers = {})
|
@@ -152,7 +152,7 @@ module Cushion
|
|
152
152
|
end
|
153
153
|
|
154
154
|
# Issues a MOVE request to the CouchDB server, moving a document from +source+
|
155
|
-
# to +destination+. Returns a parsed response body if the +accept+
|
155
|
+
# to +destination+. Returns a parsed response body if the +accept+ option
|
156
156
|
# is set to 'application/json' (the default), otherwise returns the raw
|
157
157
|
# RestClient response body.
|
158
158
|
def move(source, destination, headers = {})
|
@@ -160,7 +160,9 @@ module Cushion
|
|
160
160
|
parse_response(RestClient.move("#{@uri}/#{source}", defaults.merge(headers)))
|
161
161
|
end
|
162
162
|
|
163
|
-
|
163
|
+
# Issues a generic request to the server +path+ with the method set to +verb+.
|
164
|
+
# Accepts +body+ and +headers+ options.
|
165
|
+
def request(verb, path, params = {})
|
164
166
|
RestClient::Request.execute(
|
165
167
|
:method => verb,
|
166
168
|
:url => "#{@uri}/#{path}",
|
data/spec/database_spec.rb
CHANGED
@@ -7,7 +7,7 @@ describe "Cushion::Database" do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
after do
|
10
|
-
@db.
|
10
|
+
@db.drop rescue nil
|
11
11
|
end
|
12
12
|
|
13
13
|
describe "Operations" do
|
@@ -18,10 +18,10 @@ describe "Cushion::Database" do
|
|
18
18
|
lambda { Cushion::Database.new( :foo, "mydb" ) }.should.raise ArgumentError
|
19
19
|
end
|
20
20
|
|
21
|
-
it "should create and
|
21
|
+
it "should create and drop a database" do
|
22
22
|
newdb = Cushion.db('http://127.0.0.1:5984/foo_test')
|
23
23
|
newdb.create["ok"].should.be.true
|
24
|
-
newdb.
|
24
|
+
newdb.drop["ok"].should.be.true
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should retrieve database info" do
|
@@ -31,14 +31,12 @@ describe "Cushion::Database" do
|
|
31
31
|
it "should compact a database" do
|
32
32
|
@db.save_doc("foo" => "bar")
|
33
33
|
@db.compact["ok"].should.be.true
|
34
|
-
# Finish compaction before moving on
|
35
|
-
while @db.server.active_tasks != [] do
|
36
|
-
sleep(0.5)
|
37
|
-
end
|
38
34
|
end
|
39
35
|
|
40
36
|
it "should pass requests to external handlers" do
|
41
|
-
|
37
|
+
# This is kind of lame, but installing an external handler to run a "real"
|
38
|
+
# external test seems like overkill.
|
39
|
+
lambda { @db.external(:get, "all_docs") }.should.not.raise
|
42
40
|
end
|
43
41
|
|
44
42
|
end
|
data/spec/server_spec.rb
CHANGED
@@ -41,21 +41,21 @@ describe "Cushion::Server" do
|
|
41
41
|
|
42
42
|
describe "Databases" do
|
43
43
|
|
44
|
-
it "should create and
|
44
|
+
it "should create and drop databases" do
|
45
45
|
@server.create(:server_test)["ok"].should.be.true
|
46
|
-
@server.
|
46
|
+
@server.drop(:server_test)["ok"].should.be.true
|
47
47
|
end
|
48
48
|
|
49
49
|
it "should retrieve a list of all databases on the server" do
|
50
50
|
@server.create(:server_list_test)
|
51
51
|
@server.all_dbs.should.include "server_list_test"
|
52
|
-
@server.
|
52
|
+
@server.drop(:server_list_test)
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should recreate a database" do
|
56
56
|
@server.recreate(:server_recreate_test).should.be.kind_of Cushion::Database
|
57
57
|
@server.all_dbs.should.include "server_recreate_test"
|
58
|
-
@server.
|
58
|
+
@server.drop(:server_recreate_test)
|
59
59
|
end
|
60
60
|
|
61
61
|
it "should instantiate a Cushion::Database with server[:dbname]" do
|
@@ -69,7 +69,7 @@ describe "Cushion::Server" do
|
|
69
69
|
it "should create a database if it does not already exist with db!('dbname')" do
|
70
70
|
@server.db!(:server_autocreate_test).should.be.kind_of Cushion::Database
|
71
71
|
@server.all_dbs.should.include "server_autocreate_test"
|
72
|
-
@server.
|
72
|
+
@server.drop(:server_autocreate_test)
|
73
73
|
end
|
74
74
|
|
75
75
|
end
|
@@ -82,8 +82,8 @@ describe "Cushion::Server" do
|
|
82
82
|
end
|
83
83
|
|
84
84
|
after do
|
85
|
-
@server.
|
86
|
-
@server.
|
85
|
+
@server.drop(:server_replication_test1)
|
86
|
+
@server.drop(:server_replication_test2)
|
87
87
|
end
|
88
88
|
|
89
89
|
it "should replicate databases" do
|