diff --git a/lib/couchrest.rb b/lib/couchrest.rb index e63b520..42ffeca 100644 --- a/lib/couchrest.rb +++ b/lib/couchrest.rb @@ -2,15 +2,14 @@ require "rubygems" require 'json' require 'rest_client' -$:.unshift File.expand_path(File.dirname(__FILE__)) - - -require 'monkeypatches' -require 'couchrest/server' -require 'couchrest/database' - +require 'couchrest/monkeypatches' module CouchRest + autoload :Server, 'couchrest/core/server' + autoload :Database, 'couchrest/core/database' + autoload :Pager, 'couchrest/helper/pager' + autoload :FileManager, 'couchrest/helper/file_manager' + def self.new(*opts) Server.new(*opts) end diff --git a/lib/couchrest/commands.rb b/lib/couchrest/commands.rb deleted file mode 100644 index 88369eb..0000000 --- a/lib/couchrest/commands.rb +++ /dev/null @@ -1,5 +0,0 @@ -require File.join(File.dirname(__FILE__), "..", "couchrest") - -%w(push generate).each do |filename| - require File.join(File.dirname(__FILE__), "commands", filename) -end diff --git a/lib/couchrest/commands/generate.rb b/lib/couchrest/commands/generate.rb index bc46bcb..dfdfc22 100644 --- a/lib/couchrest/commands/generate.rb +++ b/lib/couchrest/commands/generate.rb @@ -1,6 +1,6 @@ require 'fileutils' -class CouchRest +module CouchRest module Commands module Generate diff --git a/lib/couchrest/commands/push.rb b/lib/couchrest/commands/push.rb index a9a2e95..604639d 100644 --- a/lib/couchrest/commands/push.rb +++ b/lib/couchrest/commands/push.rb @@ -1,4 +1,4 @@ -class CouchRest +module CouchRest module Commands diff --git a/lib/couchrest/database.rb b/lib/couchrest/core/database.rb similarity index 99% rename from lib/couchrest/database.rb rename to lib/couchrest/core/database.rb index 73397f6..90c99f5 100644 --- a/lib/couchrest/database.rb +++ b/lib/couchrest/core/database.rb @@ -1,7 +1,7 @@ require 'cgi' require "base64" -class CouchRest +module CouchRest class Database attr_reader :server, :host, :name, :root diff --git a/lib/couchrest/core/server.rb b/lib/couchrest/core/server.rb new file mode 100644 index 0000000..91481e3 --- /dev/null +++ b/lib/couchrest/core/server.rb @@ -0,0 +1,98 @@ +module CouchRest + class Server + attr_accessor :uri, :uuid_batch_count + def initialize server = 'http://localhost:5984', uuid_batch_count = 1000 + @uri = server + @uuid_batch_count = uuid_batch_count + end + + # ensure that a database exists + # creates it if it isn't already there + # returns it after it's been created + def self.database! url + uri = URI.parse url + path = uri.path + uri.path = '' + cr = CouchRest.new(uri.to_s) + cr.database!(path) + end + + def self.database url + uri = URI.parse url + path = uri.path + uri.path = '' + cr = CouchRest.new(uri.to_s) + cr.database(path) + end + + # list all databases on the server + def databases + CouchRest.get "#{@uri}/_all_dbs" + end + + def database name + CouchRest::Database.new(self, name) + end + + # creates the database if it doesn't exist + def database! name + create_db(name) rescue nil + database name + end + + # get the welcome message + def info + CouchRest.get "#{@uri}/" + end + + # create a database + def create_db name + CouchRest.put "#{@uri}/#{name}" + database name + end + + # restart the couchdb instance + def restart! + CouchRest.post "#{@uri}/_restart" + end + + def next_uuid count = @uuid_batch_count + @uuids ||= [] + if @uuids.empty? + @uuids = CouchRest.post("#{@uri}/_uuids?count=#{count}")["uuids"] + end + @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/helpers/file_manager.rb index 60ee7a4..e41940e 100644 --- a/lib/couchrest/helpers/file_manager.rb +++ b/lib/couchrest/helpers/file_manager.rb @@ -1,6 +1,6 @@ require 'digest/md5' -class CouchRest +module CouchRest class FileManager attr_reader :db attr_accessor :loud diff --git a/lib/couchrest/helpers/pager.rb b/lib/couchrest/helpers/pager.rb index 229ef8a..6442a90 100644 --- a/lib/couchrest/helpers/pager.rb +++ b/lib/couchrest/helpers/pager.rb @@ -1,4 +1,4 @@ -class CouchRest +module CouchRest class Pager attr_accessor :db def initialize db diff --git a/lib/couchrest/helpers/streamer.rb b/lib/couchrest/helpers/streamer.rb index c9133ee..ee7d4c0 100644 --- a/lib/couchrest/helpers/streamer.rb +++ b/lib/couchrest/helpers/streamer.rb @@ -1,4 +1,4 @@ -class CouchRest +module CouchRest class Streamer attr_accessor :db def initialize db diff --git a/lib/couchrest/server.rb b/lib/couchrest/server.rb deleted file mode 100644 index fa5e8ab..0000000 --- a/lib/couchrest/server.rb +++ /dev/null @@ -1,96 +0,0 @@ -class CouchRest - attr_accessor :uri, :uuid_batch_count - def initialize server = 'http://localhost:5984', uuid_batch_count = 1000 - @uri = server - @uuid_batch_count = uuid_batch_count - end - - # ensure that a database exists - # creates it if it isn't already there - # returns it after it's been created - def self.database! url - uri = URI.parse url - path = uri.path - uri.path = '' - cr = CouchRest.new(uri.to_s) - cr.database!(path) - end - - def self.database url - uri = URI.parse url - path = uri.path - uri.path = '' - cr = CouchRest.new(uri.to_s) - cr.database(path) - end - - # list all databases on the server - def databases - CouchRest.get "#{@uri}/_all_dbs" - end - - def database name - CouchRest::Database.new(self, name) - end - - # creates the database if it doesn't exist - def database! name - create_db(name) rescue nil - database name - end - - # get the welcome message - def info - CouchRest.get "#{@uri}/" - end - - # create a database - def create_db name - CouchRest.put "#{@uri}/#{name}" - database name - end - - # restart the couchdb instance - def restart! - CouchRest.post "#{@uri}/_restart" - end - - def next_uuid count = @uuid_batch_count - @uuids ||= [] - if @uuids.empty? - @uuids = CouchRest.post("#{@uri}/_uuids?count=#{count}")["uuids"] - end - @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