Class: CouchClient::ConnectionHandler
- Inherits:
-
Object
- Object
- CouchClient::ConnectionHandler
- Defined in:
- lib/couch-client/connection_handler.rb
Overview
ConnectionHandler creates properly formed URIs and paths, while also specifying sensible defaults for CouchDB. Once initialized, parameters can be wrote and read using getter and setter syntax.
Instance Attribute Summary collapse
-
#database ⇒ Object
Returns the value of attribute database.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#initialize ⇒ ConnectionHandler
constructor
ConnectionHandler is constructed without any parameters, and with defaults for scheme, host and port.
- #inspect ⇒ Object
-
#path(path_obj = nil, query_obj = nil) ⇒ Object
Creates a properly formed path that can be used by a HTTP library.
-
#uri(path_obj = nil, query_obj = nil) ⇒ Object
Creates a properly formed URI that can be used by a HTTP library.
Constructor Details
#initialize ⇒ ConnectionHandler
ConnectionHandler is constructed without any parameters, and with defaults for scheme, host and port. Other settings are set via the accessors above.
17 18 19 20 21 |
# File 'lib/couch-client/connection_handler.rb', line 17 def initialize @scheme = "http" @host = "localhost" @port = 5984 end |
Instance Attribute Details
#database ⇒ Object
Returns the value of attribute database.
13 14 15 |
# File 'lib/couch-client/connection_handler.rb', line 13 def database @database end |
#host ⇒ Object
Returns the value of attribute host.
12 13 14 |
# File 'lib/couch-client/connection_handler.rb', line 12 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
12 13 14 |
# File 'lib/couch-client/connection_handler.rb', line 12 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
12 13 14 |
# File 'lib/couch-client/connection_handler.rb', line 12 def port @port end |
#scheme ⇒ Object
Returns the value of attribute scheme.
12 13 14 |
# File 'lib/couch-client/connection_handler.rb', line 12 def scheme @scheme end |
#username ⇒ Object
Returns the value of attribute username.
12 13 14 |
# File 'lib/couch-client/connection_handler.rb', line 12 def username @username end |
Instance Method Details
#inspect ⇒ Object
71 72 73 |
# File 'lib/couch-client/connection_handler.rb', line 71 def inspect "#<#{self.class}>" end |
#path(path_obj = nil, query_obj = nil) ⇒ Object
Creates a properly formed path that can be used by a HTTP library. ‘path_obj` can be an Array or NilClass, `query_obj` can be Hash or NilClass.
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_handler.rb', line 41 def path(path_obj = nil, query_obj = nil) path_obj ||= [] query_obj ||= {} path_str = if path_obj.is_a?(Array) # If an Array, stringify and escape (unless it is a design document) each object and join with a "/" ([@database] + path_obj).map{|p| p.to_s.match(/_design\//) ? p.to_s : CGI.escape(p.to_s)}.join("/") else # Else, raise an error raise InvalidPathObject.new("path must be of type 'Array' not of type '#{path_obj.class}'") end # key, startkey and endkey must be JSON encoded to be valid. ["key", :key, "startkey", :startkey, "endkey", :endkey].each do |key| query_obj[key] &&= query_obj[key].to_json end query_str = if query_obj.is_a?(Hash) # If a Hash, stringify and escape each object, join each key/value with a "=" and each pair with a "&" query_obj.to_a.map{|q| q.map{|r| CGI.escape(r.to_s)}.join("=")}.join("&") else # Else, raise an error raise InvalidQueryObject.new("path must be of type 'Hash' or 'NilClass' not of type '#{query_obj.class}'") end str = "/" + path_str str += "?" + query_str unless query_str.empty? str end |
#uri(path_obj = nil, query_obj = nil) ⇒ Object
Creates a properly formed URI that can be used by a HTTP library.
33 34 35 36 37 |
# File 'lib/couch-client/connection_handler.rb', line 33 def uri(path_obj = nil, query_obj = nil) str = "#{@scheme}://#{@host}:#{@port}" str += path(path_obj, query_obj) str end |