Class: ShyCouch::CouchDatabase::CouchServerConnection
- Inherits:
-
Object
- Object
- ShyCouch::CouchDatabase::CouchServerConnection
- Defined in:
- lib/ShyCouch.rb
Instance Method Summary collapse
- #all_docs_from_database(db_name) ⇒ Object
- #couch_req_with_auth(kind, uri = nil, data = nil) ⇒ Object
- #couch_req_without_auth(kind, uri, data = nil) ⇒ Object
- #create_db(db_name) ⇒ Object
- #delete_db(db_name) ⇒ Object
- #delete_document(db_name, document) ⇒ Object
- #get_document_by_id(db_name, id) ⇒ Object
- #has_database?(db_name) ⇒ Boolean
-
#initialize(args, options = nil) ⇒ CouchServerConnection
constructor
host, port, user, password, options = nil).
-
#pull_all_doc_ids(db_name) ⇒ Object
TODO - this is screwed.
- #pull_document(db_name, document) ⇒ Object
-
#push_document(db_name, document) ⇒ Object
Haven’t decided whether PUT/POST should take a CouchDocument or a JSON string.
- #req(kind, uri, data = nil) ⇒ Object
- #req_host ⇒ Object
- #responds? ⇒ Boolean
Constructor Details
#initialize(args, options = nil) ⇒ CouchServerConnection
host, port, user, password, options = nil)
236 237 238 239 240 241 242 243 |
# File 'lib/ShyCouch.rb', line 236 def initialize(args, =nil)#host, port, user, password, options = nil) @host = args["host"] @port = args["port"] @user = args["user"] @password = args["password"] @db_name = args["database"] @options = end |
Instance Method Details
#all_docs_from_database(db_name) ⇒ Object
307 308 309 |
# File 'lib/ShyCouch.rb', line 307 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
285 286 287 288 289 290 291 292 |
# File 'lib/ShyCouch.rb', line 285 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
293 294 295 296 297 298 299 300 301 |
# File 'lib/ShyCouch.rb', line 293 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
337 338 339 |
# File 'lib/ShyCouch.rb', line 337 def create_db(db_name) req(:put, "/#{db_name}/") end |
#delete_db(db_name) ⇒ Object
340 341 342 |
# File 'lib/ShyCouch.rb', line 340 def delete_db(db_name) req(:delete,"/#{db_name}/") end |
#delete_document(db_name, document) ⇒ Object
332 333 334 335 |
# File 'lib/ShyCouch.rb', line 332 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 |
#get_document_by_id(db_name, id) ⇒ Object
311 312 313 |
# File 'lib/ShyCouch.rb', line 311 def get_document_by_id(db_name, id) document = Data::CouchDocument.new(:data => req(:get,"/#{db_name}/#{id}")) end |
#has_database?(db_name) ⇒ Boolean
255 256 257 258 259 260 |
# File 'lib/ShyCouch.rb', line 255 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
303 304 305 |
# File 'lib/ShyCouch.rb', line 303 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
314 315 316 |
# File 'lib/ShyCouch.rb', line 314 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.
320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/ShyCouch.rb', line 320 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
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/ShyCouch.rb', line 262 def req(kind, uri, data = nil) 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_host ⇒ Object
282 283 284 |
# File 'lib/ShyCouch.rb', line 282 def req_host "http://#{(@user + ':' + @password + '@') if @user and @password}#{@host}:#{@port}" end |
#responds? ⇒ Boolean
245 246 247 248 249 250 251 252 253 |
# File 'lib/ShyCouch.rb', line 245 def responds? if req(:get, "/")['couchdb'] == "Welcome" true else false end rescue Errno::ECONNREFUSED false end |