Class: CouchRest

Inherits:
Object
  • Object
show all
Defined in:
lib/pager.rb,
lib/database.rb,
lib/streamer.rb,
lib/couch_rest.rb,
lib/file_manager.rb

Defined Under Namespace

Classes: Database, FileManager, Pager, Streamer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server = 'http://localhost:5984', uuid_batch_count = 1000) ⇒ CouchRest

Returns a new instance of CouchRest.



3
4
5
6
# File 'lib/couch_rest.rb', line 3

def initialize server = 'http://localhost:5984', uuid_batch_count = 1000
  @uri = server
  @uuid_batch_count = uuid_batch_count
end

Instance Attribute Details

#uriObject

Returns the value of attribute uri.



2
3
4
# File 'lib/couch_rest.rb', line 2

def uri
  @uri
end

#uuid_batch_countObject

Returns the value of attribute uuid_batch_count.



2
3
4
# File 'lib/couch_rest.rb', line 2

def uuid_batch_count
  @uuid_batch_count
end

Class Method Details

.database(url) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/couch_rest.rb', line 19

def self.database url
  uri = URI.parse url
  path = uri.path
  uri.path = ''
  cr = CouchRest.new(uri.to_s)
  cr.database(path)
end

.database!(url) ⇒ Object

ensure that a database exists creates it if it isn’t already there returns it after it’s been created



11
12
13
14
15
16
17
# File 'lib/couch_rest.rb', line 11

def self.database! url
  uri = URI.parse url
  path = uri.path
  uri.path = ''
  cr = CouchRest.new(uri.to_s)
  cr.database!(path)
end

.delete(uri) ⇒ Object



81
82
83
# File 'lib/couch_rest.rb', line 81

def delete uri
  JSON.parse(RestClient.delete(uri))
end

.get(uri) ⇒ Object



72
73
74
# File 'lib/couch_rest.rb', line 72

def get uri
  JSON.parse(RestClient.get(uri), :max_nesting => false)
end

.paramify_url(url, params = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/couch_rest.rb', line 85

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

.post(uri, doc = nil) ⇒ Object



76
77
78
79
# File 'lib/couch_rest.rb', line 76

def post uri, doc = nil
  payload = doc.to_json if doc
  JSON.parse(RestClient.post(uri, payload))
end

.put(uri, doc = nil) ⇒ Object



67
68
69
70
# File 'lib/couch_rest.rb', line 67

def put uri, doc = nil
  payload = doc.to_json if doc
  JSON.parse(RestClient.put(uri, payload))
end

Instance Method Details

#create_db(name) ⇒ Object

create a database



48
49
50
51
# File 'lib/couch_rest.rb', line 48

def create_db name
  CouchRest.put "#{@uri}/#{name}"
  database name
end

#database(name) ⇒ Object



32
33
34
# File 'lib/couch_rest.rb', line 32

def database name
  CouchRest::Database.new(self, name)
end

#database!(name) ⇒ Object

creates the database if it doesn’t exist



37
38
39
40
# File 'lib/couch_rest.rb', line 37

def database! name
  create_db(path) rescue nil
  database name
end

#databasesObject

list all databases on the server



28
29
30
# File 'lib/couch_rest.rb', line 28

def databases
  CouchRest.get "#{@uri}/_all_dbs"
end

#infoObject

get the welcome message



43
44
45
# File 'lib/couch_rest.rb', line 43

def info
  CouchRest.get "#{@uri}/"
end

#next_uuid(count = @uuid_batch_count) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/couch_rest.rb', line 58

def next_uuid count = @uuid_batch_count
  @uuids ||= []
  if @uuids.empty?
    @uuids = CouchRest.post("#{@uri}/_uuids?count=#{count}")["uuids"]
  end
  @uuids.pop
end

#restart!Object

restart the couchdb instance



54
55
56
# File 'lib/couch_rest.rb', line 54

def restart!
  CouchRest.post "#{@uri}/_restart"
end