Class: CouchClient::Connection
- Inherits:
-
Object
- Object
- CouchClient::Connection
- Defined in:
- lib/couch-client/connection.rb
Overview
Connection is the high-level interface used to interact with the CouchDB Server.
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#hookup ⇒ Object
readonly
Returns the value of attribute hookup.
Instance Method Summary collapse
-
#[](id, options = {}) ⇒ Object
Fetches documents from the CouchDB server.
-
#all_design_docs(options = {}) ⇒ Object
Returns a list of all _design documents.
-
#all_docs(options = {}) ⇒ Object
Acts as the interface to CouchDB’s
_all_docsmap view. -
#build(body = {}) ⇒ Object
The interface used to construct new CouchDB documents.
-
#design(id) ⇒ Object
Constructs a new design factory that manages
views,shows,listsandfulltextsearches. -
#initialize(args = {}) ⇒ Connection
constructor
Connection is constructed with a Hash or with a block specifying connection parameters.
- #inspect ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Connection
Connection is constructed with a Hash or with a block specifying connection parameters. An error will be raised if a database is not specified.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/couch-client/connection.rb', line 12 def initialize(args = {}) handler = ConnectionHandler.new # Set ConnectionHandler settings via a block if block_given? yield(handler) end # Set remaining ConnectionHandler settings via a Hash args.each_pair do |key, value| handler.send("#{key}=", value) end # Ensure a database is provided unless handler.database raise DatabaseNotGiven.new("specify a database to connect to") end # `@hookup` is used as the HTTP interface and `@database` is a namespace for all # database specific commands such as creation, deletion, compaction and replication. @hookup = Hookup.new(handler) @database = Database.new(self) end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
8 9 10 |
# File 'lib/couch-client/connection.rb', line 8 def database @database end |
#hookup ⇒ Object (readonly)
Returns the value of attribute hookup.
8 9 10 |
# File 'lib/couch-client/connection.rb', line 8 def hookup @hookup end |
Instance Method Details
#[](id, options = {}) ⇒ Object
Fetches documents from the CouchDB server. Although [] makes get requests and therefore could fetch design views and more, anything received that is not a valid document will raise an error. As such, fetching designs can only be done through the design method.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/couch-client/connection.rb', line 39 def [](id, = {}) code, body = @hookup.get([id], ) case code # If something was found when 200 # And that something is a document if body["_id"] && body["_rev"] # Make a new document object Document.new(code, body, self) else # Else raise an error as `[]` should only return document objects raise DocumentNotValid.new("the id '#{id}' does not correspond to a document") end # If nothing was found when 404 case body["reason"] # Because the document was deleted when "deleted" # Tell the user it was deleted raise DocumentNotFound.new("the document with id '#{id}' has been deleted") else # Else tell the user it was never there to begin with raise DocumentNotFound.new("a document could not be found with id '#{id}'") end # If something else happened else # Raise an error raise Error.new("code: #{code}, error: #{body["error"]}, reason: #{body["reason"]}") end end |
#all_design_docs(options = {}) ⇒ Object
Returns a list of all _design documents.
83 84 85 |
# File 'lib/couch-client/connection.rb', line 83 def all_design_docs( = {}) all_docs({"startkey" => "_design/", "endkey" => "_design0"}.merge()) end |
#all_docs(options = {}) ⇒ Object
Acts as the interface to CouchDB’s _all_docs map view.
77 78 79 80 |
# File 'lib/couch-client/connection.rb', line 77 def all_docs( = {}) # Create a new Collection with the response code, body and connection. Collection.new(*@hookup.get(["_all_docs"], ), self) end |
#build(body = {}) ⇒ Object
The interface used to construct new CouchDB documents. Once constructed, these documents can be saved, updated, validated and deleted.
89 90 91 |
# File 'lib/couch-client/connection.rb', line 89 def build(body = {}) Document.new(nil, body, self) end |
#design(id) ⇒ Object
Constructs a new design factory that manages views, shows, lists and fulltext searches.
72 73 74 |
# File 'lib/couch-client/connection.rb', line 72 def design(id) Design.new(id, self) end |
#inspect ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/couch-client/connection.rb', line 93 def inspect head = "#<#{self.class}: " body = [] body << "username: #{@hookup.handler.username}" if @hookup.handler.username body << "password: #{@hookup.handler.password}" if @hookup.handler.password body << "uri: #{@hookup.handler.uri}" tail = ">" head + body.join(", ") + tail end |