Class: ShyCouch::CouchDatabase::CouchServerConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/ShyCouch.rb

Instance Method Summary collapse

Constructor Details

#initialize(args, options = nil) ⇒ CouchServerConnection

host, port, user, password, options = nil)



240
241
242
243
244
245
246
247
# File 'lib/ShyCouch.rb', line 240

def initialize(args, options=nil)#host, port, user, password, options = nil)
	@host = args["host"]
	@port = args["port"]
	@user = args["user"]
	@password = args["password"]
	@db_name = args["database"]
	@options = options
end

Instance Method Details

#all_docs_from_database(db_name) ⇒ Object



312
313
314
# File 'lib/ShyCouch.rb', line 312

def all_docs_from_database(db_name)
  pull_all_doc_ids(db_name).map { |id| Data::CouchDocument.new(req(:get,"/#{db_name}/#{id}")) }
end

#couch_req_with_auth(kind, uri = nil, data = nil) ⇒ Object



290
291
292
293
294
295
296
297
# File 'lib/ShyCouch.rb', line 290

def couch_req_with_auth(kind, uri = nil, data = nil)
uri ? uri = req_host + uri : uri = req_host
if kind == :get or kind == :delete
  RestClient.method(kind).call(uri, :content_type => :json, :user => @user, :password => @password)  
else
  RestClient.method(kind).call(uri, data, :content_type => :json, :user => @user, :password => @password)  
end
end

#couch_req_without_auth(kind, uri, data = nil) ⇒ Object



298
299
300
301
302
303
304
305
306
# File 'lib/ShyCouch.rb', line 298

def couch_req_without_auth(kind, uri, data = nil)
uri ? uri = req_host + uri : uri = req_host
if kind == :get or kind == :delete
  RestClient.method(kind).call(uri, :content_type => :json)  
else
  RestClient.method(kind).call(uri, data,:content_type => :json)  
end

end

#create_db(db_name) ⇒ Object



342
343
344
# File 'lib/ShyCouch.rb', line 342

def create_db(db_name)
	req(:put, "/#{db_name}/")
end

#delete_db(db_name) ⇒ Object



345
346
347
# File 'lib/ShyCouch.rb', line 345

def delete_db(db_name)
	req(:delete,"/#{db_name}/")
end

#delete_document(db_name, document) ⇒ Object

Raises:

  • (TypeError)


337
338
339
340
# File 'lib/ShyCouch.rb', line 337

def delete_document(db_name, document)
	raise TypeError unless document.kind_of?(ShyCouch::Data::CouchDocument)
	return req(:delete, "/#{db_name}/#{document._id}?rev=#{document._rev}")
end

#getDocumentById(db_name, id) ⇒ Object



316
317
318
# File 'lib/ShyCouch.rb', line 316

def getDocumentById(db_name, id)
	document = Data::CouchDocument.new(:data => req(:get,"/#{db_name}/#{id}"))
end

#has_database?(db_name) ⇒ Boolean

Returns:

  • (Boolean)


259
260
261
262
263
264
# File 'lib/ShyCouch.rb', line 259

def has_database?(db_name)
  req(:get, "/#{db_name}/")
	true
rescue RestClient::ResourceNotFound
	false
end

#pull_all_doc_ids(db_name) ⇒ Object

TODO - this is screwed



308
309
310
# File 'lib/ShyCouch.rb', line 308

def pull_all_doc_ids(db_name)
	req(:get,"/#{db_name}/_all_docs")["rows"].map { |doc| doc["id"] }
end

#pull_document(db_name, document) ⇒ Object



319
320
321
# File 'lib/ShyCouch.rb', line 319

def pull_document(db_name, document)
	document = Data::CouchDocument.new(:data => req(:get,"/#{db_name}/#{document._id}"))  			
end

#push_document(db_name, document) ⇒ Object

Haven’t decided whether PUT/POST should take a CouchDocument or a JSON string.

Raises:

  • (TypeError)


325
326
327
328
329
330
331
332
333
334
335
# File 'lib/ShyCouch.rb', line 325

def push_document(db_name, document)
raise TypeError unless document.kind_of?(ShyCouch::Data::CouchDocument)
raise JSON::GeneratorError unless document.valid?
if document["_rev"]
return req(:put, "/#{db_name}/#{document._id}?rev=#{document._rev}", document.to_json)
elsif document["_id"]
  return req(:put, "/#{db_name}/#{document._id}", document.to_json)
else
  return req(:post, "/#{db_name}/", document.to_json)
end
end

#req(kind, uri, data = nil) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ShyCouch.rb', line 266

def req(kind, uri, data = nil)
	# TODO - remove this
raise TypeError unless [:get,:delete,:put,:post].include?(kind) # only support these 4 request methods currently
if @user and @password
	res = couch_req_with_auth kind, uri, data
else
	res = couch_req_without_auth kind, uri, data
end
return JSON.parse(res)
rescue RestClient::Conflict => e
# attempting to update without rev will throw this error

# sometimes it's because a server booted and tried to push design documents that existed:
if JSON.parse(data)["_id"].include? "_design"
  # throw a different error
  raise ShyCouch::DesignConflict, "Design document update refused, or other resource conflict."
else
  raise e
end

end

#req_hostObject



287
288
289
# File 'lib/ShyCouch.rb', line 287

def req_host
"http://#{(@user + ':' + @password + '@') if @user and @password}#{@host}:#{@port}"
end

#responds?Boolean

Returns:

  • (Boolean)


249
250
251
252
253
254
255
256
257
# File 'lib/ShyCouch.rb', line 249

def responds?
	if req(:get, "/")['couchdb'] == "Welcome"
		true
	else
		false
	end
rescue Errno::ECONNREFUSED
	false
end