module CouchRest
CouchDB, close to the metal¶ ↑
When included, provide the owner with an attributes hash and accessors that forward calls to it.
Provides the basic functionality of Hash without actually being a Hash using Ruby's standard Forwardable module.
Provides basic functions for controlling documents returned from the CouchDB database and provides methods to act as a wrapper around a Hash of @_attributes.
The idea is to provide the basic functionality of a Hash, just enought to support the needs of CouchRest
, but not inherit all of the functionality found in a basic Hash.
A Response is similar to Rails' HashWithIndifferentAccess as all requests will convert the keys into Symbols and be stored in the master hash as such.
Restricted set of HTTP error response we'd expect from a CouchDB server. If we don't have a specific error handler, a generic Exception
will be returned with the http_code attribute set.
Implementation based on [rest-client exception handling](github.com/rest-client/rest-client/blob/master/lib/restclient/exceptions.rb).
In general, exceptions in the `CouchRest` scope are only generated by the couchrest library, exceptions generated by other libraries will not be re-mapped.
USAGE
in your rack.rb file require this file and then:
couch = CouchRest.new
LOG_DB = couch.database!('couchrest-logger') use CouchRest::Logger
, LOG_DB
Note: to require just this middleware, if you have the gem installed do: require 'couchrest/middlewares/logger'
For log processing examples, see examples at the bottom of this file
Copyright © 2006-2009 David Heinemeier Hansson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Extracted From github.com/rails/rails/commit/971e2438d98326c994ec6d3ef8e37b7e868ed6e2
Constants
- STATUSES
- VERSION
Public Class Methods
# File lib/couchrest.rb, line 107 def database url parsed = parse url cr = CouchRest.new(parsed[:host]) cr.database(parsed[:database]) end
ensure that a database exists creates it if it isn't already there returns it after it's been created
# File lib/couchrest.rb, line 101 def database! url parsed = parse url cr = CouchRest.new(parsed[:host]) cr.database!(parsed[:database]) end
When set to true, CouchRest.get tries to decode the JSON returned from CouchDB into a Ruby object. Default: false.
# File lib/couchrest.rb, line 138 def decode_json_objects @@decode_json_objects end
# File lib/couchrest.rb, line 132 def decode_json_objects=(value) @@decode_json_objects = value end
Instantiate a new Server
object
# File lib/couchrest.rb, line 55 def new(*opts) Server.new(*opts) end
# File lib/couchrest.rb, line 113 def paramify_url url, params = {} query = params_to_query(params) query ? "#{url}?#{query}" : url end
# File lib/couchrest.rb, line 118 def params_to_query(params) if params && !params.empty? query = params.collect do |k,v| v = MultiJson.encode(v) if %w{key startkey endkey}.include?(k.to_s) "#{k}=#{CGI.escape(v.to_s)}" end.join("&") query else nil end end
# File lib/couchrest.rb, line 59 def parse url case url when /^(https?:\/\/)(.*)\/(.*)\/(.*)/ scheme = $1 host = $2 db = $3 docid = $4 when /^(https?:\/\/)(.*)\/(.*)/ scheme = $1 host = $2 db = $3 when /^(https?:\/\/)(.*)/ scheme = $1 host = $2 when /(.*)\/(.*)\/(.*)/ host = $1 db = $2 docid = $3 when /(.*)\/(.*)/ host = $1 db = $2 else db = url end db = nil if db && db.empty? { :host => (scheme || "http://") + (host || "127.0.0.1:5984"), :database => db, :doc => docid } end
Set default proxy to use in connections
# File lib/couchrest.rb, line 94 def proxy url CouchRest::Connection.proxy = url end