diff --git a/lib/couchrest.rb b/lib/couchrest.rb index 572902f..efb5847 100644 --- a/lib/couchrest.rb +++ b/lib/couchrest.rb @@ -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| diff --git a/lib/couchrest/core/rest_api.rb b/lib/couchrest/core/rest_api.rb new file mode 100644 index 0000000..62075b0 --- /dev/null +++ b/lib/couchrest/core/rest_api.rb @@ -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 \ No newline at end of file diff --git a/lib/couchrest/more/casted_model.rb b/lib/couchrest/more/casted_model.rb index 6f442c8..5e4da2e 100644 --- a/lib/couchrest/more/casted_model.rb +++ b/lib/couchrest/more/casted_model.rb @@ -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 diff --git a/spec/couchrest/core/database_spec.rb b/spec/couchrest/core/database_spec.rb index 3375779..8d4bdd9 100644 --- a/spec/couchrest/core/database_spec.rb +++ b/spec/couchrest/core/database_spec.rb @@ -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