extracted the rest API to its own module

This commit is contained in:
Matt Aimonetti 2009-07-29 18:32:34 -07:00
parent 0958be55f7
commit 9141669df1
4 changed files with 56 additions and 48 deletions

View file

@ -45,8 +45,13 @@ module CouchRest
autoload :ExtendedDocument, 'couchrest/more/extended_document'
autoload :CastedModel, 'couchrest/more/casted_model'
require File.join(File.dirname(__FILE__), 'couchrest', 'core', 'rest_api')
require File.join(File.dirname(__FILE__), 'couchrest', 'core', 'http_abstraction')
require File.join(File.dirname(__FILE__), 'couchrest', 'mixins')
# we extend CouchRest with the RestAPI module which gives us acess to
# the get, post, put, delete and copy
CouchRest.extend(::RestAPI)
# The CouchRest module methods handle the basic JSON serialization
# and deserialization, as well as query parameters. The module also includes
@ -139,52 +144,6 @@ module CouchRest
cr.database(parsed[:database])
end
def put(uri, doc = nil)
payload = doc.to_json if doc
begin
JSON.parse(HttpAbstraction.put(uri, payload))
rescue Exception => e
if $DEBUG
raise "Error while sending a PUT request #{uri}\npayload: #{payload.inspect}\n#{e}"
else
raise e
end
end
end
def get(uri)
begin
JSON.parse(HttpAbstraction.get(uri), :max_nesting => false)
rescue => e
if $DEBUG
raise "Error while sending a GET request #{uri}\n: #{e}"
else
raise e
end
end
end
def post uri, doc = nil
payload = doc.to_json if doc
begin
JSON.parse(HttpAbstraction.post(uri, payload))
rescue Exception => e
if $DEBUG
raise "Error while sending a POST request #{uri}\npayload: #{payload.inspect}\n#{e}"
else
raise e
end
end
end
def delete uri
JSON.parse(HttpAbstraction.delete(uri))
end
def copy uri, destination
JSON.parse(HttpAbstraction.copy(uri, {'Destination' => destination}))
end
def paramify_url url, params = {}
if params && !params.empty?
query = params.collect do |k,v|

View file

@ -0,0 +1,49 @@
module RestAPI
def put(uri, doc = nil)
payload = doc.to_json if doc
begin
JSON.parse(HttpAbstraction.put(uri, payload))
rescue Exception => e
if $DEBUG
raise "Error while sending a PUT request #{uri}\npayload: #{payload.inspect}\n#{e}"
else
raise e
end
end
end
def get(uri)
begin
JSON.parse(HttpAbstraction.get(uri), :max_nesting => false)
rescue => e
if $DEBUG
raise "Error while sending a GET request #{uri}\n: #{e}"
else
raise e
end
end
end
def post(uri, doc = nil)
payload = doc.to_json if doc
begin
JSON.parse(HttpAbstraction.post(uri, payload))
rescue Exception => e
if $DEBUG
raise "Error while sending a POST request #{uri}\npayload: #{payload.inspect}\n#{e}"
else
raise e
end
end
end
def delete(uri)
JSON.parse(HttpAbstraction.delete(uri))
end
def copy(uri, destination)
JSON.parse(HttpAbstraction.copy(uri, {'Destination' => destination}))
end
end

View file

@ -4,7 +4,7 @@ module CouchRest
module CastedModel
def self.included(base)
base.send(:include, CouchRest::Mixins::Properties)
base.send(:include, ::CouchRest::Mixins::Properties)
base.send(:attr_accessor, :casted_by)
end

View file

@ -369,7 +369,7 @@ describe CouchRest::Database do
it "should delete the attachment" do
lambda { @db.fetch_attachment(@doc,'test.html') }.should_not raise_error
@db.delete_attachment(@doc, "test.html")
lambda { @db.fetch_attachment(@doc,'test.html') }.should raise_error(RestClient::ResourceNotFound)
@db.fetch_attachment(@doc,'test.html').should be_nil
end
end