Class: CouchRest

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

Defined Under Namespace

Classes: Database, FileManager, Pager

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CouchRest.



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

def initialize server = 'http://localhost:5984'
  @uri = server
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

Class Method Details

.delete(uri) ⇒ Object



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

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

.get(uri) ⇒ Object



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

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

.paramify_url(url, params = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/couch_rest.rb', line 51

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



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

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

.put(uri, doc = nil) ⇒ Object



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

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



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

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

#database(name) ⇒ Object



12
13
14
# File 'lib/couch_rest.rb', line 12

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

#databasesObject

list all databases on the server



8
9
10
# File 'lib/couch_rest.rb', line 8

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

#infoObject

get the welcome message



17
18
19
# File 'lib/couch_rest.rb', line 17

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

#restart!Object

restart the couchdb instance



22
23
24
# File 'lib/couch_rest.rb', line 22

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