Merge commit 'mattetti/master'

This commit is contained in:
Peter Gumeson 2009-07-19 00:01:07 -07:00
commit 1e44302d1a
25 changed files with 695 additions and 135 deletions

View file

@ -0,0 +1,35 @@
module RestClientAdapter
module API
def proxy=(url)
RestClient.proxy = url
end
def proxy
RestClient.proxy
end
def get(uri, headers={})
RestClient.get(uri, headers)
end
def post(uri, payload, headers={})
RestClient.post(uri, payload, headers)
end
def put(uri, payload, headers={})
RestClient.put(uri, payload, headers)
end
def delete(uri, headers={})
RestClient.delete(uri, headers)
end
def copy(uri, headers)
RestClient::Request.execute( :method => :copy,
:url => uri,
:headers => headers)
end
end
end

View file

@ -58,7 +58,7 @@ module CouchRest
keys = params.delete(:keys)
funcs = funcs.merge({:keys => keys}) if keys
url = CouchRest.paramify_url "#{@root}/_temp_view", params
JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'}))
JSON.parse(HttpAbstraction.post(url, funcs.to_json, {"Content-Type" => 'application/json'}))
end
# backwards compatibility is a plus
@ -100,11 +100,8 @@ module CouchRest
# GET an attachment directly from CouchDB
def fetch_attachment(doc, name)
# slug = escape_docid(docid)
# name = CGI.escape(name)
uri = url_for_attachment(doc, name)
RestClient.get uri
# "#{@uri}/#{slug}/#{name}"
HttpAbstraction.get uri
end
# PUT an attachment directly to CouchDB
@ -112,14 +109,14 @@ module CouchRest
docid = escape_docid(doc['_id'])
name = CGI.escape(name)
uri = url_for_attachment(doc, name)
JSON.parse(RestClient.put(uri, file, options))
JSON.parse(HttpAbstraction.put(uri, file, options))
end
# DELETE an attachment directly from CouchDB
def delete_attachment doc, name
uri = url_for_attachment(doc, name)
# this needs a rev
JSON.parse(RestClient.delete(uri))
JSON.parse(HttpAbstraction.delete(uri))
end
# Save a document to CouchDB. This will use the <tt>_id</tt> field from
@ -146,7 +143,7 @@ module CouchRest
slug = escape_docid(doc['_id'])
begin
CouchRest.put "#{@root}/#{slug}", doc
rescue RestClient::ResourceNotFound
rescue HttpAbstraction::ResourceNotFound
p "resource not found when saving even tho an id was passed"
slug = doc['_id'] = @server.next_uuid
CouchRest.put "#{@root}/#{slug}", doc
@ -252,7 +249,7 @@ module CouchRest
def recreate!
delete!
create!
rescue RestClient::ResourceNotFound
rescue HttpAbstraction::ResourceNotFound
ensure
create!
end

View file

@ -3,10 +3,6 @@ require 'delegate'
module CouchRest
class Document < Response
include CouchRest::Mixins::Attachments
# def self.inherited(subklass)
# subklass.send(:extlib_inheritable_accessor, :database)
# end
extlib_inheritable_accessor :database
attr_accessor :database
@ -30,6 +26,7 @@ module CouchRest
def new?
!rev
end
alias :new_document? :new?
# Saves the document to the db using create or update. Also runs the :save
# callbacks. Sets the <tt>_id</tt> and <tt>_rev</tt> fields based on

View file

@ -0,0 +1,48 @@
require 'couchrest/core/adapters/restclient'
# Abstraction layet for HTTP communications.
#
# By defining a basic API that CouchRest is relying on,
# it allows for easy experimentations and implementations of various libraries.
#
# Most of the API is based on the RestClient API that was used in the early version of CouchRest.
#
module HttpAbstraction
# here is the list of exception expected by CouchRest
# please convert the underlying errors in this set of known
# exceptions.
class ResourceNotFound < StandardError; end
class RequestFailed < StandardError; end
class RequestTimeout < StandardError; end
class ServerBrokeConnection < StandardError; end
class Conflict < StandardError; end
# # Here is the API you need to implement if you want to write a new adapter
# # See adapters/restclient.rb for more information.
#
# def self.proxy=(url)
# end
#
# def self.proxy
# end
#
# def self.get(uri, headers=nil)
# end
#
# def self.post(uri, payload, headers=nil)
# end
#
# def self.put(uri, payload, headers=nil)
# end
#
# def self.delete(uri, headers=nil)
# end
#
# def self.copy(uri, headers)
# end
end
HttpAbstraction.extend(RestClientAdapter::API)