From 211331f4a6151e90e407178a90cb64b32b764006 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 11 Sep 2008 21:22:43 -0700 Subject: [PATCH] most specs passing with CouchRest module --- lib/couchrest.rb | 41 +++++++++++++++++-- lib/couchrest/core/server.rb | 30 -------------- .../{helpers => helper}/file_manager.rb | 0 lib/couchrest/{helpers => helper}/pager.rb | 0 lib/couchrest/{helpers => helper}/streamer.rb | 0 5 files changed, 38 insertions(+), 33 deletions(-) rename lib/couchrest/{helpers => helper}/file_manager.rb (100%) rename lib/couchrest/{helpers => helper}/pager.rb (100%) rename lib/couchrest/{helpers => helper}/streamer.rb (100%) diff --git a/lib/couchrest.rb b/lib/couchrest.rb index fc9320f..0ee562a 100644 --- a/lib/couchrest.rb +++ b/lib/couchrest.rb @@ -14,8 +14,43 @@ module CouchRest autoload :Database, 'couchrest/core/database' autoload :Pager, 'couchrest/helper/pager' autoload :FileManager, 'couchrest/helper/file_manager' + autoload :Streamer, 'couchrest/helper/streamer' - def self.new(*opts) - Server.new(*opts) - end + # The CouchRest module methods handle the basic JSON serialization + # and deserialization, as well as query parameters. + class << self + + def new(*opts) + Server.new(*opts) + end + + def put uri, doc = nil + payload = doc.to_json if doc + JSON.parse(RestClient.put(uri, payload)) + end + + def get uri + JSON.parse(RestClient.get(uri), :max_nesting => false) + end + + def post uri, doc = nil + payload = doc.to_json if doc + JSON.parse(RestClient.post(uri, payload)) + end + + def delete uri + JSON.parse(RestClient.delete(uri)) + end + + def paramify_url url, params = nil + if params + 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 end \ No newline at end of file diff --git a/lib/couchrest/core/server.rb b/lib/couchrest/core/server.rb index 91481e3..7365277 100644 --- a/lib/couchrest/core/server.rb +++ b/lib/couchrest/core/server.rb @@ -64,35 +64,5 @@ module CouchRest @uuids.pop end - class << self - def put uri, doc = nil - payload = doc.to_json if doc - JSON.parse(RestClient.put(uri, payload)) - end - - def get uri - JSON.parse(RestClient.get(uri), :max_nesting => false) - end - - def post uri, doc = nil - payload = doc.to_json if doc - JSON.parse(RestClient.post(uri, payload)) - end - - def delete uri - JSON.parse(RestClient.delete(uri)) - end - - def paramify_url url, params = nil - if params - 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 end end diff --git a/lib/couchrest/helpers/file_manager.rb b/lib/couchrest/helper/file_manager.rb similarity index 100% rename from lib/couchrest/helpers/file_manager.rb rename to lib/couchrest/helper/file_manager.rb diff --git a/lib/couchrest/helpers/pager.rb b/lib/couchrest/helper/pager.rb similarity index 100% rename from lib/couchrest/helpers/pager.rb rename to lib/couchrest/helper/pager.rb diff --git a/lib/couchrest/helpers/streamer.rb b/lib/couchrest/helper/streamer.rb similarity index 100% rename from lib/couchrest/helpers/streamer.rb rename to lib/couchrest/helper/streamer.rb