2008-09-12 06:39:48 +02:00
# Copyright 2008 J. Chris Anderson
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2008-09-12 06:31:59 +02:00
2009-05-14 08:20:05 +02:00
require 'rubygems'
begin
require 'json'
rescue LoadError
2009-05-21 01:33:19 +02:00
raise " You need install and require your own json compatible library since couchrest rest couldn't load the json/json_pure gem " unless Kernel . const_defined? ( " JSON " )
2009-05-14 08:20:05 +02:00
end
2008-09-12 06:00:44 +02:00
require 'rest_client'
2008-09-12 06:16:01 +02:00
$: . unshift File . dirname ( __FILE__ ) unless
$: . include? ( File . dirname ( __FILE__ ) ) ||
$: . include? ( File . expand_path ( File . dirname ( __FILE__ ) ) )
2009-05-27 03:27:49 +02:00
2008-09-12 06:14:34 +02:00
require 'couchrest/monkeypatches'
2008-09-12 06:00:44 +02:00
2008-09-30 08:39:57 +02:00
# = CouchDB, close to the metal
2008-09-12 06:00:44 +02:00
module CouchRest
2009-07-23 01:54:28 +02:00
VERSION = '0.32' unless self . const_defined? ( " VERSION " )
2009-01-13 06:33:12 +01:00
2008-09-12 06:14:34 +02:00
autoload :Server , 'couchrest/core/server'
autoload :Database , 'couchrest/core/database'
2009-02-25 09:22:11 +01:00
autoload :Response , 'couchrest/core/response'
2008-11-04 07:52:50 +01:00
autoload :Document , 'couchrest/core/document'
2009-02-25 09:22:11 +01:00
autoload :Design , 'couchrest/core/design'
2008-11-04 07:53:46 +01:00
autoload :View , 'couchrest/core/view'
2008-09-29 18:55:40 +02:00
autoload :Model , 'couchrest/core/model'
2008-09-12 06:14:34 +02:00
autoload :Pager , 'couchrest/helper/pager'
autoload :FileManager , 'couchrest/helper/file_manager'
2008-09-12 06:22:43 +02:00
autoload :Streamer , 'couchrest/helper/streamer'
2009-03-15 02:42:34 +01:00
autoload :Upgrade , 'couchrest/helper/upgrade'
2008-09-12 06:14:34 +02:00
2009-01-30 03:45:01 +01:00
autoload :ExtendedDocument , 'couchrest/more/extended_document'
2009-02-09 20:20:23 +01:00
autoload :CastedModel , 'couchrest/more/casted_model'
2009-01-30 03:45:01 +01:00
2009-07-14 10:43:40 +02:00
require File . join ( File . dirname ( __FILE__ ) , 'couchrest' , 'core' , 'http_abstraction' )
2009-01-29 07:55:42 +01:00
require File . join ( File . dirname ( __FILE__ ) , 'couchrest' , 'mixins' )
2008-09-12 06:22:43 +02:00
# The CouchRest module methods handle the basic JSON serialization
2008-09-12 06:25:51 +02:00
# and deserialization, as well as query parameters. The module also includes
# some helpers for tasks like instantiating a new Database or Server instance.
2008-09-12 06:22:43 +02:00
class << self
2008-09-12 06:25:51 +02:00
2009-02-09 20:20:23 +01:00
# extracted from Extlib
#
# Constantize tries to find a declared constant with the name specified
# in the string. It raises a NameError when the name is not in CamelCase
# or is not initialized.
#
# @example
# "Module".constantize #=> Module
# "Class".constantize #=> Class
def constantize ( camel_cased_word )
unless / \ A(?:::)?([A-Z] \ w*(?:::[A-Z] \ w*)*) \ z / =~ camel_cased_word
raise NameError , " #{ camel_cased_word . inspect } is not a valid constant name! "
end
Object . module_eval ( " :: #{ $1 } " , __FILE__ , __LINE__ )
end
2009-03-18 19:32:49 +01:00
# extracted from Extlib
#
# Capitalizes the first word and turns underscores into spaces and strips _id.
# Like titleize, this is meant for creating pretty output.
#
# @example
# "employee_salary" #=> "Employee salary"
# "author_id" #=> "Author"
def humanize ( lower_case_and_underscored_word )
lower_case_and_underscored_word . to_s . gsub ( / _id$ / , " " ) . gsub ( / _ / , " " ) . capitalize
end
2008-09-12 06:25:51 +02:00
# todo, make this parse the url and instantiate a Server or Database instance
# depending on the specificity.
2008-09-12 06:22:43 +02:00
def new ( * opts )
Server . new ( * opts )
end
2008-09-14 03:07:21 +02:00
def parse url
case url
when / ^http: \/ \/ (.*) \/ (.*) \/ (.*) /
host = $1
db = $2
docid = $3
when / ^http: \/ \/ (.*) \/ (.*) /
host = $1
db = $2
when / ^http: \/ \/ (.*) /
host = $1
when / (.*) \/ (.*) \/ (.*) /
host = $1
db = $2
docid = $3
when / (.*) \/ (.*) /
host = $1
db = $2
else
db = url
end
db = nil if db && db . empty?
{
2008-12-14 12:05:02 +01:00
:host = > host || " 127.0.0.1:5984 " ,
2008-09-14 03:07:21 +02:00
:database = > db ,
:doc = > docid
}
end
2009-07-14 10:43:40 +02:00
# set proxy to use
2009-01-11 04:52:08 +01:00
def proxy url
2009-07-14 10:43:40 +02:00
HttpAbstraction . proxy = url
2009-01-11 04:52:08 +01:00
end
2008-09-12 06:25:51 +02:00
# ensure that a database exists
# creates it if it isn't already there
# returns it after it's been created
def database! url
2008-09-29 01:03:15 +02:00
parsed = parse url
cr = CouchRest . new ( parsed [ :host ] )
cr . database! ( parsed [ :database ] )
2008-09-12 06:25:51 +02:00
end
def database url
2008-09-29 01:03:15 +02:00
parsed = parse url
cr = CouchRest . new ( parsed [ :host ] )
cr . database ( parsed [ :database ] )
2008-09-12 06:25:51 +02:00
end
2009-02-27 04:53:01 +01:00
def put ( uri , doc = nil )
2009-03-03 06:26:18 +01:00
payload = doc . to_json if doc
2009-02-27 04:53:01 +01:00
begin
2009-07-14 10:43:40 +02:00
JSON . parse ( HttpAbstraction . put ( uri , payload ) )
2009-02-27 04:53:01 +01:00
rescue Exception = > e
2009-05-27 03:27:49 +02:00
if $DEBUG
2009-03-03 07:36:57 +01:00
raise " Error while sending a PUT request #{ uri } \n payload: #{ payload . inspect } \n #{ e } "
else
raise e
end
2009-02-27 04:53:01 +01:00
end
2008-09-12 06:22:43 +02:00
end
2009-02-27 03:44:39 +01:00
def get ( uri )
begin
2009-07-14 10:43:40 +02:00
JSON . parse ( HttpAbstraction . get ( uri ) , :max_nesting = > false )
2009-02-27 03:44:39 +01:00
rescue = > e
2009-05-27 03:27:49 +02:00
if $DEBUG
2009-03-03 07:36:57 +01:00
raise " Error while sending a GET request #{ uri } \n : #{ e } "
else
raise e
end
2009-02-27 03:44:39 +01:00
end
2008-09-12 06:22:43 +02:00
end
def post uri , doc = nil
payload = doc . to_json if doc
2009-03-03 06:26:18 +01:00
begin
2009-07-14 10:43:40 +02:00
JSON . parse ( HttpAbstraction . post ( uri , payload ) )
2009-03-03 06:26:18 +01:00
rescue Exception = > e
2009-05-27 03:27:49 +02:00
if $DEBUG
2009-03-03 07:36:57 +01:00
raise " Error while sending a POST request #{ uri } \n payload: #{ payload . inspect } \n #{ e } "
else
raise e
end
2009-03-03 06:26:18 +01:00
end
2008-09-12 06:22:43 +02:00
end
def delete uri
2009-07-14 10:43:40 +02:00
JSON . parse ( HttpAbstraction . delete ( uri ) )
2008-09-12 06:22:43 +02:00
end
2009-01-05 09:44:12 +01:00
def copy uri , destination
2009-07-14 10:43:40 +02:00
JSON . parse ( HttpAbstraction . copy ( uri , { 'Destination' = > destination } ) )
2009-01-05 09:44:12 +01:00
end
2008-09-12 06:22:43 +02:00
2008-10-08 21:19:28 +02:00
def paramify_url url , params = { }
if params && ! params . empty?
2008-09-12 06:22:43 +02:00
query = params . collect do | k , v |
v = v . to_json if %w{ key startkey endkey } . include? ( k . to_s )
" #{ k } = #{ CGI . escape ( v . to_s ) } "
end . join ( " & " )
url = " #{ url } ? #{ query } "
end
url
end
end # class << self
2008-09-12 06:00:44 +02:00
end