made CouchRest a module
This commit is contained in:
parent
26de4acc5a
commit
f5fdc8b913
10 changed files with 110 additions and 114 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -1,6 +1,6 @@
|
|||
require 'fileutils'
|
||||
|
||||
class CouchRest
|
||||
module CouchRest
|
||||
module Commands
|
||||
module Generate
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
class CouchRest
|
||||
module CouchRest
|
||||
|
||||
module Commands
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
require 'cgi'
|
||||
require "base64"
|
||||
|
||||
class CouchRest
|
||||
module CouchRest
|
||||
class Database
|
||||
attr_reader :server, :host, :name, :root
|
||||
|
98
lib/couchrest/core/server.rb
Normal file
98
lib/couchrest/core/server.rb
Normal file
|
@ -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
|
|
@ -1,6 +1,6 @@
|
|||
require 'digest/md5'
|
||||
|
||||
class CouchRest
|
||||
module CouchRest
|
||||
class FileManager
|
||||
attr_reader :db
|
||||
attr_accessor :loud
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
class CouchRest
|
||||
module CouchRest
|
||||
class Pager
|
||||
attr_accessor :db
|
||||
def initialize db
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
class CouchRest
|
||||
module CouchRest
|
||||
class Streamer
|
||||
attr_accessor :db
|
||||
def initialize db
|
||||
|
|
|
@ -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
|
Loading…
Reference in a new issue